Print correct and incorrect MD5 checksums in color
type: changed
This commit is contained in:
parent
1cac9ab924
commit
3257614c4f
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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"),
|
||||
|
|
Loading…
Reference in New Issue
Block a user