Skip to main content

allium_parser/
diagnostic.rs

1use serde::Serialize;
2
3use crate::Span;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6pub enum Severity {
7    Error,
8    Warning,
9    Info,
10}
11
12#[derive(Debug, Clone, Serialize)]
13pub struct Diagnostic {
14    pub span: Span,
15    pub message: String,
16    pub severity: Severity,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub code: Option<&'static str>,
19}
20
21impl Diagnostic {
22    pub fn error(span: Span, message: impl Into<String>) -> Self {
23        Self { span, message: message.into(), severity: Severity::Error, code: None }
24    }
25
26    pub fn warning(span: Span, message: impl Into<String>) -> Self {
27        Self { span, message: message.into(), severity: Severity::Warning, code: None }
28    }
29
30    pub fn info(span: Span, message: impl Into<String>) -> Self {
31        Self { span, message: message.into(), severity: Severity::Info, code: None }
32    }
33
34    pub fn with_code(mut self, code: &'static str) -> Self {
35        self.code = Some(code);
36        self
37    }
38}