Skip to content

Commit

Permalink
error: implement Display
Browse files Browse the repository at this point in the history
Useful for printing out error messages to users.
  • Loading branch information
cccs-sadugas committed Jul 14, 2020
1 parent 9b2261c commit 69449b6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Error handling.
use std::fmt::Display;
use std::io;
use std::result;

Expand All @@ -22,3 +23,38 @@ impl From<io::Error> for Error {
Error::IOError(e)
}
}

impl Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IOError(e) => e.fmt(fmt),
Error::LZMAError(e) => e.fmt(fmt),
Error::XZError(e) => e.fmt(fmt),
}
}
}

#[cfg(test)]
mod test {
use super::Error;

#[test]
fn test_display() {
assert_eq!(
Error::IOError(std::io::Error::new(
std::io::ErrorKind::Other,
"this is an error"
))
.to_string(),
"this is an error"
);
assert_eq!(
Error::LZMAError("this is an error".to_string()).to_string(),
"this is an error"
);
assert_eq!(
Error::XZError("this is an error".to_string()).to_string(),
"this is an error"
);
}
}

0 comments on commit 69449b6

Please sign in to comment.