holiday/
eq.rs

1use crate::*;
2
3use std::cmp::Ordering;
4
5impl<S: ToString> PartialEq<NaiveDate> for Holiday<S> {
6    fn eq(&self, date: &NaiveDate) -> bool {
7        match &self.date {
8            HolidayDate::FixedDate(fixed) => fixed == date,
9            HolidayDate::NthDate(nth) => nth == date,
10        }
11    }
12}
13
14impl<S: ToString> PartialEq<NthWeekdayOfMonth> for Holiday<S> {
15    fn eq(&self, nth: &NthWeekdayOfMonth) -> bool {
16        if let HolidayDate::NthDate(self_nth) = self.date {
17            &self_nth == nth
18        } else {
19            false
20        }
21    }
22}
23
24impl<S: ToString> PartialEq for Holiday<S> {
25    fn eq(&self, other: &Self) -> bool {
26        self.date == other.date && self.name.to_string() == other.name.to_string()
27    }
28}
29
30impl<S: ToString> Eq for Holiday<S> {}
31
32impl<S: ToString> Ord for Holiday<S> {
33    fn cmp(&self, other: &Self) -> Ordering {
34        if self.date == other.date {
35            self.name.to_string().cmp(&other.name.to_string())
36        } else {
37            self.date.cmp(&other.date)
38        }
39    }
40}
41
42impl<S: ToString> PartialOrd for Holiday<S> {
43    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
44        Some(self.cmp(other))
45    }
46}
47
48impl PartialOrd for HolidayDate {
49    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
50        Some(self.cmp(other))
51    }
52}
53
54impl Ord for HolidayDate {
55    fn cmp(&self, other: &Self) -> Ordering {
56        match (self, other) {
57            (FixedDate(self_dom), FixedDate(other_dom)) => self_dom.cmp(other_dom),
58            (NthDate(self_nwom), NthDate(other_nwom)) => self_nwom.cmp(other_nwom),
59            (FixedDate(self_dom), NthDate(other_nwom)) => {
60                if self_dom.month == other_nwom.month {
61                    Ordering::Less
62                } else {
63                    self_dom.month.cmp(&other_nwom.month)
64                }
65            }
66            (NthDate(self_nwom), FixedDate(other_dom)) => {
67                if self_nwom.month == other_dom.month {
68                    Ordering::Greater
69                } else {
70                    self_nwom.month.cmp(&other_dom.month)
71                }
72            }
73        }
74    }
75}
76
77impl PartialOrd for DayOfMonth {
78    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
79        Some(self.cmp(other))
80    }
81}
82
83impl Ord for DayOfMonth {
84    fn cmp(&self, other: &Self) -> Ordering {
85        if self.month == other.month {
86            self.day.cmp(&other.day)
87        } else {
88            self.month.cmp(&other.month)
89        }
90    }
91}
92
93impl PartialEq<NaiveDate> for DayOfMonth {
94    fn eq(&self, date: &NaiveDate) -> bool {
95        self.month == date.month() && self.day == date.day()
96    }
97}
98
99
100impl PartialOrd for NthWeekdayOfMonth {
101    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
102        if self.month == other.month {
103            if self.nth == other.nth {
104                self.weekday
105                    .num_days_from_sunday()
106                    .partial_cmp(&other.weekday.num_days_from_sunday())
107            } else {
108                self.nth.partial_cmp(&other.nth)
109            }
110        } else {
111            self.month.partial_cmp(&other.month)
112        }
113    }
114}
115
116impl Ord for NthWeekdayOfMonth {
117    fn cmp(&self, other: &Self) -> Ordering {
118        if self.month == other.month {
119            if self.nth == other.nth {
120                self.weekday
121                    .num_days_from_sunday()
122                    .cmp(&other.weekday.num_days_from_sunday())
123            } else {
124                self.nth.cmp(&other.nth)
125            }
126        } else {
127            self.month.cmp(&other.month)
128        }
129    }
130}
131
132impl PartialEq<NaiveDate> for NthWeekdayOfMonth {
133    fn eq(&self, date: &NaiveDate) -> bool {
134        if self.nth == NthWeekday::Last && date.weekday() == self.weekday {
135            date.is_last_weekday()
136        } else {
137            self == &NthWeekdayOfMonth::from(*date)
138        }
139    }
140}
141
142impl PartialEq<u32> for NthWeekday {
143    fn eq(&self, u: &u32) -> bool {
144        use NthWeekday::*;
145        matches!((self, u), (First, 1) | (Second, 2) | (Third, 3) | (Fourth, 4) | (Fifth, 5))
146    }
147}
148
149impl PartialEq<u32> for Month {
150    fn eq(&self, u: &u32) -> bool {
151        *self as u32 == *u
152    }
153}
154
155impl PartialEq<Month> for u32 {
156    fn eq(&self, m: &Month) -> bool {
157        m == self
158    }
159}
160
161impl PartialOrd<u32> for Month {
162    fn partial_cmp(&self, u: &u32) -> Option<Ordering> {
163        Some((*self as u32).cmp(u))
164    }
165}
166
167impl PartialOrd<Month> for u32 {
168    fn partial_cmp(&self, m: &Month) -> Option<Ordering> {
169        Some((*self).cmp(&(*m as u32)))
170    }
171}