/home/a220/proj/radnelac/src/display/symmetry.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::CommonWeekOfYear; |
6 | | use crate::calendar::Symmetry; |
7 | | use crate::calendar::Symmetry010; |
8 | | use crate::calendar::Symmetry010Solstice; |
9 | | use crate::calendar::Symmetry454; |
10 | | use crate::calendar::Symmetry454Solstice; |
11 | | use crate::calendar::ToFromCommonDate; |
12 | | use crate::calendar::ToFromOrdinalDate; |
13 | | use crate::clock::TimeOfDay; |
14 | | use crate::day_count::ToFixed; |
15 | | use crate::day_cycle::Weekday; |
16 | | use crate::display::moment::DisplayMomentItem; |
17 | | use crate::display::prelude::PresetDisplay; |
18 | | use crate::display::private::fmt_days_since_epoch; |
19 | | use crate::display::private::fmt_number; |
20 | | use crate::display::private::fmt_quarter; |
21 | | use crate::display::private::fmt_seconds_since_epoch; |
22 | | use crate::display::private::fmt_string; |
23 | | use crate::display::private::get_dict; |
24 | | use crate::display::private::DisplayItem; |
25 | | use crate::display::private::DisplayOptions; |
26 | | use crate::display::private::NumericContent; |
27 | | use crate::display::private::TextContent; |
28 | | use crate::display::text::prelude::Language; |
29 | | use std::fmt; |
30 | | |
31 | | impl<const T: bool, const U: bool> DisplayItem for Symmetry<T, U> { |
32 | 2.05k | fn supported_lang(lang: Language) -> bool { |
33 | 2.05k | get_dict(lang).symmetry.as_ref().is_some() |
34 | 2.05k | } |
35 | | |
36 | 74.7k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
37 | 74.7k | match n { |
38 | | NumericContent::Month | NumericContent::DayOfMonth | NumericContent::Year => { |
39 | 65.5k | self.to_common_date().fmt_numeric(n, opt) |
40 | | } |
41 | 0 | NumericContent::DayOfWeek => self.convert::<Weekday>().fmt_numeric(n, opt), |
42 | 2.04k | NumericContent::DayOfYear => self.to_ordinal().fmt_numeric(n, opt), |
43 | | NumericContent::Hour1to12 |
44 | | | NumericContent::Hour0to23 |
45 | | | NumericContent::Minute |
46 | 3.07k | | NumericContent::Second => self.convert::<TimeOfDay>().fmt_numeric(n, opt), |
47 | 2.04k | NumericContent::SecondsSinceEpoch => fmt_seconds_since_epoch(*self, opt), |
48 | 0 | NumericContent::Quarter => fmt_quarter(*self, opt), |
49 | 2.04k | NumericContent::DaysSinceEpoch => fmt_days_since_epoch(*self, opt), |
50 | 0 | NumericContent::ComplementaryDay => String::from(""), |
51 | 0 | NumericContent::WeekOfYear => fmt_number(self.week_of_year() as i16, opt), |
52 | | } |
53 | 74.7k | } |
54 | | |
55 | 50.1k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
56 | 50.1k | match (t, get_dict(lang).symmetry.as_ref()) { |
57 | 16.3k | (TextContent::MonthName, Some(dict)) => { |
58 | 16.3k | let months: [&str; 13] = [ |
59 | 16.3k | dict.january, |
60 | 16.3k | dict.february, |
61 | 16.3k | dict.march, |
62 | 16.3k | dict.april, |
63 | 16.3k | dict.may, |
64 | 16.3k | dict.june, |
65 | 16.3k | dict.july, |
66 | 16.3k | dict.august, |
67 | 16.3k | dict.september, |
68 | 16.3k | dict.october, |
69 | 16.3k | dict.november, |
70 | 16.3k | dict.december, |
71 | 16.3k | dict.irvember, |
72 | 16.3k | ]; |
73 | 16.3k | let name = months[self.to_common_date().month as usize - 1]; |
74 | 16.3k | fmt_string(name, opt) |
75 | | } |
76 | 0 | (TextContent::DayOfMonthName, _) => fmt_string("", opt), |
77 | 16.3k | (TextContent::DayOfWeekName, _) => self.convert::<Weekday>().fmt_text(t, lang, opt), |
78 | | (TextContent::HalfDayName | TextContent::HalfDayAbbrev, _) => { |
79 | 0 | self.convert::<TimeOfDay>().fmt_text(t, lang, opt) |
80 | | } |
81 | 12.2k | (TextContent::EraName, Some(dict)) => { |
82 | 12.2k | if self.to_common_date().year < 0 { |
83 | 6.28k | fmt_string(dict.before_epoch_full, opt) |
84 | | } else { |
85 | 6.00k | fmt_string(dict.after_epoch_full, opt) |
86 | | } |
87 | | } |
88 | 4.09k | (TextContent::EraAbbreviation, Some(dict)) => { |
89 | 4.09k | if self.to_common_date().year < 0 { |
90 | 2.05k | fmt_string(dict.before_epoch_abr, opt) |
91 | | } else { |
92 | 2.04k | fmt_string(dict.after_epoch_abr, opt) |
93 | | } |
94 | | } |
95 | 1.02k | (_, _) => String::from(""), |
96 | | } |
97 | 50.1k | } |
98 | | } |
99 | | |
100 | | impl<const T: bool, const U: bool> PresetDisplay for Symmetry<T, U> {} |
101 | | |
102 | | impl<const T: bool, const U: bool> fmt::Display for Symmetry<T, U> { |
103 | 10.2k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
104 | 10.2k | write!(f, "{}", self.long_date()) |
105 | 10.2k | } |
106 | | } |
107 | | |
108 | | impl DisplayMomentItem for Symmetry454 {} |
109 | | impl DisplayMomentItem for Symmetry010 {} |
110 | | impl DisplayMomentItem for Symmetry454Solstice {} |
111 | | impl DisplayMomentItem for Symmetry010Solstice {} |
112 | | |
113 | | #[cfg(test)] |
114 | | mod tests { |
115 | | use super::*; |
116 | | |
117 | | #[test] |
118 | 1 | fn expected_languages() { |
119 | 1 | assert!(Symmetry::<false, false>::supported_lang(Language::EN)); |
120 | 1 | assert!(Symmetry::<false, false>::supported_lang(Language::FR)); |
121 | 1 | assert!(Symmetry::<false, true>::supported_lang(Language::EN)); |
122 | 1 | assert!(Symmetry::<false, true>::supported_lang(Language::FR)); |
123 | 1 | assert!(Symmetry::<true, false>::supported_lang(Language::EN)); |
124 | 1 | assert!(Symmetry::<true, false>::supported_lang(Language::FR)); |
125 | 1 | assert!(Symmetry::<true, true>::supported_lang(Language::EN)); |
126 | 1 | assert!(Symmetry::<true, true>::supported_lang(Language::FR)); |
127 | 1 | } |
128 | | } |