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