Skip to main content

treetop_bundle/
diagnostic.rs

1use serde::{Deserialize, Serialize};
2
3/// Severity of a bundle diagnostic.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "lowercase")]
6pub enum DiagnosticSeverity {
7    Error,
8    Warning,
9}
10
11/// A stable, serializable compiler diagnostic.
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(deny_unknown_fields)]
14pub struct Diagnostic {
15    pub severity: DiagnosticSeverity,
16    pub code: String,
17    pub module: Option<String>,
18    pub path: Option<String>,
19    pub line: Option<usize>,
20    pub column: Option<usize>,
21    pub message: String,
22}
23
24impl Diagnostic {
25    pub(crate) fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
26        Self {
27            severity: DiagnosticSeverity::Error,
28            code: code.into(),
29            module: None,
30            path: None,
31            line: None,
32            column: None,
33            message: message.into(),
34        }
35    }
36
37    pub(crate) fn warning(code: impl Into<String>, message: impl Into<String>) -> Self {
38        Self {
39            severity: DiagnosticSeverity::Warning,
40            code: code.into(),
41            module: None,
42            path: None,
43            line: None,
44            column: None,
45            message: message.into(),
46        }
47    }
48
49    pub(crate) fn in_module(mut self, module: impl Into<String>) -> Self {
50        self.module = Some(module.into());
51        self
52    }
53
54    pub(crate) fn at_path(mut self, path: impl Into<String>) -> Self {
55        self.path = Some(path.into());
56        self
57    }
58}