Skip to main content

mirascript_core/diagnostic/
source_diagnostic.rs

1use std::{error::Error, fmt::Display, ops::Deref};
2
3use super::{DiagnosticCode, SourceRange};
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct SourceDiagnostic {
7    pub range: SourceRange,
8    pub error: DiagnosticCode,
9}
10
11impl SourceDiagnostic {
12    pub fn new(range: SourceRange, error: DiagnosticCode) -> Self {
13        SourceDiagnostic { range, error }
14    }
15}
16
17impl Display for SourceDiagnostic {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(
20            f,
21            "{}({}) at {}~{}: {}",
22            self.error,
23            self.code(),
24            self.range.start,
25            self.range.end,
26            self.error.message()
27        )
28    }
29}
30
31impl Error for SourceDiagnostic {
32    fn source(&self) -> Option<&(dyn Error + 'static)> {
33        None
34    }
35}
36
37impl Deref for SourceDiagnostic {
38    fn deref(&self) -> &DiagnosticCode {
39        &self.error
40    }
41
42    type Target = DiagnosticCode;
43}