1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use serde::Serialize;
#[derive(Serialize)]
pub struct Diagnostic<T> {
pub message: DiagnosticMessage<T>,
pub location: DiagnosticLocation,
pub severity: DiagnosticSeverity,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<DiagnosticSource>,
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<DiagnosticCode>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub suggestions: Vec<DiagnosticSuggestion>,
}
impl<T> Diagnostic<T> {
pub fn error(message: DiagnosticMessage<T>, location: DiagnosticLocation) -> Self {
Self {
message,
location,
severity: DiagnosticSeverity::Error,
source: None,
code: None,
suggestions: Vec::new(),
}
}
pub fn warning(message: DiagnosticMessage<T>, location: DiagnosticLocation) -> Self {
Self {
message,
location,
severity: DiagnosticSeverity::Warning,
source: None,
code: None,
suggestions: Vec::new(),
}
}
}
pub struct DiagnosticMessage<T> {
pub target: T,
pub message: String,
}
impl<T> Serialize for DiagnosticMessage<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.message)
}
}
#[derive(Clone, Serialize)]
pub enum MetadataDiagnosticTarget {
Identifier(String, Option<u8>, Option<u8>),
Tag(String),
}
impl MetadataDiagnosticTarget {
pub fn album(album_id: String) -> Self {
Self::Identifier(album_id, None, None)
}
pub fn disc(album_id: String, disc_id: u8) -> Self {
Self::Identifier(album_id, Some(disc_id), None)
}
pub fn track(album_id: String, disc_id: u8, track_id: u8) -> Self {
Self::Identifier(album_id, Some(disc_id), Some(track_id))
}
}
#[derive(Serialize)]
pub struct DiagnosticLocation {
pub path: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub range: Option<DiagnosticRange>,
}
impl DiagnosticLocation {
pub fn simple(path: String) -> DiagnosticLocation {
DiagnosticLocation { path, range: None }
}
}
impl DiagnosticLocation {
pub fn start_line(&self) -> u32 {
self.range.as_ref().map(|r| r.start.line).unwrap_or(0)
}
pub fn start_column(&self) -> Option<u32> {
self.range.as_ref().and_then(|r| r.start.column)
}
pub fn end_line(&self) -> Option<u32> {
self.range
.as_ref()
.and_then(|r| r.end.as_ref().map(|p| p.line))
}
pub fn end_column(&self) -> Option<u32> {
self.range
.as_ref()
.and_then(|r| r.end.as_ref().and_then(|p| p.column))
}
}
#[derive(Serialize, Default)]
pub struct DiagnosticRange {
pub start: DiagnosticPosition,
#[serde(skip_serializing_if = "Option::is_none")]
pub end: Option<DiagnosticPosition>,
}
#[derive(Serialize, Default)]
pub struct DiagnosticPosition {
pub line: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub column: Option<u32>,
}
#[derive(Serialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum DiagnosticSeverity {
Error,
Warning,
#[serde(rename = "INFO")]
Information,
Hint,
}
impl Default for DiagnosticSeverity {
fn default() -> Self {
DiagnosticSeverity::Information
}
}
#[derive(Serialize, Default)]
pub struct DiagnosticSource {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
}
#[derive(Serialize, Default)]
pub struct DiagnosticCode {
pub value: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
}
impl DiagnosticCode {
pub fn new(value: String) -> Self {
Self {
value,
..Default::default()
}
}
}
#[derive(Serialize, Default)]
pub struct DiagnosticSuggestion {
pub range: DiagnosticRange,
pub text: String,
}