From 85e5c29fe96ce2837b3105f4390c9c12d5ae8bdc Mon Sep 17 00:00:00 2001 From: Simon Dugas Date: Tue, 14 Jul 2020 21:49:55 +0000 Subject: [PATCH] error: implement Display Useful for printing out error messages to users. --- src/error.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/error.rs b/src/error.rs index 2ade0a1..0e51e0b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,6 @@ //! Error handling. +use std::fmt::Display; use std::io; use std::result; @@ -22,3 +23,37 @@ impl From 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) | 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" + ); + } +}