Skip to content

Commit 3497508

Browse files
authoredFeb 9, 2023
fix(graphical): Fix wrong severity of related errors (#234)
1 parent b658fc0 commit 3497508

File tree

4 files changed

+131
-5
lines changed

4 files changed

+131
-5
lines changed
 

‎src/handlers/graphical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl GraphicalReportHandler {
281281
if let Some(related) = diagnostic.related() {
282282
writeln!(f)?;
283283
for rel in related {
284-
match diagnostic.severity() {
284+
match rel.severity() {
285285
Some(Severity::Error) | None => write!(f, "Error: ")?,
286286
Some(Severity::Warning) => write!(f, "Warning: ")?,
287287
Some(Severity::Advice) => write!(f, "Advice: ")?,

‎src/handlers/narratable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl NarratableReportHandler {
132132
if let Some(related) = diagnostic.related() {
133133
writeln!(f)?;
134134
for rel in related {
135-
match diagnostic.severity() {
135+
match rel.severity() {
136136
Some(Severity::Error) | None => write!(f, "Error: ")?,
137137
Some(Severity::Warning) => write!(f, "Warning: ")?,
138138
Some(Severity::Advice) => write!(f, "Advice: ")?,
@@ -218,10 +218,10 @@ impl NarratableReportHandler {
218218
Ok(())
219219
}
220220

221-
fn render_context<'a>(
221+
fn render_context(
222222
&self,
223223
f: &mut impl fmt::Write,
224-
source: &'a dyn SourceCode,
224+
source: &dyn SourceCode,
225225
context: &LabeledSpan,
226226
labels: &[LabeledSpan],
227227
) -> fmt::Result {

‎src/protocol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl SourceOffset {
488488
Ok((
489489
loc.file().into(),
490490
fs::read_to_string(loc.file())
491-
.map(|txt| Self::from_location(&txt, loc.line() as usize, loc.column() as usize))?,
491+
.map(|txt| Self::from_location(txt, loc.line() as usize, loc.column() as usize))?,
492492
))
493493
}
494494
}

‎tests/graphical.rs

+126
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,132 @@ Error: oops::my::bad
10571057
Ok(())
10581058
}
10591059

1060+
#[test]
1061+
fn related_severity() -> Result<(), MietteError> {
1062+
#[derive(Debug, Diagnostic, Error)]
1063+
#[error("oops!")]
1064+
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
1065+
struct MyBad {
1066+
#[source_code]
1067+
src: NamedSource,
1068+
#[label("this bit here")]
1069+
highlight: SourceSpan,
1070+
#[related]
1071+
related: Vec<MyRelated>,
1072+
}
1073+
1074+
#[derive(Debug, Diagnostic, Error)]
1075+
enum MyRelated {
1076+
#[error("oops!")]
1077+
#[diagnostic(
1078+
severity(Error),
1079+
code(oops::my::related::error),
1080+
help("try doing it better next time?")
1081+
)]
1082+
Error {
1083+
#[source_code]
1084+
src: NamedSource,
1085+
#[label("this bit here")]
1086+
highlight: SourceSpan,
1087+
},
1088+
1089+
#[error("oops!")]
1090+
#[diagnostic(
1091+
severity(Warning),
1092+
code(oops::my::related::warning),
1093+
help("try doing it better next time?")
1094+
)]
1095+
Warning {
1096+
#[source_code]
1097+
src: NamedSource,
1098+
#[label("this bit here")]
1099+
highlight: SourceSpan,
1100+
},
1101+
1102+
#[error("oops!")]
1103+
#[diagnostic(
1104+
severity(Advice),
1105+
code(oops::my::related::advice),
1106+
help("try doing it better next time?")
1107+
)]
1108+
Advice {
1109+
#[source_code]
1110+
src: NamedSource,
1111+
#[label("this bit here")]
1112+
highlight: SourceSpan,
1113+
},
1114+
}
1115+
1116+
let src = "source\n text\n here".to_string();
1117+
let err = MyBad {
1118+
src: NamedSource::new("bad_file.rs", src.clone()),
1119+
highlight: (9, 4).into(),
1120+
related: vec![
1121+
MyRelated::Error {
1122+
src: NamedSource::new("bad_file.rs", src.clone()),
1123+
highlight: (0, 6).into(),
1124+
},
1125+
MyRelated::Warning {
1126+
src: NamedSource::new("bad_file.rs", src.clone()),
1127+
highlight: (0, 6).into(),
1128+
},
1129+
MyRelated::Advice {
1130+
src: NamedSource::new("bad_file.rs", src),
1131+
highlight: (0, 6).into(),
1132+
},
1133+
],
1134+
};
1135+
let out = fmt_report(err.into());
1136+
println!("Error: {}", out);
1137+
let expected = r#"oops::my::bad
1138+
1139+
× oops!
1140+
╭─[bad_file.rs:1:1]
1141+
1 │ source
1142+
2 │ text
1143+
· ──┬─
1144+
· ╰── this bit here
1145+
3 │ here
1146+
╰────
1147+
help: try doing it better next time?
1148+
1149+
Error: oops::my::related::error
1150+
1151+
× oops!
1152+
╭─[bad_file.rs:1:1]
1153+
1 │ source
1154+
· ───┬──
1155+
· ╰── this bit here
1156+
2 │ text
1157+
╰────
1158+
help: try doing it better next time?
1159+
Warning: oops::my::related::warning
1160+
1161+
⚠ oops!
1162+
╭─[bad_file.rs:1:1]
1163+
1 │ source
1164+
· ───┬──
1165+
· ╰── this bit here
1166+
2 │ text
1167+
╰────
1168+
help: try doing it better next time?
1169+
Advice: oops::my::related::advice
1170+
1171+
☞ oops!
1172+
╭─[bad_file.rs:1:1]
1173+
1 │ source
1174+
· ───┬──
1175+
· ╰── this bit here
1176+
2 │ text
1177+
╰────
1178+
help: try doing it better next time?
1179+
"#
1180+
.trim_start()
1181+
.to_string();
1182+
assert_eq!(expected, out);
1183+
Ok(())
1184+
}
1185+
10601186
#[test]
10611187
fn zero_length_eol_span() {
10621188
#[derive(Error, Debug, Diagnostic)]

0 commit comments

Comments
 (0)
Please sign in to comment.