Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Display for Error #53

Merged
merged 2 commits into from
Jul 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 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,47 @@ 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) => write!(fmt, "io error: {}", e),
Error::LZMAError(e) => write!(fmt, "lzma error: {}", e),
Error::XZError(e) => write!(fmt, "xz error: {}", e),
}
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::IOError(e) => Some(e),
Error::LZMAError(_) | Error::XZError(_) => None,
}
}
}

#[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(),
"io error: this is an error"
);
assert_eq!(
Error::LZMAError("this is an error".to_string()).to_string(),
"lzma error: this is an error"
);
assert_eq!(
Error::XZError("this is an error".to_string()).to_string(),
"xz error: this is an error"
);
}
}