gitql_parser/
diagnostic.rs

1use crate::token::SourceLocation;
2
3/// In Memory representation for the Diagnostic element
4pub struct Diagnostic {
5    label: String,
6    message: String,
7    location: Option<SourceLocation>,
8    notes: Vec<String>,
9    helps: Vec<String>,
10    docs: Option<String>,
11}
12
13impl Diagnostic {
14    /// Create new instance of Diagnostic with required label and message
15    #[must_use]
16    pub fn new(label: &str, message: &str) -> Self {
17        Diagnostic {
18            label: label.to_owned(),
19            message: message.to_owned(),
20            location: None,
21            notes: vec![],
22            helps: vec![],
23            docs: None,
24        }
25    }
26
27    /// Create new instance of Diagnostic with label `Error`
28    #[must_use]
29    pub fn error(message: &str) -> Self {
30        Diagnostic {
31            label: "Error".to_owned(),
32            message: message.to_owned(),
33            location: None,
34            notes: vec![],
35            helps: vec![],
36            docs: None,
37        }
38    }
39
40    /// Create new instance of Diagnostic with label `Exception`
41    #[must_use]
42    pub fn exception(message: &str) -> Self {
43        Diagnostic {
44            label: "Exception".to_owned(),
45            message: message.to_owned(),
46            location: None,
47            notes: vec![],
48            helps: vec![],
49            docs: None,
50        }
51    }
52
53    /// Set location start and end from Location type
54    pub fn with_location(mut self, location: SourceLocation) -> Self {
55        self.location = Some(location);
56        self
57    }
58
59    /// Set location start and end
60    pub fn with_location_span(mut self, start: u32, end: u32) -> Self {
61        self.location = Some(SourceLocation {
62            line_start: 1,
63            line_end: 1,
64            column_start: start,
65            column_end: end,
66        });
67        self
68    }
69
70    /// Add new note to the current list
71    pub fn add_note(mut self, note: &str) -> Self {
72        self.notes.push(note.to_owned());
73        self
74    }
75
76    /// Add new help to the current list
77    pub fn add_help(mut self, help: &str) -> Self {
78        self.helps.push(help.to_owned());
79        self
80    }
81
82    /// Set Docs url
83    pub fn with_docs(mut self, docs: &str) -> Self {
84        self.docs = Some(docs.to_owned());
85        self
86    }
87
88    /// Return the Diagnostic label
89    pub fn label(&self) -> &String {
90        &self.label
91    }
92
93    /// Return the Diagnostic message
94    pub fn message(&self) -> &String {
95        &self.message
96    }
97
98    /// Return the diagnostic source location span
99    pub fn location(&self) -> Option<SourceLocation> {
100        self.location
101    }
102
103    /// Return the list of notes messages
104    pub fn notes(&self) -> &Vec<String> {
105        &self.notes
106    }
107
108    /// Return the list of helps messages
109    pub fn helps(&self) -> &Vec<String> {
110        &self.helps
111    }
112
113    /// Return the docs url if exists
114    pub fn docs(&self) -> &Option<String> {
115        &self.docs
116    }
117
118    /// Get the Diagnostic as Box::<Diagnostic>
119    pub fn as_boxed(self) -> Box<Self> {
120        Box::new(self)
121    }
122}