1use std::fmt;
4
5use crate::mark::Mark;
6use crate::path::Path;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum Severity {
11 Warning,
13 Error,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum Origin {
20 Model,
22 File {
24 name: String,
26 line: usize,
28 },
29 Environment {
31 name: String,
33 },
34}
35
36impl fmt::Display for Origin {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 match self {
39 Self::Model => Ok(()),
40 Self::File { name, line } => write!(f, "{name}:{line}"),
41 Self::Environment { name } => write!(f, "environment variable {name}"),
42 }
43 }
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum Kind {
49 Source,
51 UnknownName,
53 Obsolete,
55 InvalidValue,
57 Validation,
59 Strictness,
61}
62
63#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct Problem {
66 pub severity: Severity,
68 pub kind: Kind,
70 pub origin: Origin,
72 pub path: Path,
74 pub message: String,
76 pub similar: Vec<String>,
78}
79
80impl fmt::Display for Problem {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 let mut head = false;
84 if !matches!(self.origin, Origin::Model) {
85 write!(f, "{}", self.origin)?;
86 head = true;
87 }
88 if !self.path.is_root() {
89 if head {
90 f.write_str(": ")?;
91 }
92 write!(f, "{}", self.path)?;
93 head = true;
94 }
95 if head {
96 f.write_str(": ")?;
97 }
98 f.write_str(&self.message)?;
99 if !self.similar.is_empty() {
100 let similar: Vec<String> = self
101 .similar
102 .iter()
103 .map(|name| format!("{name:?}"))
104 .collect();
105 write!(f, " (similar: {})", similar.join(", "))?;
106 }
107 Ok(())
108 }
109}
110
111#[derive(Debug, Clone, PartialEq, Eq)]
113pub struct Problems {
114 items: Vec<Problem>,
115 unknown_names: Severity,
116}
117
118impl Default for Problems {
119 fn default() -> Self {
120 Self {
121 items: Vec::new(),
122 unknown_names: Severity::Error,
123 }
124 }
125}
126
127impl Problems {
128 pub fn new() -> Self {
130 Self::default()
131 }
132
133 pub(crate) fn lenient() -> Self {
135 Self {
136 items: Vec::new(),
137 unknown_names: Severity::Warning,
138 }
139 }
140
141 pub(crate) fn unknown_name(
143 &mut self,
144 mark: &Mark,
145 path: Path,
146 name: &str,
147 similar: Vec<String>,
148 ) {
149 let severity = self.unknown_names;
150 self.push(
151 severity,
152 Kind::UnknownName,
153 mark,
154 path,
155 format!("unknown name {name:?}"),
156 similar,
157 );
158 }
159
160 pub(crate) fn error(&mut self, kind: Kind, mark: &Mark, path: Path, message: String) {
162 self.push(Severity::Error, kind, mark, path, message, Vec::new());
163 }
164
165 pub(crate) fn warning(&mut self, kind: Kind, mark: &Mark, path: Path, message: String) {
167 self.push(Severity::Warning, kind, mark, path, message, Vec::new());
168 }
169
170 fn push(
171 &mut self,
172 severity: Severity,
173 kind: Kind,
174 mark: &Mark,
175 path: Path,
176 message: String,
177 similar: Vec<String>,
178 ) {
179 let origin = mark.origin();
180 self.items.push(Problem {
181 severity,
182 kind,
183 origin,
184 path,
185 message,
186 similar,
187 });
188 }
189
190 pub fn iter(&self) -> impl Iterator<Item = &Problem> {
192 self.items.iter()
193 }
194
195 pub fn has_errors(&self) -> bool {
197 self.items
198 .iter()
199 .any(|problem| problem.severity == Severity::Error)
200 }
201
202 pub fn is_empty(&self) -> bool {
204 self.items.is_empty()
205 }
206
207 pub fn len(&self) -> usize {
209 self.items.len()
210 }
211}
212
213impl fmt::Display for Problems {
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216 for (position, problem) in self.items.iter().enumerate() {
217 if position > 0 {
218 f.write_str("\n")?;
219 }
220 write!(f, "{problem}")?;
221 }
222 Ok(())
223 }
224}
225
226impl std::error::Error for Problems {}
227
228impl IntoIterator for Problems {
229 type Item = Problem;
230 type IntoIter = std::vec::IntoIter<Problem>;
231
232 fn into_iter(self) -> Self::IntoIter {
233 self.items.into_iter()
234 }
235}