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