Skip to content

Commit

Permalink
Print correct and incorrect MD5 checksums in color
Browse files Browse the repository at this point in the history
type: changed
  • Loading branch information
casey committed Apr 8, 2020
1 parent 1cac9ab commit 3257614
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
46 changes: 22 additions & 24 deletions src/file_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,30 @@ impl From<io::Error> for FileError {
}
}

impl Display for FileError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Io(io_error) => write!(f, "{}", io_error),
Self::Missing => write!(f, "File missing"),
Self::Directory => write!(f, "Expected file but found directory"),
Self::Surfeit(difference) => write!(f, "{} too long", difference),
Self::Dearth(difference) => write!(f, "{} too short", difference),
Self::Md5 { actual, expected } => write!(
f,
"MD5 checksum mismatch: {} (expected {})",
actual, expected
),
}
}
}

impl Print for FileError {
fn print(&self, stream: &mut OutputStream) -> io::Result<()> {
let style = stream.style();
write!(
stream,
"{}{}{}",
style.error().prefix(),
self,
style.error().suffix(),
)

if let Self::Md5 { actual, expected } = self {
write!(
stream,
"MD5 checksum mismatch: {} (expected {})",
style.error().paint(actual.to_string()),
style.good().paint(expected.to_string()),
)?;

return Ok(());
}

match self {
Self::Io(io_error) => write!(stream, "{}", io_error)?,
Self::Missing => write!(stream, "File missing")?,
Self::Directory => write!(stream, "Expected file but found directory")?,
Self::Surfeit(difference) => write!(stream, "{} too long", difference)?,
Self::Dearth(difference) => write!(stream, "{} too short", difference)?,
Self::Md5 { .. } => unreachable!(),
}

Ok(())
}
}
8 changes: 8 additions & 0 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ impl Style {
}
}

pub(crate) fn good(self) -> ansi_term::Style {
if self.active {
ansi_term::Style::new().fg(ansi_term::Color::Green).bold()
} else {
ansi_term::Style::new()
}
}

pub(crate) fn dim(self) -> ansi_term::Style {
if self.active {
ansi_term::Style::new().dimmed()
Expand Down
11 changes: 6 additions & 5 deletions src/subcommand/torrent/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ mod tests {
style.message().prefix(),
path,
style.message().suffix(),
style.error().paint(message)
message,
)
}

Expand All @@ -428,10 +428,11 @@ mod tests {
create_env.resolve("foo").display()
))
),
&error(
"a",
"MD5 checksum mismatch: d16fb36f911f878998c136191af705e (expected \
90150983cd24fb0d6963f7d28e17f72)",
&format!(
"{} MD5 checksum mismatch: {} (expected {})",
style.message().paint("a:"),
style.error().paint("d16fb36f911f878998c136191af705e"),
style.good().paint("90150983cd24fb0d6963f7d28e17f72"),
),
&error("d", "1 byte too long"),
&error("h", "1 byte too short"),
Expand Down

0 comments on commit 3257614

Please sign in to comment.