Skip to main content

chappi_syntax/
diagnostic.rs

1use serde::{Deserialize, Serialize};
2
3use crate::Span;
4
5/// The externally visible severity of a syntax diagnostic.
6#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
7pub enum Severity {
8    Error,
9    Warning,
10}
11
12/// A structured parser error suitable for either human or machine output.
13#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
14pub struct Diagnostic {
15    pub code: String,
16    pub severity: Severity,
17    pub span: Span,
18    pub semantic_path: Vec<String>,
19    pub message: String,
20    pub hint: Option<String>,
21}
22
23impl Diagnostic {
24    pub(crate) fn error(code: &str, span: Span, message: impl Into<String>) -> Self {
25        Self {
26            code: code.to_owned(),
27            severity: Severity::Error,
28            span,
29            semantic_path: Vec::new(),
30            message: message.into(),
31            hint: None,
32        }
33    }
34}