chinese_format/gregorian/date/
errors.rs

1use std::{error::Error, fmt::Display};
2
3/// Error for when the *month* part of a date is out of range.
4///
5/// ```
6/// use chinese_format::gregorian::*;
7///
8/// assert_eq!(
9///     MonthOutOfRange(90).to_string(),
10///     "Month out of range: 90"
11/// );
12/// ```
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct MonthOutOfRange(pub u8);
15
16impl Display for MonthOutOfRange {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(f, "Month out of range: {}", self.0)
19    }
20}
21
22impl Error for MonthOutOfRange {}
23
24/// Error for when the *day* part of a date is out of range.
25///
26/// ```
27/// use chinese_format::gregorian::*;
28///
29/// assert_eq!(
30///     DayOutOfRange(91).to_string(),
31///     "Day out of range: 91"
32/// );
33/// ```
34#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub struct DayOutOfRange(pub u8);
36
37impl Display for DayOutOfRange {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        write!(f, "Day out of range: {}", self.0)
40    }
41}
42
43impl Error for DayOutOfRange {}
44
45/// Error for when the *week day* part of a date is out of range.
46///
47/// ```
48/// use chinese_format::gregorian::*;
49///
50/// assert_eq!(
51///     WeekDayOutOfRange(92).to_string(),
52///     "Week day out of range: 92"
53/// );
54/// ```
55#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
56pub struct WeekDayOutOfRange(pub u8);
57
58impl Display for WeekDayOutOfRange {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(f, "Week day out of range: {}", self.0)
61    }
62}
63
64impl Error for WeekDayOutOfRange {}
65
66/// Error for when a date cannot exist in reality - such as `2009-02-31`.
67///
68/// ```
69/// use chinese_format::gregorian::*;
70///
71/// assert_eq!(
72///     InvalidDate {
73///         year: None,
74///         month: 2,
75///         day: 31
76///     }.to_string(),
77///     "Invalid date: 2-31"
78/// );
79///
80/// assert_eq!(
81///     InvalidDate {
82///         year: Some(1986),
83///         month: 2,
84///         day: 31
85///     }.to_string(),
86///     "Invalid date: 1986-2-31"
87/// );
88/// ```
89#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
90pub struct InvalidDate {
91    pub year: Option<u16>,
92    pub month: u8,
93    pub day: u8,
94}
95
96impl Display for InvalidDate {
97    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98        match self.year {
99            Some(year) => write!(f, "Invalid date: {}-{}-{}", year, self.month, self.day),
100
101            None => write!(f, "Invalid date: {}-{}", self.month, self.day),
102        }
103    }
104}
105
106impl Error for InvalidDate {}