44 lines
1.5 KiB
Rust
44 lines
1.5 KiB
Rust
use codespan_reporting::diagnostic::Label;
|
|
use dmc_lib::diagnostic::Diagnostic;
|
|
|
|
pub fn to_csr_diagnostic(
|
|
file_id: usize,
|
|
diagnostic: &Diagnostic,
|
|
) -> codespan_reporting::diagnostic::Diagnostic<usize> {
|
|
let mut primary_label = Label::primary(file_id, diagnostic.start()..diagnostic.end());
|
|
if let Some(primary_label_message) = diagnostic.primary_label_message() {
|
|
primary_label = primary_label.with_message(primary_label_message);
|
|
}
|
|
|
|
let secondary_labels: Vec<Label<usize>> = diagnostic
|
|
.secondary_labels()
|
|
.iter()
|
|
.map(|secondary_label| {
|
|
let mut label =
|
|
Label::secondary(file_id, secondary_label.start()..secondary_label.end());
|
|
if let Some(message) = secondary_label.message() {
|
|
label = label.with_message(message);
|
|
}
|
|
label
|
|
})
|
|
.collect();
|
|
|
|
let mut csr_diagnostic = codespan_reporting::diagnostic::Diagnostic::error()
|
|
.with_message(diagnostic.message())
|
|
.with_label(primary_label)
|
|
.with_labels(secondary_labels);
|
|
|
|
if let Some(error_code) = diagnostic.error_code() {
|
|
csr_diagnostic = csr_diagnostic.with_code(format!("E{:04}", error_code));
|
|
}
|
|
|
|
if let Some((reporter_file, reporter_line)) = diagnostic.reporter() {
|
|
csr_diagnostic = csr_diagnostic.with_note(format!(
|
|
"Reported by (Rust) source: {}, line {}",
|
|
reporter_file, reporter_line
|
|
));
|
|
}
|
|
|
|
csr_diagnostic
|
|
}
|