gitql_parser/
diagnostic.rs1use crate::token::SourceLocation;
2
3pub 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 #[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 #[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 #[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 pub fn with_location(mut self, location: SourceLocation) -> Self {
55 self.location = Some(location);
56 self
57 }
58
59 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 pub fn add_note(mut self, note: &str) -> Self {
72 self.notes.push(note.to_owned());
73 self
74 }
75
76 pub fn add_help(mut self, help: &str) -> Self {
78 self.helps.push(help.to_owned());
79 self
80 }
81
82 pub fn with_docs(mut self, docs: &str) -> Self {
84 self.docs = Some(docs.to_owned());
85 self
86 }
87
88 pub fn label(&self) -> &String {
90 &self.label
91 }
92
93 pub fn message(&self) -> &String {
95 &self.message
96 }
97
98 pub fn location(&self) -> Option<SourceLocation> {
100 self.location
101 }
102
103 pub fn notes(&self) -> &Vec<String> {
105 &self.notes
106 }
107
108 pub fn helps(&self) -> &Vec<String> {
110 &self.helps
111 }
112
113 pub fn docs(&self) -> &Option<String> {
115 &self.docs
116 }
117
118 pub fn as_boxed(self) -> Box<Self> {
120 Box::new(self)
121 }
122}