Skip to main content

flowsurface_indicator_engine/
diagnostics.rs

1use std::fmt::{Display, Formatter};
2use std::path::PathBuf;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct Span {
6    pub start: usize,
7    pub end: usize,
8}
9
10impl Span {
11    pub const fn new(start: usize, end: usize) -> Self {
12        Self { start, end }
13    }
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum Severity {
18    Error,
19    Warning,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct Diagnostic {
24    pub severity: Severity,
25    pub message: String,
26    pub path: PathBuf,
27    pub span: Span,
28}
29
30impl Diagnostic {
31    pub fn error(path: impl Into<PathBuf>, span: Span, message: impl Into<String>) -> Self {
32        Self {
33            severity: Severity::Error,
34            message: message.into(),
35            path: path.into(),
36            span,
37        }
38    }
39
40    pub fn warning(path: impl Into<PathBuf>, span: Span, message: impl Into<String>) -> Self {
41        Self {
42            severity: Severity::Warning,
43            message: message.into(),
44            path: path.into(),
45            span,
46        }
47    }
48}
49
50impl Display for Diagnostic {
51    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
52        write!(
53            formatter,
54            "{}:{}..{}: {}",
55            self.path.display(),
56            self.span.start,
57            self.span.end,
58            self.message
59        )
60    }
61}
62
63impl std::error::Error for Diagnostic {}