Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(report): Show path for errors #176

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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