/home/a220/proj/radnelac/src/display/positivist.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::HasIntercalaryDays; |
6 | | use crate::calendar::Perennial; |
7 | | use crate::calendar::Positivist; |
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::DisplayOptions; |
24 | | use crate::display::private::NumericContent; |
25 | | use crate::display::private::TextContent; |
26 | | use crate::display::text::prelude::Language; |
27 | | use std::fmt; |
28 | | |
29 | | impl DisplayItem for Positivist { |
30 | 2 | fn supported_lang(lang: Language) -> bool { |
31 | 2 | get_dict(lang).positivist.as_ref().is_some() |
32 | 2 | } |
33 | | |
34 | 19.1k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
35 | 19.1k | match n { |
36 | | NumericContent::Month | NumericContent::DayOfMonth | NumericContent::Year => { |
37 | 15.8k | self.to_common_date().fmt_numeric(n, opt) |
38 | | } |
39 | 512 | NumericContent::DayOfWeek => match self.weekday() { |
40 | 512 | Some(d) => d.fmt_numeric(n, opt), |
41 | 0 | None => "".to_string(), |
42 | | }, |
43 | 512 | NumericContent::DayOfYear => self.to_ordinal().fmt_numeric(n, opt), |
44 | | NumericContent::Hour1to12 |
45 | | | NumericContent::Hour0to23 |
46 | | | NumericContent::Minute |
47 | 768 | | NumericContent::Second => self.convert::<TimeOfDay>().fmt_numeric(n, opt), |
48 | 512 | NumericContent::SecondsSinceEpoch => fmt_seconds_since_epoch(*self, opt), |
49 | 0 | NumericContent::Quarter => fmt_quarter(*self, opt), |
50 | 512 | NumericContent::DaysSinceEpoch => fmt_days_since_epoch(*self, opt), |
51 | 0 | NumericContent::ComplementaryDay => match self.complementary() { |
52 | 0 | Some(d) => fmt_number(d as i8, opt), |
53 | 0 | None => "".to_string(), |
54 | | }, |
55 | 512 | NumericContent::WeekOfYear => match self.try_week_of_year() { |
56 | 512 | Some(w) => fmt_number(w as i8, opt), |
57 | 0 | None => "".to_string(), |
58 | | }, |
59 | | } |
60 | 19.1k | } |
61 | 12.2k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
62 | 12.2k | match (t, get_dict(lang).positivist.as_ref()) { |
63 | 3.57k | (TextContent::MonthName, Some(dict)) => { |
64 | 3.57k | let months: [&str; 13] = [ |
65 | 3.57k | dict.moses, |
66 | 3.57k | dict.homer, |
67 | 3.57k | dict.aristotle, |
68 | 3.57k | dict.archimedes, |
69 | 3.57k | dict.caesar, |
70 | 3.57k | dict.saint_paul, |
71 | 3.57k | dict.charlemagne, |
72 | 3.57k | dict.dante, |
73 | 3.57k | dict.gutenburg, |
74 | 3.57k | dict.shakespeare, |
75 | 3.57k | dict.descartes, |
76 | 3.57k | dict.frederick, |
77 | 3.57k | dict.bichat, |
78 | 3.57k | ]; |
79 | 3.57k | let name = match self.try_month() { |
80 | 3.57k | Some(m) => months[(m as usize) - 1], |
81 | 2 | None => "", |
82 | | }; |
83 | 3.57k | fmt_string(name, opt) |
84 | | } |
85 | 0 | (TextContent::DayOfMonthName, _) => fmt_string("", opt), |
86 | 3.57k | (TextContent::DayOfWeekName, _) => match self.weekday() { |
87 | 3.57k | Some(m) => m.fmt_text(t, lang, opt), |
88 | 2 | None => fmt_string("", opt), |
89 | | }, |
90 | | (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => { |
91 | 0 | self.convert::<TimeOfDay>().fmt_text(t, lang, opt) |
92 | | } |
93 | 2.56k | (TextContent::EraName, Some(dict)) => { |
94 | 2.56k | if self.to_common_date().year < 0 { |
95 | 1.26k | fmt_string(dict.before_crisis_full, opt) |
96 | | } else { |
97 | 1.29k | fmt_string(dict.after_crisis_full, opt) |
98 | | } |
99 | | } |
100 | 1.02k | (TextContent::EraAbbreviation, Some(dict)) => { |
101 | 1.02k | if self.to_common_date().year < 0 { |
102 | 528 | fmt_string(dict.before_crisis_abr, opt) |
103 | | } else { |
104 | 496 | fmt_string(dict.after_crisis_abr, opt) |
105 | | } |
106 | | } |
107 | 1.54k | (TextContent::ComplementaryDayName, Some(dict)) => { |
108 | 1.54k | let compl: [&str; 2] = [dict.festival_of_dead, dict.festival_of_holy_women]; |
109 | 1.54k | let name = match self.complementary() { |
110 | 1.03k | Some(d) => compl[(d as usize) - 1], |
111 | 512 | None => "", |
112 | | }; |
113 | 1.54k | fmt_string(name, opt) |
114 | | } |
115 | 0 | (_, _) => String::from(""), |
116 | | } |
117 | 12.2k | } |
118 | | } |
119 | | |
120 | | impl PresetDisplay for Positivist { |
121 | 2.56k | fn long_date(&self) -> String { |
122 | 2.56k | if self.complementary().is_some() { |
123 | 6 | self.preset_str(Language::EN, LONG_COMPL) |
124 | | } else { |
125 | 2.55k | self.preset_str(Language::EN, LONG_DATE) |
126 | | } |
127 | 2.56k | } |
128 | | } |
129 | | |
130 | | impl fmt::Display for Positivist { |
131 | 2.56k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
132 | 2.56k | write!(f, "{}", self.long_date()) |
133 | 2.56k | } |
134 | | } |
135 | | |
136 | | impl DisplayMomentItem for Positivist {} |
137 | | |
138 | | #[cfg(test)] |
139 | | mod tests { |
140 | | use super::*; |
141 | | |
142 | | #[test] |
143 | 1 | fn expected_languages() { |
144 | 1 | assert!(Positivist::supported_lang(Language::EN)); |
145 | 1 | assert!(Positivist::supported_lang(Language::FR)); |
146 | 1 | } |
147 | | } |