forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.rs
More file actions
31 lines (26 loc) · 1.23 KB
/
Copy pathdiagnostics.rs
File metadata and controls
31 lines (26 loc) · 1.23 KB
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
use crate::files::{FileStore, SourceFileId};
pub use codespan_reporting::diagnostic::{Diagnostic as CsDiagnostic, Label, LabelStyle, Severity};
use codespan_reporting::term;
use term::termcolor::{BufferWriter, ColorChoice};
pub type Diagnostic = CsDiagnostic<SourceFileId>;
/// Print the given diagnostics to stderr.
pub fn print_diagnostics(diagnostics: &[Diagnostic], files: &FileStore) {
let writer = BufferWriter::stderr(ColorChoice::Auto);
let mut buffer = writer.buffer();
let config = term::Config::default();
for diag in diagnostics {
term::emit(&mut buffer, &config, files, &diag).unwrap();
}
// If we use `writer` here, the output won't be captured by rust's test system.
eprintln!("{}", std::str::from_utf8(buffer.as_slice()).unwrap());
}
/// Format the given diagnostics as a string.
pub fn diagnostics_string(diagnostics: &[Diagnostic], files: &FileStore) -> String {
let writer = BufferWriter::stderr(ColorChoice::Never);
let mut buffer = writer.buffer();
let config = term::Config::default();
for diag in diagnostics {
term::emit(&mut buffer, &config, files, &diag).expect("failed to emit diagnostic");
}
std::str::from_utf8(buffer.as_slice()).unwrap().to_string()
}