Merge pull request #176 from epage/file-error

fix(report): Show path for errors
This commit is contained in:
Ed Page 2020-11-23 12:14:45 -06:00 committed by GitHub
commit e3791065b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,8 +13,7 @@ pub enum Message<'m> {
Typo(Typo<'m>),
File(File<'m>),
Parse(Parse<'m>),
PathError(PathError<'m>),
Error(Error),
Error(Error<'m>),
}
impl<'m> Message<'m> {
@ -24,7 +23,6 @@ impl<'m> Message<'m> {
Message::Typo(c) => c.corrections.is_correction(),
Message::File(_) => false,
Message::Parse(_) => false,
Message::PathError(_) => false,
Message::Error(_) => false,
}
}
@ -35,7 +33,6 @@ impl<'m> Message<'m> {
Message::Typo(_) => false,
Message::File(_) => false,
Message::Parse(_) => false,
Message::PathError(_) => true,
Message::Error(_) => true,
}
}
@ -50,6 +47,10 @@ impl<'m> Message<'m> {
let parse = parse.context(context);
Message::Parse(parse)
}
Message::Error(error) => {
let error = error.context(context);
Message::Error(error)
}
_ => self,
}
}
@ -182,38 +183,27 @@ impl<'m> Default for Parse<'m> {
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct PathError<'m> {
pub path: &'m std::path::Path,
pub struct Error<'m> {
#[serde(flatten)]
pub context: Option<Context<'m>>,
pub msg: String,
}
impl<'m> Default for PathError<'m> {
impl<'m> Error<'m> {
pub fn new(msg: String) -> Self {
Self { context: None, msg }
}
}
impl<'m> Default for Error<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
context: None,
msg: "".to_owned(),
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct Error {
pub msg: String,
}
impl Error {
pub fn new(msg: String) -> Self {
Self { msg }
}
}
impl Default for Error {
fn default() -> Self {
Self { msg: "".to_owned() }
}
}
pub trait Report: Send + Sync {
fn report(&self, msg: Message) -> Result<(), std::io::Error>;
}
@ -277,11 +267,8 @@ impl Report for PrintBrief {
Message::Parse(msg) => {
writeln!(io::stdout(), "{}", itertools::join(msg.data.iter(), " "))?;
}
Message::PathError(msg) => {
log::error!("{}: {}", msg.path.display(), msg.msg);
}
Message::Error(msg) => {
log::error!("{}", msg.msg);
log::error!("{}: {}", context_display(&msg.context), msg.msg);
}
}
Ok(())
@ -304,11 +291,8 @@ impl Report for PrintLong {
Message::Parse(msg) => {
writeln!(io::stdout(), "{}", itertools::join(msg.data.iter(), " "))?;
}
Message::PathError(msg) => {
log::error!("{}: {}", msg.path.display(), msg.msg);
}
Message::Error(msg) => {
log::error!("{}", msg.msg);
log::error!("{}: {}", context_display(&msg.context), msg.msg);
}
}
Ok(())