chrono_english/
errors.rs

1use scanlex::ScanError;
2use std::error::Error;
3use std::fmt;
4
5#[derive(Debug, Hash, Clone, Eq, PartialEq, Ord, PartialOrd)]
6pub struct DateError {
7    details: String,
8}
9
10impl fmt::Display for DateError {
11    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12        write!(f, "{}", self.details)
13    }
14}
15
16impl Error for DateError {}
17
18pub type DateResult<T> = Result<T, DateError>;
19
20pub fn date_error(msg: impl ToString) -> DateError {
21    DateError {
22        details: msg.to_string(),
23    }
24}
25
26pub fn date_result<T>(msg: &str) -> DateResult<T> {
27    Err(date_error(msg).into())
28}
29
30impl From<ScanError> for DateError {
31    fn from(err: ScanError) -> DateError {
32        date_error(err)
33    }
34}
35
36/// This trait maps optional values onto `DateResult`
37pub trait OrErr<T> {
38    /// use when the error message is always a simple string
39    fn or_err(self, msg: &str) -> DateResult<T>;
40
41}
42
43impl<T> OrErr<T> for Option<T> {
44    fn or_err(self, msg: &str) -> DateResult<T> {
45        self.ok_or(date_error(msg))
46    }
47
48}