simple_datetime_rs/
date_error.rs

1use std::{error::Error, fmt};
2
3#[derive(Debug, Copy, Clone, PartialEq)]
4pub enum DateErrorKind {
5    WrongDateStringFormat,
6    WrongTimeStringFormat,
7    WrongDateTimeStringFormat,
8    WrongTimeShiftStringFormat,
9    Inner,
10}
11
12#[derive(Debug)]
13pub struct DateError {
14    pub source:Option<Box<dyn Error>>,
15    pub kind:DateErrorKind,
16}
17
18impl DateError {
19    pub fn new_inner(source:Box<dyn Error>)->Self{
20        DateError {
21            source: Some(source),
22            kind: DateErrorKind::Inner
23        }
24    }
25
26    pub fn new_kind(kind:DateErrorKind)->Self{
27        DateError {
28            source: None,
29            kind
30        }
31    }
32}
33
34impl Error for DateError {
35    fn source(&self) -> Option<&(dyn Error + 'static)> {
36        self.source.as_deref()
37    }
38}
39
40impl fmt::Display for DateErrorKind {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        match self {
43            DateErrorKind::WrongDateStringFormat => write!(f, "wrong date format"),
44            DateErrorKind::WrongTimeStringFormat =>  write!(f, "wrong time format"),
45            DateErrorKind::WrongDateTimeStringFormat =>  write!(f, "wrong datetime format"),
46            DateErrorKind::WrongTimeShiftStringFormat =>  write!(f, "wrong time shift format"),
47            DateErrorKind::Inner =>  write!(f, "error in DateError::source"),
48        }
49    }
50}
51
52impl fmt::Display for DateError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "{}", self.kind)
55    }
56}
57
58impl From<DateErrorKind> for DateError {
59    fn from(kind: DateErrorKind) -> Self {
60        Self::new_kind(kind)
61    }
62}
63
64impl From<Box<dyn Error>> for DateError {
65    fn from(err: Box<dyn Error>) -> Self {
66        Self::new_inner(err)
67    }
68}
69
70impl DateErrorKind {
71    pub fn into_boxed_error(self) -> Box<dyn Error> {
72        let error:DateError = self.into();
73        error.into()
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use std::io;
81
82    #[test]
83    fn test_date_error_kind_display() {
84        assert_eq!(DateErrorKind::WrongDateStringFormat.to_string(), "wrong date format");
85        assert_eq!(DateErrorKind::WrongTimeStringFormat.to_string(), "wrong time format");
86        assert_eq!(DateErrorKind::WrongDateTimeStringFormat.to_string(), "wrong datetime format");
87        assert_eq!(DateErrorKind::WrongTimeShiftStringFormat.to_string(), "wrong time shift format");
88        assert_eq!(DateErrorKind::Inner.to_string(), "error in DateError::source");
89    }
90
91    #[test]
92    fn test_date_error_display() {
93        let error = DateError::new_kind(DateErrorKind::WrongDateStringFormat);
94        assert_eq!(error.to_string(), "wrong date format");
95        
96        let error2 = DateError::new_kind(DateErrorKind::WrongTimeStringFormat);
97        assert_eq!(error2.to_string(), "wrong time format");
98    }
99
100    #[test]
101    fn test_date_error_with_source() {
102        let io_error = io::Error::new(io::ErrorKind::InvalidInput, "test error");
103        let date_error = DateError::new_inner(Box::new(io_error));
104        
105        assert!(date_error.source().is_some());
106        assert_eq!(date_error.kind, DateErrorKind::Inner);
107    }
108
109    #[test]
110    fn test_date_error_from_kind() {
111        let error: DateError = DateErrorKind::WrongDateStringFormat.into();
112        assert_eq!(error.kind, DateErrorKind::WrongDateStringFormat);
113        assert!(error.source().is_none());
114    }
115
116    #[test]
117    fn test_date_error_from_boxed_error() {
118        let io_error = io::Error::new(io::ErrorKind::InvalidInput, "test error");
119        let date_error: DateError = (Box::new(io_error) as Box<dyn Error>).into();
120        
121        assert_eq!(date_error.kind, DateErrorKind::Inner);
122        assert!(date_error.source().is_some());
123    }
124
125    #[test]
126    fn test_date_error_kind_into_boxed_error() {
127        let boxed_error = DateErrorKind::WrongDateStringFormat.into_boxed_error();
128        assert!(boxed_error.is::<DateError>());
129    }
130
131    #[test]
132    fn test_date_error_constructors() {
133        let error1 = DateError::new_kind(DateErrorKind::WrongTimeStringFormat);
134        assert_eq!(error1.kind, DateErrorKind::WrongTimeStringFormat);
135        assert!(error1.source().is_none());
136        
137        let io_error = io::Error::new(io::ErrorKind::InvalidInput, "test");
138        let error2 = DateError::new_inner(Box::new(io_error));
139        assert_eq!(error2.kind, DateErrorKind::Inner);
140        assert!(error2.source().is_some());
141    }
142
143    #[test]
144    fn test_date_error_debug() {
145        let error = DateError::new_kind(DateErrorKind::WrongDateStringFormat);
146        let debug_str = format!("{:?}", error);
147        assert!(debug_str.contains("DateError"));
148    }
149
150    #[test]
151    fn test_date_error_kind_debug() {
152        let kind = DateErrorKind::WrongTimeStringFormat;
153        let debug_str = format!("{:?}", kind);
154        assert!(debug_str.contains("WrongTimeStringFormat"));
155    }
156
157    #[test]
158    fn test_date_error_kind_access() {
159        let error = DateError::new_kind(DateErrorKind::WrongDateTimeStringFormat);
160        assert_eq!(error.kind, DateErrorKind::WrongDateTimeStringFormat);
161    }
162
163    #[test]
164    fn test_date_error_kind_clone() {
165        let kind = DateErrorKind::WrongTimeShiftStringFormat;
166        let cloned_kind = kind.clone();
167        assert_eq!(kind, cloned_kind);
168    }
169}