1use serde::{Deserialize, Serialize};
4
5use crate::path::Path;
6use crate::tree::{Node, Span};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
10#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
11#[serde(rename_all = "snake_case")]
12pub enum Severity {
13 Error,
15 Warning,
17 Hint,
19}
20
21impl Severity {
22 #[must_use]
24 pub const fn name(self) -> &'static str {
25 match self {
26 Self::Error => "error",
27 Self::Warning => "warning",
28 Self::Hint => "hint",
29 }
30 }
31}
32
33#[derive(Debug, PartialEq, Eq, Serialize)]
36pub struct Code {
37 pub name: &'static str,
39 pub severity: Severity,
41 pub level: u8,
43 pub summary: &'static str,
45}
46
47impl Code {
48 #[must_use]
50 pub fn layer(&self) -> &'static str {
51 self.name.split('.').next().unwrap_or(self.name)
52 }
53}
54
55pub const REVIEW_PREFIX: &str = "review.";
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
60#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
61#[serde(rename_all = "snake_case")]
62pub enum Applicability {
63 Certain,
66 Likely,
68}
69
70#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
72#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
73#[serde(tag = "op", rename_all = "snake_case")]
74pub enum Edit {
75 Text {
77 offset: usize,
79 length: usize,
81 replacement: String,
83 },
84 Replace {
86 path: Path,
88 value: Node,
90 },
91 RenameKey {
93 path: Path,
95 to: String,
97 },
98 Remove {
100 path: Path,
102 },
103 Insert {
106 path: Path,
108 value: Node,
110 },
111 Move {
113 from: Path,
115 to: Path,
117 },
118}
119
120impl Edit {
121 #[must_use]
123 pub const fn path(&self) -> Option<&Path> {
124 match self {
125 Self::Text { .. } => None,
126 Self::Replace { path, .. }
127 | Self::RenameKey { path, .. }
128 | Self::Remove { path }
129 | Self::Insert { path, .. } => Some(path),
130 Self::Move { from, .. } => Some(from),
131 }
132 }
133}
134
135#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
137#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
138pub struct Fix {
139 pub title: String,
141 pub applicability: Applicability,
143 pub edits: Vec<Edit>,
145}
146
147impl Fix {
148 #[must_use]
150 pub fn certain(title: impl Into<String>, edits: Vec<Edit>) -> Self {
151 Self {
152 title: title.into(),
153 applicability: Applicability::Certain,
154 edits,
155 }
156 }
157
158 #[must_use]
160 pub fn likely(title: impl Into<String>, edits: Vec<Edit>) -> Self {
161 Self {
162 title: title.into(),
163 applicability: Applicability::Likely,
164 edits,
165 }
166 }
167}
168
169#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
172#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
173pub struct Reference {
174 pub topic: String,
176 pub definition: String,
178}
179
180#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
182#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
183pub struct Diagnostic {
184 pub code: String,
186 pub severity: Severity,
188 pub level: u8,
190 pub path: Path,
192 #[serde(default, skip_serializing_if = "Option::is_none")]
194 pub span: Option<Span>,
195 pub message: String,
197 #[serde(default)]
199 pub help: Vec<String>,
200 #[serde(default)]
202 pub fixes: Vec<Fix>,
203 #[serde(default, skip_serializing_if = "Option::is_none")]
205 pub reference: Option<Reference>,
206 #[serde(default, skip_serializing_if = "Option::is_none")]
208 pub subject: Option<Node>,
209 #[serde(default = "one")]
211 pub count: usize,
212 #[serde(default, skip_serializing_if = "Vec::is_empty")]
214 pub also: Vec<Path>,
215 #[serde(default, skip_serializing_if = "Option::is_none")]
217 pub data: Option<Node>,
218 #[serde(default, skip_serializing_if = "Option::is_none")]
220 pub origin: Option<String>,
221 #[serde(default, skip_serializing_if = "Option::is_none")]
223 pub excerpt: Option<String>,
224}
225
226const fn one() -> usize {
227 1
228}
229
230impl Diagnostic {
231 #[must_use]
233 pub fn new(code: &'static Code, path: Path, message: impl Into<String>) -> Self {
234 Self::external(code.name, code.severity, code.level, path, message)
235 }
236
237 #[must_use]
240 pub fn external(
241 code: impl Into<String>,
242 severity: Severity,
243 level: u8,
244 path: Path,
245 message: impl Into<String>,
246 ) -> Self {
247 Self {
248 code: code.into(),
249 severity,
250 level,
251 path,
252 span: None,
253 message: message.into(),
254 help: Vec::new(),
255 fixes: Vec::new(),
256 reference: None,
257 subject: None,
258 count: 1,
259 also: Vec::new(),
260 data: None,
261 origin: None,
262 excerpt: None,
263 }
264 }
265
266 #[must_use]
268 pub const fn at(mut self, span: Option<Span>) -> Self {
269 self.span = span;
270 self
271 }
272
273 #[must_use]
275 pub fn help(mut self, line: impl Into<String>) -> Self {
276 self.help.push(line.into());
277 self
278 }
279
280 #[must_use]
282 pub fn fix(mut self, fix: Fix) -> Self {
283 self.fixes.push(fix);
284 self
285 }
286
287 #[must_use]
289 pub fn reference(mut self, topic: impl Into<String>, definition: impl Into<String>) -> Self {
290 self.reference = Some(Reference {
291 topic: topic.into(),
292 definition: definition.into(),
293 });
294 self
295 }
296
297 #[must_use]
299 pub fn subject(mut self, subject: Node) -> Self {
300 self.subject = Some(subject);
301 self
302 }
303
304 #[must_use]
306 pub fn data(mut self, data: Node) -> Self {
307 self.data = Some(data);
308 self
309 }
310
311 #[must_use]
313 pub fn origin(mut self, origin: impl Into<String>) -> Self {
314 self.origin = Some(origin.into());
315 self
316 }
317
318 #[must_use]
320 pub fn is(&self, code: &Code) -> bool {
321 self.code == code.name
322 }
323
324 #[must_use]
326 pub fn is_error(&self) -> bool {
327 self.severity == Severity::Error
328 }
329
330 #[must_use]
332 pub fn datum(&self, key: &str) -> Option<&Node> {
333 self.data.as_ref()?.get(key)
334 }
335
336 #[must_use]
338 pub fn has_advice(&self) -> bool {
339 !self.help.is_empty() || !self.fixes.is_empty()
340 }
341}
342
343#[derive(Clone, Debug)]
345pub struct Collector {
346 items: Vec<Diagnostic>,
347 dropped: usize,
348 capacity: usize,
349}
350
351impl Default for Collector {
352 fn default() -> Self {
353 Self::new(Self::DEFAULT_CAPACITY)
354 }
355}
356
357impl Collector {
358 pub const DEFAULT_CAPACITY: usize = 1000;
360
361 #[must_use]
363 pub const fn new(capacity: usize) -> Self {
364 Self {
365 items: Vec::new(),
366 dropped: 0,
367 capacity,
368 }
369 }
370
371 pub fn push(&mut self, diagnostic: Diagnostic) {
373 if self.items.len() < self.capacity {
374 self.items.push(diagnostic);
375 } else {
376 self.dropped += 1;
377 }
378 }
379
380 #[must_use]
382 pub const fn is_full(&self) -> bool {
383 self.items.len() >= self.capacity
384 }
385
386 #[must_use]
388 pub fn items(&self) -> &[Diagnostic] {
389 &self.items
390 }
391
392 pub fn items_mut(&mut self) -> &mut [Diagnostic] {
394 &mut self.items
395 }
396
397 #[must_use]
399 pub const fn dropped(&self) -> usize {
400 self.dropped
401 }
402
403 #[must_use]
405 pub fn has_errors(&self) -> bool {
406 self.items.iter().any(Diagnostic::is_error)
407 }
408
409 #[must_use]
411 pub fn into_parts(self) -> (Vec<Diagnostic>, usize) {
412 (self.items, self.dropped)
413 }
414}
415
416impl Extend<Diagnostic> for Collector {
417 fn extend<I: IntoIterator<Item = Diagnostic>>(&mut self, items: I) {
418 for item in items {
419 self.push(item);
420 }
421 }
422}