Skip to content

Commit 2216235

Browse files
acatangiuioanachirca
authored andcommitted
implement fmt::Display for VersionizeError
Signed-off-by: Adrian Catangiu <[email protected]>
1 parent 2d1c7e4 commit 2216235

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ pub enum VersionizeError {
4242
Semantic(String),
4343
}
4444

45+
impl std::fmt::Display for VersionizeError {
46+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
47+
use VersionizeError::*;
48+
match self {
49+
Io(e) => write!(f, "An IO error occured: {}", e),
50+
Serialize(e) => write!(f, "A serialization error occured: {}", e),
51+
Deserialize(e) => write!(f, "A deserialization error occured: {}", e),
52+
Semantic(e) => write!(f, "A user generated semantic error occured: {}", e),
53+
}
54+
}
55+
}
56+
4557
/// Versioned serialization/deserialization result.
4658
pub type VersionizeResult<T> = std::result::Result<T, VersionizeError>;
4759

@@ -76,3 +88,17 @@ pub trait Versionize {
7688
/// Returns latest `Self` version number.
7789
fn version() -> u16;
7890
}
91+
92+
#[cfg(test)]
93+
mod tests {
94+
#[test]
95+
fn test_error_debug_display() {
96+
// Validates Debug and Display are implemented.
97+
use VersionizeError::*;
98+
let str = String::from("test");
99+
format!("{:?}{}", Io(0), Io(0));
100+
format!("{:?}{}", Serialize(str.clone()), Serialize(str.clone()));
101+
format!("{:?}{}", Deserialize(str.clone()), Deserialize(str.clone()));
102+
format!("{:?}{}", Semantic(str.clone()), Semantic(str.clone()));
103+
}
104+
}

0 commit comments

Comments
 (0)