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