Skip to content

Commit

Permalink
fix(report): Show path for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Page committed Nov 23, 2020
1 parent 0ccccab commit 9b0cd5b
Showing 1 changed file with 17 additions and 33 deletions.
50 changes: 17 additions & 33 deletions crates/typos/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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,
}
}
Expand All @@ -35,7 +33,6 @@ impl<'m> Message<'m> {
Message::Typo(_) => false,
Message::File(_) => false,
Message::Parse(_) => false,
Message::PathError(_) => true,
Message::Error(_) => true,
}
}
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -182,35 +183,24 @@ 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 msg: String,
}

impl<'m> Default for PathError<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
msg: "".to_owned(),
}
}
}

#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct Error {
pub struct Error<'m> {
#[serde(flatten)]
pub context: Option<Context<'m>>,
pub msg: String,
}

impl Error {
impl<'m> Error<'m> {
pub fn new(msg: String) -> Self {
Self { msg }
Self { context: None, msg }
}
}

impl Default for Error {
impl<'m> Default for Error<'m> {
fn default() -> Self {
Self { msg: "".to_owned() }
Self {
context: None,
msg: "".to_owned(),
}
}
}

Expand Down Expand Up @@ -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(())
Expand All @@ -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(())
Expand Down

0 comments on commit 9b0cd5b

Please sign in to comment.