/home/a220/proj/radnelac/src/display/french_rev.rs
Line | Count | Source |
1 | | // This Source Code Form is subject to the terms of the Mozilla Public |
2 | | // License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | // file, You can obtain one at https://mozilla.org/MPL/2.0/. |
4 | | |
5 | | use crate::calendar::FrenchRevArith; |
6 | | use crate::calendar::HasIntercalaryDays; |
7 | | use crate::calendar::Perennial; |
8 | | use crate::calendar::ToFromCommonDate; |
9 | | use crate::calendar::ToFromOrdinalDate; |
10 | | use crate::clock::TimeOfDay; |
11 | | use crate::day_count::ToFixed; |
12 | | use crate::display::moment::DisplayMomentItem; |
13 | | use crate::display::prelude::PresetDisplay; |
14 | | use crate::display::prelude::LONG_COMPL; |
15 | | use crate::display::prelude::LONG_DATE; |
16 | | use crate::display::private::fmt_days_since_epoch; |
17 | | use crate::display::private::fmt_number; |
18 | | use crate::display::private::fmt_quarter; |
19 | | use crate::display::private::fmt_seconds_since_epoch; |
20 | | use crate::display::private::fmt_string; |
21 | | use crate::display::private::get_dict; |
22 | | use crate::display::private::DisplayItem; |
23 | | use crate::display::private::NumericContent; |
24 | | use crate::display::private::TextContent; |
25 | | use crate::display::text::prelude::Language; |
26 | | use std::fmt; |
27 | | |
28 | | use crate::display::private::DisplayOptions; |
29 | | |
30 | | impl<const L: bool> DisplayItem for FrenchRevArith<L> { |
31 | 4 | fn supported_lang(lang: Language) -> bool { |
32 | 4 | get_dict(lang).french_rev.as_ref().is_some() |
33 | 4 | } |
34 | | |
35 | 38.3k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
36 | 38.3k | match n { |
37 | | NumericContent::Month | NumericContent::DayOfMonth | NumericContent::Year => { |
38 | 31.6k | self.to_common_date().fmt_numeric(n, opt) |
39 | | } |
40 | 1.02k | NumericContent::DayOfWeek => match self.weekday() { |
41 | 1.02k | Some(d) => fmt_number(d as i8, opt), |
42 | 0 | None => "".to_string(), |
43 | | }, |
44 | 1.02k | NumericContent::DayOfYear => self.to_ordinal().fmt_numeric(n, opt), |
45 | | NumericContent::Hour1to12 |
46 | | | NumericContent::Hour0to23 |
47 | | | NumericContent::Minute |
48 | 1.53k | | NumericContent::Second => self.convert::<TimeOfDay>().fmt_numeric(n, opt), |
49 | 1.02k | NumericContent::SecondsSinceEpoch => fmt_seconds_since_epoch(*self, opt), |
50 | 0 | NumericContent::Quarter => fmt_quarter(*self, opt), |
51 | 1.02k | NumericContent::DaysSinceEpoch => fmt_days_since_epoch(*self, opt), |
52 | 0 | NumericContent::ComplementaryDay => match self.complementary() { |
53 | 0 | Some(d) => fmt_number(d as i8, opt), |
54 | 0 | None => "".to_string(), |
55 | | }, |
56 | 1.02k | NumericContent::WeekOfYear => match self.try_week_of_year() { |
57 | 1.02k | Some(w) => fmt_number(w as i8, opt), |
58 | 0 | None => "".to_string(), |
59 | | }, |
60 | | } |
61 | 38.3k | } |
62 | 28.0k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
63 | 28.0k | match (t, get_dict(lang).french_rev.as_ref()) { |
64 | 7.09k | (TextContent::MonthName, Some(dict)) => { |
65 | 7.09k | let months: [&str; 12] = [ |
66 | 7.09k | dict.vendemiaire, |
67 | 7.09k | dict.brumaire, |
68 | 7.09k | dict.frimaire, |
69 | 7.09k | dict.nivose, |
70 | 7.09k | dict.pluviose, |
71 | 7.09k | dict.ventose, |
72 | 7.09k | dict.germinal, |
73 | 7.09k | dict.floreal, |
74 | 7.09k | dict.prairial, |
75 | 7.09k | dict.messidor, |
76 | 7.09k | dict.thermidor, |
77 | 7.09k | dict.fructidor, |
78 | 7.09k | ]; |
79 | 7.09k | let name = match self.try_month() { |
80 | 7.05k | Some(m) => months[(m as usize) - 1], |
81 | 38 | None => "", |
82 | | }; |
83 | 7.09k | fmt_string(name, opt) |
84 | | } |
85 | 0 | (TextContent::DayOfMonthName, _) => fmt_string("", opt), |
86 | 7.09k | (TextContent::DayOfWeekName, Some(dict)) => { |
87 | 7.09k | let weekdays: [&str; 10] = [ |
88 | 7.09k | dict.primidi, |
89 | 7.09k | dict.duodi, |
90 | 7.09k | dict.tridi, |
91 | 7.09k | dict.quartidi, |
92 | 7.09k | dict.quintidi, |
93 | 7.09k | dict.sextidi, |
94 | 7.09k | dict.septidi, |
95 | 7.09k | dict.octidi, |
96 | 7.09k | dict.nonidi, |
97 | 7.09k | dict.decadi, |
98 | 7.09k | ]; |
99 | 7.09k | let name = match self.weekday() { |
100 | 7.05k | Some(m) => weekdays[(m as usize) - 1], |
101 | 38 | None => "", |
102 | | }; |
103 | 7.09k | fmt_string(name, opt) |
104 | | } |
105 | | (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => { |
106 | 0 | self.convert::<TimeOfDay>().fmt_text(t, lang, opt) |
107 | | } |
108 | 5.12k | (TextContent::EraName, Some(dict)) => { |
109 | 5.12k | if self.to_common_date().year < 0 { |
110 | 2.60k | fmt_string(dict.before_republic_full, opt) |
111 | | } else { |
112 | 2.51k | fmt_string(dict.after_republic_full, opt) |
113 | | } |
114 | | } |
115 | 2.04k | (TextContent::EraAbbreviation, Some(dict)) => { |
116 | 2.04k | if self.to_common_date().year < 0 { |
117 | 1.07k | fmt_string(dict.before_republic_abr, opt) |
118 | | } else { |
119 | 976 | fmt_string(dict.after_republic_abr, opt) |
120 | | } |
121 | | } |
122 | 6.72k | (TextContent::ComplementaryDayName, Some(dict)) => { |
123 | 6.72k | let sansculottides: [&str; 6] = [ |
124 | 6.72k | dict.fete_de_la_vertu, |
125 | 6.72k | dict.fete_du_genie, |
126 | 6.72k | dict.fete_du_travail, |
127 | 6.72k | dict.fete_de_lopinion, |
128 | 6.72k | dict.fete_des_recompenses, |
129 | 6.72k | dict.fete_de_la_revolution, |
130 | 6.72k | ]; |
131 | 6.72k | let name = match self.complementary() { |
132 | 5.70k | Some(d) => sansculottides[(d as usize) - 1], |
133 | 1.02k | None => "", |
134 | | }; |
135 | 6.72k | fmt_string(name, opt) |
136 | | } |
137 | 0 | (_, _) => fmt_string("", opt), |
138 | | } |
139 | 28.0k | } |
140 | | } |
141 | | |
142 | | impl<const L: bool> PresetDisplay for FrenchRevArith<L> { |
143 | 5.12k | fn long_date(&self) -> String { |
144 | 5.12k | if self.complementary().is_some() { |
145 | 72 | self.preset_str(Language::EN, LONG_COMPL) |
146 | | } else { |
147 | 5.04k | self.preset_str(Language::EN, LONG_DATE) |
148 | | } |
149 | 5.12k | } |
150 | | } |
151 | | |
152 | | impl<const L: bool> fmt::Display for FrenchRevArith<L> { |
153 | 5.12k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
154 | 5.12k | write!(f, "{}", self.long_date()) |
155 | 5.12k | } |
156 | | } |
157 | | |
158 | | impl<const L: bool> DisplayMomentItem for FrenchRevArith<L> {} |
159 | | |
160 | | #[cfg(test)] |
161 | | mod tests { |
162 | | use super::*; |
163 | | |
164 | | #[test] |
165 | 1 | fn expected_languages() { |
166 | 1 | assert!(FrenchRevArith::<true>::supported_lang(Language::EN)); |
167 | 1 | assert!(FrenchRevArith::<true>::supported_lang(Language::EN)); |
168 | 1 | assert!(FrenchRevArith::<false>::supported_lang(Language::FR)); |
169 | 1 | assert!(FrenchRevArith::<false>::supported_lang(Language::FR)); |
170 | 1 | } |
171 | | } |