diff --git a/src/file_error.rs b/src/file_error.rs index b28c7fa..32116fb 100644 --- a/src/file_error.rs +++ b/src/file_error.rs @@ -67,32 +67,30 @@ impl From 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(()) } } diff --git a/src/style.rs b/src/style.rs index c167959..01f05a1 100644 --- a/src/style.rs +++ b/src/style.rs @@ -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() diff --git a/src/subcommand/torrent/verify.rs b/src/subcommand/torrent/verify.rs index 936fdc6..24e5304 100644 --- a/src/subcommand/torrent/verify.rs +++ b/src/subcommand/torrent/verify.rs @@ -408,7 +408,7 @@ mod tests { style.message().prefix(), path, style.message().suffix(), - style.error().paint(message) + message, ) } @@ -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"),