use crate::diagnostics::registry::{lookup, Severity};
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Diagnostic {
pub code: &'static str,
pub error: String,
pub fix: String,
pub example: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub span: Option<Span>,
pub severity: Severity,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_url: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Span {
pub file: PathBuf,
pub line: u32,
pub col: u32,
pub end_line: u32,
pub end_col: u32,
}
impl Diagnostic {
pub fn error(
code: &'static str,
error: impl Into<String>,
fix: impl Into<String>,
example: impl Into<String>,
) -> Self {
let severity = lookup(code).map(|e| e.severity).unwrap_or_else(|| {
debug_assert!(false, "diagnostic code {code} not in registry");
Severity::Error
});
Self {
code,
error: error.into(),
fix: fix.into(),
example: example.into(),
span: None,
severity,
doc_url: Some(format!("https://shaperail.io/errors/{code}.html")),
}
}
pub fn with_span(mut self, span: Span) -> Self {
self.span = Some(span);
self
}
}