1//! Rustc Diagnostic JSON Output
2//!
3//! The following data types are copied from [rust-lang/rust](https://github.com/rust-lang/rust/blob/de78655bca47cac8e783dbb563e7e5c25c1fae40/src/libsyntax/json.rs)
4
5use serde::Deserialize;
6
7#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
8pub struct Diagnostic {
9 /// The primary error message.
10 pub message: String,
11 pub code: Option<DiagnosticCode>,
12 /// "error: internal compiler error", "error", "warning", "note", "help".
13 level: String,
14 pub spans: Vec<DiagnosticSpan>,
15 /// Associated diagnostic messages.
16 pub children: Vec<Diagnostic>,
17 /// The message as rustc would render it. Currently this is only
18 /// `Some` for "suggestions", but eventually it will include all
19 /// snippets.
20 pub rendered: Option<String>,
21}
22
23#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
24pub struct DiagnosticSpan {
25 pub file_name: String,
26 pub byte_start: u32,
27 pub byte_end: u32,
28 /// 1-based.
29 pub line_start: usize,
30 pub line_end: usize,
31 /// 1-based, character offset.
32 pub column_start: usize,
33 pub column_end: usize,
34 /// Is this a "primary" span -- meaning the point, or one of the points,
35 /// where the error occurred?
36 pub is_primary: bool,
37 /// Source text from the start of line_start to the end of line_end.
38 pub text: Vec<DiagnosticSpanLine>,
39 /// Label that should be placed at this location (if any)
40 label: Option<String>,
41 /// If we are suggesting a replacement, this will contain text
42 /// that should be sliced in atop this span. You may prefer to
43 /// load the fully rendered version from the parent `Diagnostic`,
44 /// however.
45 pub suggested_replacement: Option<String>,
46 pub suggestion_applicability: Option<Applicability>,
47 /// Macro invocations that created the code at this span, if any.
48 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
49}
50
51#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
52pub enum Applicability {
53 MachineApplicable,
54 HasPlaceholders,
55 MaybeIncorrect,
56 Unspecified,
57}
58
59#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
60pub struct DiagnosticSpanLine {
61 pub text: String,
62
63 /// 1-based, character offset in self.text.
64 pub highlight_start: usize,
65
66 pub highlight_end: usize,
67}
68
69#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
70struct DiagnosticSpanMacroExpansion {
71 /// span where macro was applied to generate this code; note that
72 /// this may itself derive from a macro (if
73 /// `span.expansion.is_some()`)
74 span: DiagnosticSpan,
75
76 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
77 macro_decl_name: String,
78
79 /// span where macro was defined (if known)
80 def_site_span: Option<DiagnosticSpan>,
81}
82
83#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
84pub struct DiagnosticCode {
85 /// The code itself.
86 pub code: String,
87 /// An explanation for the code.
88 explanation: Option<String>,
89}
90