Print correct and incorrect MD5 checksums in color

type: changed
This commit is contained in:
Casey Rodarmor 2020-03-16 01:07:29 -07:00
parent 1cac9ab924
commit 3257614c4f
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
3 changed files with 36 additions and 29 deletions

View File

@ -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 { impl Print for FileError {
fn print(&self, stream: &mut OutputStream) -> io::Result<()> { fn print(&self, stream: &mut OutputStream) -> io::Result<()> {
let style = stream.style(); let style = stream.style();
write!(
stream, if let Self::Md5 { actual, expected } = self {
"{}{}{}", write!(
style.error().prefix(), stream,
self, "MD5 checksum mismatch: {} (expected {})",
style.error().suffix(), 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(())
} }
} }

View File

@ -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 { pub(crate) fn dim(self) -> ansi_term::Style {
if self.active { if self.active {
ansi_term::Style::new().dimmed() ansi_term::Style::new().dimmed()

View File

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