Skip to content

Commit 2c522d4

Browse files
sunng87piperRyan
authored andcommitted
feat: implement Error for ParseError
1 parent 3a56bed commit 2c522d4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/interval_parse/parse_error.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::num::{ParseFloatError, ParseIntError};
23

34
#[derive(Debug, PartialEq, Eq)]
@@ -35,6 +36,30 @@ impl From<ParseFloatError> for ParseError {
3536
}
3637
}
3738

39+
impl fmt::Display for ParseError {
40+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41+
match self {
42+
ParseError::ParseIntErr(e) => write!(f, "Failed to parse integer: {}", e),
43+
ParseError::ParseFloatErr(e) => write!(f, "Failed to parse float: {}", e),
44+
ParseError::InvalidYearMonth(s) => write!(f, "Invalid year/month interval: {}", s),
45+
ParseError::InvalidTime(s) => write!(f, "Invalid time interval: {}", s),
46+
ParseError::InvalidInterval(s) => write!(f, "Invalid interval: {}", s),
47+
}
48+
}
49+
}
50+
51+
impl std::error::Error for ParseError {
52+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53+
match self {
54+
ParseError::ParseIntErr(e) => Some(e),
55+
ParseError::ParseFloatErr(e) => Some(e),
56+
ParseError::InvalidYearMonth(_)
57+
| ParseError::InvalidTime(_)
58+
| ParseError::InvalidInterval(_) => None,
59+
}
60+
}
61+
}
62+
3863
#[cfg(test)]
3964
mod tests {
4065
use super::ParseError;

0 commit comments

Comments
 (0)