Skip to main content

kode_core/
diagnostic.rs

1use crate::{Marker, MarkerSeverity, Position};
2
3/// Severity levels, matching LSP `DiagnosticSeverity` semantics.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum DiagnosticSeverity {
6    Error,
7    Warning,
8    Information,
9    Hint,
10}
11
12/// A single diagnostic finding from a [`DiagnosticProvider`].
13///
14/// Modelled after LSP's `Diagnostic` but simplified — no URI (the editor
15/// owns the document), no code/tags (can be added later).
16#[derive(Debug, Clone)]
17pub struct Diagnostic {
18    pub start: Position,
19    pub end: Position,
20    pub severity: DiagnosticSeverity,
21    pub message: String,
22    /// Identifies the provider that produced this diagnostic
23    /// (e.g. `"bigquery"`, `"jsonschema"`, `"python"`).
24    pub source: Option<String>,
25}
26
27impl Diagnostic {
28    pub fn error(start: Position, end: Position, message: impl Into<String>) -> Self {
29        Self {
30            start,
31            end,
32            severity: DiagnosticSeverity::Error,
33            message: message.into(),
34            source: None,
35        }
36    }
37
38    pub fn warning(start: Position, end: Position, message: impl Into<String>) -> Self {
39        Self {
40            start,
41            end,
42            severity: DiagnosticSeverity::Warning,
43            message: message.into(),
44            source: None,
45        }
46    }
47
48    pub fn with_source(mut self, source: impl Into<String>) -> Self {
49        self.source = Some(source.into());
50        self
51    }
52}
53
54impl From<DiagnosticSeverity> for MarkerSeverity {
55    fn from(s: DiagnosticSeverity) -> Self {
56        match s {
57            DiagnosticSeverity::Error => MarkerSeverity::Error,
58            DiagnosticSeverity::Warning => MarkerSeverity::Warning,
59            DiagnosticSeverity::Information => MarkerSeverity::Info,
60            DiagnosticSeverity::Hint => MarkerSeverity::Hint,
61        }
62    }
63}
64
65impl From<Diagnostic> for Marker {
66    fn from(d: Diagnostic) -> Self {
67        Marker {
68            start: d.start,
69            end: d.end,
70            message: d.message,
71            severity: d.severity.into(),
72        }
73    }
74}