Skip to main content

rustex_diagnostics/
lib.rs

1use camino::Utf8PathBuf;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
5#[serde(rename_all = "lowercase")]
6pub enum Severity {
7    Error,
8    Warning,
9    Note,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
13pub struct SourceSpan {
14    pub file: Utf8PathBuf,
15    pub line: usize,
16    pub column: usize,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
20pub struct Diagnostic {
21    pub code: String,
22    pub severity: Severity,
23    pub message: String,
24    pub symbol: Option<String>,
25    pub provenance: Option<String>,
26    pub suggestion: Option<String>,
27    pub primary_span: Option<SourceSpan>,
28    #[serde(default)]
29    pub related_spans: Vec<SourceSpan>,
30    pub snippet: Option<String>,
31}
32
33impl Diagnostic {
34    pub fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
35        Self {
36            code: code.into(),
37            severity: Severity::Error,
38            message: message.into(),
39            symbol: None,
40            provenance: None,
41            suggestion: None,
42            primary_span: None,
43            related_spans: Vec::new(),
44            snippet: None,
45        }
46    }
47}