miniextendr_lint/
diagnostic.rs1use std::fmt;
4use std::path::PathBuf;
5
6use crate::lint_code::LintCode;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
10pub enum Severity {
11 Info,
13 Warning,
15 Error,
17}
18
19impl fmt::Display for Severity {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 match self {
22 Self::Info => f.write_str("info"),
23 Self::Warning => f.write_str("warning"),
24 Self::Error => f.write_str("error"),
25 }
26 }
27}
28
29#[derive(Clone, Debug)]
31pub struct Diagnostic {
32 pub code: LintCode,
34 pub severity: Severity,
36 pub path: PathBuf,
38 pub line: usize,
40 pub message: String,
42 pub help: Option<String>,
44}
45
46impl Diagnostic {
47 pub fn new(code: LintCode, path: impl Into<PathBuf>, line: usize, message: String) -> Self {
49 Self {
50 severity: code.default_severity(),
51 code,
52 path: path.into(),
53 line,
54 message,
55 help: None,
56 }
57 }
58
59 pub fn with_help(mut self, help: impl Into<String>) -> Self {
61 self.help = Some(help.into());
62 self
63 }
64
65 pub fn to_legacy_string(&self) -> String {
67 let mut s = format!("{}:{}: {}", self.path.display(), self.line, self.message);
68 if let Some(ref help) = self.help {
69 s.push(' ');
70 s.push_str(help);
71 }
72 s
73 }
74}
75
76impl fmt::Display for Diagnostic {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 write!(
79 f,
80 "[{}] {}:{}: {}",
81 self.code,
82 self.path.display(),
83 self.line,
84 self.message,
85 )?;
86 if let Some(ref help) = self.help {
87 write!(f, " Help: {}", help)?;
88 }
89 Ok(())
90 }
91}