Skip to content

Commit 2cb5cb6

Browse files
authored
Merge pull request #26 from wiktor-k/impl-display-error
Implement `std::fmt::Display` and `std::error::Error` for `crate::Error`
2 parents 407cbcc + 796b589 commit 2cb5cb6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ pub enum Error {
2828
WalkDir(walkdir::Error),
2929
}
3030

31+
impl std::fmt::Display for Error {
32+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33+
match self {
34+
Error::Io(inner) => write!(f, "I/O error: {}", inner),
35+
Error::StripPrefix(inner) => write!(f, "Strip prefix error: {}", inner),
36+
Error::WalkDir(inner) => write!(f, "Walk dir error: {}", inner),
37+
}
38+
}
39+
}
40+
41+
impl std::error::Error for Error {}
42+
3143
/// Are the contents of two directories different?
3244
///
3345
/// # Examples
@@ -96,3 +108,21 @@ impl From<walkdir::Error> for Error {
96108
Error::WalkDir(e)
97109
}
98110
}
111+
112+
#[cfg(test)]
113+
mod tests {
114+
use super::*;
115+
116+
#[test]
117+
fn test_display() {
118+
use std::io::ErrorKind;
119+
120+
assert_eq!(
121+
format!(
122+
"{}",
123+
Error::Io(std::io::Error::new(ErrorKind::Other, "oh no!"))
124+
),
125+
"I/O error: oh no!"
126+
);
127+
}
128+
}

0 commit comments

Comments
 (0)