sqruff_lib/cli/
json_types.rs

1use std::collections::BTreeMap;
2
3use serde::Serialize;
4use sqruff_lib_core::errors::SQLBaseError;
5
6impl From<SQLBaseError> for Diagnostic {
7    fn from(value: SQLBaseError) -> Self {
8        let code = value.rule.map(|rule| rule.code.to_string());
9        Diagnostic {
10            range: Range {
11                start: Position::new(value.line_no as u32, value.line_pos as u32),
12                end: Position::new(value.line_no as u32, value.line_pos as u32),
13            },
14            message: value.description,
15            severity: DiagnosticSeverity::Warning,
16            source: Some("sqruff".to_string()),
17            code,
18            // code: todo!(),
19            // source: Some(value.get_source().to_string()),
20            // code: Some(DiagnosticCode {
21            //     value: value.rule_code().to_string(),
22            //     target: Uri::new("".to_string()),
23            // }),
24            // related_information: Vec::new(),
25            // tags: Vec::new(),
26        }
27    }
28}
29
30/// Represents a line and character position, such as the position of the cursor.
31#[derive(Serialize)]
32struct Position {
33    /// The zero-based line value.
34    line: u32,
35    /// The zero-based character value.
36    character: u32,
37}
38
39impl Position {
40    /// Creates a new `Position` instance.
41    fn new(line: u32, character: u32) -> Self {
42        Self { line, character }
43    }
44}
45
46/// A range represents an ordered pair of two positions. It is guaranteed that `start` is before or equal to `end`.
47#[derive(Serialize)]
48struct Range {
49    /// The start position. It is before or equal to `end`.
50    start: Position,
51    /// The end position. It is after or equal to `start`.
52    end: Position,
53}
54
55/// Represents a diagnostic, such as a compiler error or warning. Diagnostic objects are only valid in the scope of a file.
56#[derive(Serialize)]
57pub struct Diagnostic {
58    /// The range to which this diagnostic applies.
59    range: Range,
60    /// The human-readable message.
61    message: String,
62    /// The severity, default is {@link DiagnosticSeverity::Error error}.
63    pub severity: DiagnosticSeverity,
64    /// A human-readable string describing the source of this diagnostic, e.g. 'typescript' or 'super lint'.
65    source: Option<String>,
66    // The diagnostic's code, which might appear in the user interface.
67    code: Option<String>,
68    // An optional property to describe the error code.
69    // code_description: Option<CodeDescription>,
70    // TODO Maybe implement
71    // An array of related diagnostic information, e.g. when symbol-names within a scope collide all definitions can be marked via this property.
72    // related_information: Vec<DiagnosticRelatedInformation>,
73    // Additional metadata about the diagnostic.
74    // tags: Vec<DiagnosticTag>,
75}
76
77// Structure to capture a description for an error code.
78// #[derive(Serialize)]
79// pub struct CodeDescription {
80//     /// An URI to open with more information about the diagnostic error.
81//     href: String,
82// }
83
84// /// Represents a related message and source code location for a diagnostic. This should be used to point to code locations that cause or are related to a diagnostics, e.g when duplicating a symbol in a scope.
85// #[derive(Serialize)]
86// struct DiagnosticCode {
87//     /// A code or identifier for this diagnostic.
88//     value: String,
89//     // TODO Maybe implement
90//     // A target URI to open with more information about the diagnostic error.
91//     // target: Uri,
92// }
93
94/// Represents the severity of diagnostics.
95#[derive(Serialize)]
96pub enum DiagnosticSeverity {
97    /// Something not allowed by the rules of a language or other means.
98    Error = 0,
99    /// Something suspicious but allowed.
100    Warning = 1,
101    /// Something to inform about but not a problem.
102    Information = 2,
103    /// Something to hint to a better way of doing it, like proposing a refactoring.
104    Hint = 3,
105}
106
107pub type DiagnosticCollection = BTreeMap<String, Vec<Diagnostic>>;