chrono_english/
errors.rs
1use scanlex::ScanError;
2use std::error::Error;
3use std::fmt;
4
5#[derive(Debug)]
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: &str) -> DateError {
21 DateError {
22 details: msg.into(),
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.to_string())
33 }
34}
35
36pub trait OrErr<T> {
38 fn or_err(self, msg: &str) -> DateResult<T>;
40
41 fn or_then_err<C: FnOnce() -> String>(self, fun: C) -> DateResult<T>;
43}
44
45impl<T> OrErr<T> for Option<T> {
46 fn or_err(self, msg: &str) -> DateResult<T> {
47 self.ok_or(date_error(msg))
48 }
49
50 fn or_then_err<C: FnOnce() -> String>(self, fun: C) -> DateResult<T> {
51 self.ok_or_else(|| date_error(&fun()))
52 }
53}