use crate::model::DocPath;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Severity {
Error,
Warning,
}
#[derive(Clone, Debug)]
pub struct Diagnostic {
pub severity: Severity,
pub code: String,
pub path: DocPath,
pub message: String,
}
impl Diagnostic {
pub fn error(code: &str, path: &DocPath, message: impl Into<String>) -> Self {
Diagnostic {
severity: Severity::Error,
code: code.to_string(),
path: path.clone(),
message: message.into(),
}
}
pub fn warn(code: &str, path: &DocPath, message: impl Into<String>) -> Self {
Diagnostic {
severity: Severity::Warning,
code: code.to_string(),
path: path.clone(),
message: message.into(),
}
}
}
impl std::fmt::Display for Diagnostic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let sev = match self.severity {
Severity::Error => "error",
Severity::Warning => "warning",
};
write!(f, "{sev}[{}] {}: {}", self.code, self.path.0, self.message)
}
}