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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use std::{
borrow::Cow,
error::Error,
fmt::{Debug, Display},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{Diagnostic, LabeledSpan, Severity};
/// Diagnostic that can be created at runtime.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MietteDiagnostic {
/// Displayed diagnostic message
pub message: String,
/// Unique diagnostic code to look up more information
/// about this Diagnostic. Ideally also globally unique, and documented
/// in the toplevel crate's documentation for easy searching.
/// Rust path format (`foo::bar::baz`) is recommended, but more classic
/// codes like `E0123` will work just fine
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub code: Option<String>,
/// [`Diagnostic`] severity. Intended to be used by
/// [`ReportHandler`](crate::ReportHandler)s to change the way different
/// [`Diagnostic`]s are displayed. Defaults to [`Severity::Error`]
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub severity: Option<Severity>,
/// Additional help text related to this Diagnostic
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub help: Option<String>,
/// Additional note text related to this Diagnostic
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub note: Option<String>,
/// URL to visit for a more detailed explanation/help about this
/// [`Diagnostic`].
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub url: Option<String>,
/// Labels to apply to this `Diagnostic`'s [`Diagnostic::source_code`]
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Labels::is_empty"))]
pub labels: Labels,
}
/// Container for a [`MietteDiagnostic`]'s labels.
///
/// Most diagnostics carry only one or two labels, so those cases are stored
/// inline without a heap allocation. Diagnostics with three or more labels spill
/// to a [`Vec`].
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Labels {
/// No labels.
#[default]
None,
/// A single label, stored inline.
One([LabeledSpan; 1]),
/// Two labels, stored inline.
Two([LabeledSpan; 2]),
/// Three or more labels, stored on the heap.
Many(Vec<LabeledSpan>),
}
impl Labels {
/// Returns the labels as a contiguous slice.
#[must_use]
pub fn as_slice(&self) -> &[LabeledSpan] {
match self {
Labels::None => &[],
Labels::One(labels) => labels,
Labels::Two(labels) => labels,
Labels::Many(labels) => labels,
}
}
/// Returns the labels as a mutable contiguous slice.
#[must_use]
pub fn as_mut_slice(&mut self) -> &mut [LabeledSpan] {
match self {
Labels::None => &mut [],
Labels::One(labels) => labels,
Labels::Two(labels) => labels,
Labels::Many(labels) => labels,
}
}
/// Returns `true` if there are no labels.
#[must_use]
pub fn is_empty(&self) -> bool {
matches!(self, Labels::None)
}
/// Returns the number of labels.
#[must_use]
pub fn len(&self) -> usize {
self.as_slice().len()
}
/// Appends a label, keeping the storage inline while possible.
pub fn push(&mut self, label: LabeledSpan) {
// Fast path: already on the heap, push in place without moving the `Vec`.
if let Labels::Many(labels) = self {
labels.push(label);
return;
}
*self = match std::mem::take(self) {
Labels::None => Labels::One([label]),
Labels::One([a]) => Labels::Two([a, label]),
Labels::Two([a, b]) => Labels::Many(vec![a, b, label]),
Labels::Many(_) => unreachable!("handled by the fast path above"),
};
}
}
impl std::ops::Deref for Labels {
type Target = [LabeledSpan];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
impl std::ops::DerefMut for Labels {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}
impl<'a> IntoIterator for &'a Labels {
type Item = &'a LabeledSpan;
type IntoIter = std::slice::Iter<'a, LabeledSpan>;
fn into_iter(self) -> Self::IntoIter {
self.as_slice().iter()
}
}
impl<'a> IntoIterator for &'a mut Labels {
type Item = &'a mut LabeledSpan;
type IntoIter = std::slice::IterMut<'a, LabeledSpan>;
fn into_iter(self) -> Self::IntoIter {
self.as_mut_slice().iter_mut()
}
}
impl Extend<LabeledSpan> for Labels {
fn extend<I: IntoIterator<Item = LabeledSpan>>(&mut self, iter: I) {
let mut iter = iter.into_iter();
// Fill the inline tiers first — allocation-free while staying at 1-2.
while !matches!(self, Labels::Many(_)) {
match iter.next() {
Some(label) => self.push(label),
None => return,
}
}
// Once on the heap, reserve once and bulk-extend instead of re-growing
// the `Vec` on every element.
if let Labels::Many(labels) = self {
labels.reserve(iter.size_hint().0);
labels.extend(iter);
}
}
}
impl FromIterator<LabeledSpan> for Labels {
fn from_iter<I: IntoIterator<Item = LabeledSpan>>(iter: I) -> Self {
let mut iter = iter.into_iter();
// If the iterator already reports more than two elements, it will spill
// to the heap regardless, so collect straight into a `Vec`. For a
// `vec::IntoIter` source `collect` reuses the original allocation, so
// `with_labels(vec)` does not allocate at all.
if iter.size_hint().0 > 2 {
return Labels::Many(iter.collect());
}
// Otherwise pull up to three elements to pick the smallest variant
// that fits without allocating for the common one/two-label cases.
let Some(a) = iter.next() else { return Labels::None };
let Some(b) = iter.next() else { return Labels::One([a]) };
let Some(c) = iter.next() else { return Labels::Two([a, b]) };
let mut labels = Vec::with_capacity(3 + iter.size_hint().0);
labels.extend([a, b, c]);
labels.extend(iter);
Labels::Many(labels)
}
}
impl From<Vec<LabeledSpan>> for Labels {
fn from(labels: Vec<LabeledSpan>) -> Self {
if labels.len() <= 2 { labels.into_iter().collect() } else { Labels::Many(labels) }
}
}
impl From<LabeledSpan> for Labels {
fn from(label: LabeledSpan) -> Self {
Labels::One([label])
}
}
impl From<[LabeledSpan; 1]> for Labels {
fn from(labels: [LabeledSpan; 1]) -> Self {
Labels::One(labels)
}
}
impl From<[LabeledSpan; 2]> for Labels {
fn from(labels: [LabeledSpan; 2]) -> Self {
Labels::Two(labels)
}
}
#[cfg(feature = "serde")]
impl Serialize for Labels {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_slice().serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Labels {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
// Accept both a sequence and `null` (the latter mirrors the previous
// `Option<Vec<LabeledSpan>>` representation).
let labels = Option::<Vec<LabeledSpan>>::deserialize(deserializer)?;
Ok(labels.map_or(Labels::None, Labels::from))
}
}
impl Display for MietteDiagnostic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.message)
}
}
impl Error for MietteDiagnostic {}
impl Diagnostic for MietteDiagnostic {
fn code(&self) -> Option<Cow<'_, str>> {
self.code.as_deref().map(Cow::Borrowed)
}
fn severity(&self) -> Option<Severity> {
self.severity
}
fn help(&self) -> Option<Cow<'_, str>> {
self.help.as_deref().map(Cow::Borrowed)
}
fn note(&self) -> Option<Cow<'_, str>> {
self.note.as_deref().map(Cow::Borrowed)
}
fn url(&self) -> Option<Cow<'_, str>> {
self.url.as_deref().map(Cow::Borrowed)
}
fn labels(&self) -> Labels {
self.labels.clone()
}
}
impl MietteDiagnostic {
/// Create a new dynamic diagnostic with the given message.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, MietteDiagnostic, Severity};
///
/// let diag = MietteDiagnostic::new("Oops, something went wrong!");
/// assert_eq!(diag.to_string(), "Oops, something went wrong!");
/// assert_eq!(diag.message, "Oops, something went wrong!");
/// ```
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
labels: Labels::None,
severity: None,
code: None,
help: None,
note: None,
url: None,
}
}
/// Return new diagnostic with the given code.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, MietteDiagnostic};
///
/// let diag = MietteDiagnostic::new("Oops, something went wrong!").with_code("foo::bar::baz");
/// assert_eq!(diag.message, "Oops, something went wrong!");
/// assert_eq!(diag.code, Some("foo::bar::baz".to_string()));
/// ```
#[must_use]
pub fn with_code(mut self, code: impl Into<String>) -> Self {
self.code = Some(code.into());
self
}
/// Return new diagnostic with the given severity.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, MietteDiagnostic, Severity};
///
/// let diag = MietteDiagnostic::new("I warn you to stop!").with_severity(Severity::Warning);
/// assert_eq!(diag.message, "I warn you to stop!");
/// assert_eq!(diag.severity, Some(Severity::Warning));
/// ```
#[must_use]
pub fn with_severity(mut self, severity: Severity) -> Self {
self.severity = Some(severity);
self
}
/// Return new diagnostic with the given help message.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, MietteDiagnostic};
///
/// let diag = MietteDiagnostic::new("PC is not working").with_help("Try to reboot it again");
/// assert_eq!(diag.message, "PC is not working");
/// assert_eq!(diag.help, Some("Try to reboot it again".to_string()));
/// ```
#[must_use]
pub fn with_help(mut self, help: impl Into<String>) -> Self {
self.help = Some(help.into());
self
}
/// Return new diagnostic with the given note.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, MietteDiagnostic};
///
/// let diag = MietteDiagnostic::new("Something went wrong")
/// .with_note("This is additional context");
/// assert_eq!(diag.note, Some("This is additional context".to_string()));
/// assert_eq!(diag.message, "Something went wrong");
/// ```
#[must_use]
pub fn with_note(mut self, note: impl Into<String>) -> Self {
self.note = Some(note.into());
self
}
/// Return new diagnostic with the given URL.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, MietteDiagnostic};
///
/// let diag = MietteDiagnostic::new("PC is not working")
/// .with_url("https://letmegooglethat.com/?q=Why+my+pc+doesn%27t+work");
/// assert_eq!(diag.message, "PC is not working");
/// assert_eq!(
/// diag.url,
/// Some("https://letmegooglethat.com/?q=Why+my+pc+doesn%27t+work".to_string())
/// );
/// ```
#[must_use]
pub fn with_url(mut self, url: impl Into<String>) -> Self {
self.url = Some(url.into());
self
}
/// Return new diagnostic with the given label.
///
/// Discards previous labels
///
/// # Examples
/// ```
/// use miette::{Diagnostic, LabeledSpan, MietteDiagnostic};
///
/// let source = "cpp is the best language";
///
/// let label = LabeledSpan::at(0..3, "This should be Rust");
/// let diag = MietteDiagnostic::new("Wrong best language").with_label(label.clone());
/// assert_eq!(diag.message, "Wrong best language");
/// assert_eq!(diag.labels.as_slice(), &[label]);
/// ```
#[must_use]
pub fn with_label(mut self, label: impl Into<LabeledSpan>) -> Self {
self.labels = Labels::One([label.into()]);
self
}
/// Return new diagnostic with the given labels.
///
/// Discards previous labels
///
/// # Examples
/// ```
/// use miette::{Diagnostic, LabeledSpan, MietteDiagnostic};
///
/// let source = "hello wrld";
///
/// let labels = vec![
/// LabeledSpan::at_offset(3, "add 'l'"),
/// LabeledSpan::at_offset(6, "add 'r'"),
/// ];
/// let diag = MietteDiagnostic::new("Typos in 'hello world'").with_labels(labels.clone());
/// assert_eq!(diag.message, "Typos in 'hello world'");
/// assert_eq!(diag.labels.as_slice(), labels.as_slice());
/// ```
#[must_use]
pub fn with_labels(mut self, labels: impl IntoIterator<Item = LabeledSpan>) -> Self {
self.labels = labels.into_iter().collect();
self
}
/// Return new diagnostic with new label added to the existing ones.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, LabeledSpan, MietteDiagnostic};
///
/// let source = "hello wrld";
///
/// let label1 = LabeledSpan::at_offset(3, "add 'l'");
/// let label2 = LabeledSpan::at_offset(6, "add 'r'");
/// let diag = MietteDiagnostic::new("Typos in 'hello world'")
/// .and_label(label1.clone())
/// .and_label(label2.clone());
/// assert_eq!(diag.message, "Typos in 'hello world'");
/// assert_eq!(diag.labels.as_slice(), &[label1, label2]);
/// ```
#[must_use]
pub fn and_label(mut self, label: impl Into<LabeledSpan>) -> Self {
self.labels.push(label.into());
self
}
/// Return new diagnostic with new labels added to the existing ones.
///
/// # Examples
/// ```
/// use miette::{Diagnostic, LabeledSpan, MietteDiagnostic};
///
/// let source = "hello wrld";
///
/// let label1 = LabeledSpan::at_offset(3, "add 'l'");
/// let label2 = LabeledSpan::at_offset(6, "add 'r'");
/// let label3 = LabeledSpan::at_offset(9, "add '!'");
/// let diag = MietteDiagnostic::new("Typos in 'hello world!'")
/// .and_label(label1.clone())
/// .and_labels([label2.clone(), label3.clone()]);
/// assert_eq!(diag.message, "Typos in 'hello world!'");
/// assert_eq!(diag.labels.as_slice(), &[label1, label2, label3]);
/// ```
#[must_use]
pub fn and_labels(mut self, labels: impl IntoIterator<Item = LabeledSpan>) -> Self {
self.labels.extend(labels);
self
}
}
#[cfg(feature = "serde")]
#[test]
fn test_serialize_miette_diagnostic() {
use serde_json::json;
use crate::diagnostic;
let diag = diagnostic!("message");
let json = json!({ "message": "message" });
assert_eq!(json!(diag), json);
let diag = diagnostic!(
code = "code",
help = "help",
url = "url",
labels = [LabeledSpan::at_offset(0, "label1"), LabeledSpan::at(1..3, "label2")],
severity = Severity::Warning,
"message"
);
let json = json!({
"message": "message",
"code": "code",
"help": "help",
"url": "url",
"severity": "Warning",
"labels": [
{
"span": {
"offset": 0,
"length": 0
},
"label": "label1",
"primary": false
},
{
"span": {
"offset": 1,
"length": 2
},
"label": "label2",
"primary": false
}
]
});
assert_eq!(json!(diag), json);
}
#[cfg(feature = "serde")]
#[test]
fn test_deserialize_miette_diagnostic() {
use serde_json::json;
use crate::diagnostic;
let json = json!({ "message": "message" });
let diag = diagnostic!("message");
assert_eq!(diag, serde_json::from_value(json).unwrap());
let json = json!({
"message": "message",
"help": null,
"code": null,
"severity": null,
"url": null,
"labels": null
});
assert_eq!(diag, serde_json::from_value(json).unwrap());
let diag = diagnostic!(
code = "code",
help = "help",
url = "url",
labels = [LabeledSpan::at_offset(0, "label1"), LabeledSpan::at(1..3, "label2")],
severity = Severity::Warning,
"message"
);
let json = json!({
"message": "message",
"code": "code",
"help": "help",
"url": "url",
"severity": "Warning",
"labels": [
{
"span": {
"offset": 0,
"length": 0
},
"label": "label1",
"primary": false
},
{
"span": {
"offset": 1,
"length": 2
},
"label": "label2",
"primary": false
}
]
});
assert_eq!(diag, serde_json::from_value(json).unwrap());
}