Skip to main content

corpora_core/
diagnostic.rs

1//! Diagnostics are data, not panics — the corpus stays inspectable on any failure.
2
3use crate::model::DocPath;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum Severity {
7    Error,
8    Warning,
9}
10
11#[derive(Clone, Debug)]
12pub struct Diagnostic {
13    pub severity: Severity,
14    pub code: String,
15    pub path: DocPath,
16    pub message: String,
17}
18
19impl Diagnostic {
20    pub fn error(code: &str, path: &DocPath, message: impl Into<String>) -> Self {
21        Diagnostic {
22            severity: Severity::Error,
23            code: code.to_string(),
24            path: path.clone(),
25            message: message.into(),
26        }
27    }
28
29    pub fn warn(code: &str, path: &DocPath, message: impl Into<String>) -> Self {
30        Diagnostic {
31            severity: Severity::Warning,
32            code: code.to_string(),
33            path: path.clone(),
34            message: message.into(),
35        }
36    }
37}
38
39impl std::fmt::Display for Diagnostic {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        let sev = match self.severity {
42            Severity::Error => "error",
43            Severity::Warning => "warning",
44        };
45        write!(f, "{sev}[{}] {}: {}", self.code, self.path.0, self.message)
46    }
47}