Make error formatting more generic
Now, some errors are generic over the FileId type, meaning I can now pretty-print command line argument errors as well as normal errors with the same machinery. This is accomplished via the Eprint trait, whose generics are a tad complex but probably still fine.
This commit is contained in:
parent
8ae691bc3c
commit
5e47dddd4c
6 changed files with 111 additions and 59 deletions
28
src/error.rs
Normal file
28
src/error.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use codespan_reporting::diagnostic::Diagnostic;
|
||||
use codespan_reporting::files::Files;
|
||||
use codespan_reporting::term::{self, Config};
|
||||
use termcolor::StandardStream;
|
||||
|
||||
pub trait Eprint<'a, F: Files<'a>> {
|
||||
fn eprint_diagnostic<'f: 'a>(
|
||||
files: &'f F,
|
||||
config: &Config,
|
||||
diagnostic: &Diagnostic<F::FileId>,
|
||||
) {
|
||||
let mut out = StandardStream::stderr(termcolor::ColorChoice::Auto);
|
||||
if let Err(e) = term::emit(&mut out, config, files, diagnostic) {
|
||||
panic!("Error while reporting error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
fn eprint<'f: 'a>(&self, files: &'f F, config: &Config);
|
||||
}
|
||||
|
||||
pub fn eprint_error<'a, 'f: 'a, F, E>(files: &'f F, e: &E)
|
||||
where
|
||||
F: Files<'a>,
|
||||
E: Eprint<'a, F>,
|
||||
{
|
||||
let config = Config::default();
|
||||
e.eprint(files, &config);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue