|
| 1 | +use std::fmt; |
1 | 2 | use std::num::{ParseFloatError, ParseIntError}; |
2 | 3 |
|
3 | 4 | #[derive(Debug, PartialEq, Eq)] |
@@ -35,6 +36,30 @@ impl From<ParseFloatError> for ParseError { |
35 | 36 | } |
36 | 37 | } |
37 | 38 |
|
| 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 | + |
38 | 63 | #[cfg(test)] |
39 | 64 | mod tests { |
40 | 65 | use super::ParseError; |
|
0 commit comments