/home/a220/proj/radnelac/src/display/week.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::day_cycle::Weekday; |
6 | | use crate::display::private::fmt_number; |
7 | | use crate::display::private::fmt_string; |
8 | | use crate::display::private::get_dict; |
9 | | use crate::display::private::Content; |
10 | | use crate::display::private::DisplayItem; |
11 | | use crate::display::private::Item; |
12 | | use crate::display::private::NumericContent; |
13 | | use crate::display::private::Sign; |
14 | | use crate::display::private::TextContent; |
15 | | use crate::display::text::en::EN_DICTIONARY; |
16 | | use crate::display::text::prelude::Language; |
17 | | use crate::display::PresetDisplay; |
18 | | use crate::display::WEEKDAY_NAME_ONLY; |
19 | | use std::fmt; |
20 | | |
21 | | use crate::display::private::DisplayOptions; |
22 | | |
23 | | impl DisplayItem for Weekday { |
24 | 2 | fn supported_lang(lang: Language) -> bool { |
25 | 2 | get_dict(lang).common_weekday.as_ref().is_some() |
26 | 2 | } |
27 | | |
28 | 1.54k | fn fmt_numeric(&self, n: NumericContent, opt: DisplayOptions) -> String { |
29 | 1.54k | match n { |
30 | 1.54k | NumericContent::DayOfWeek => fmt_number(*self as i16, opt), |
31 | 0 | _ => "".to_string(), |
32 | | } |
33 | 1.54k | } |
34 | 56.2k | fn fmt_text(&self, t: TextContent, lang: Language, opt: DisplayOptions) -> String { |
35 | 56.2k | match (t, get_dict(lang).common_weekday.as_ref()) { |
36 | 56.2k | (TextContent::DayOfWeekName, Some(dict)) => { |
37 | 56.2k | let days: [&str; 7] = [ |
38 | 56.2k | dict.sunday, |
39 | 56.2k | dict.monday, |
40 | 56.2k | dict.tuesday, |
41 | 56.2k | dict.wednesday, |
42 | 56.2k | dict.thursday, |
43 | 56.2k | dict.friday, |
44 | 56.2k | dict.saturday, |
45 | 56.2k | ]; |
46 | 56.2k | let name = days[*self as usize]; |
47 | 56.2k | fmt_string(name, opt) |
48 | | } |
49 | 0 | (_, _) => "".to_string(), |
50 | | } |
51 | 56.2k | } |
52 | | } |
53 | | |
54 | | impl PresetDisplay for Weekday {} |
55 | | |
56 | | impl fmt::Display for Weekday { |
57 | 1.02k | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
58 | | const O: DisplayOptions = DisplayOptions { |
59 | | numerals: None, |
60 | | width: None, |
61 | | align: None, |
62 | | padding: None, |
63 | | case: None, |
64 | | sign: Sign::Never, |
65 | | }; |
66 | 1.02k | let item = Item::new(Content::Text(TextContent::DayOfWeekName), O); |
67 | 1.02k | write!(f, "{}", self.fmt_item(Language::EN, item)) |
68 | 1.02k | } |
69 | | } |
70 | | |
71 | | #[cfg(test)] |
72 | | mod tests { |
73 | | use super::*; |
74 | | use num_traits::ToPrimitive; |
75 | | |
76 | | #[test] |
77 | 1 | fn weekday_display_french() { |
78 | 1 | assert!(Weekday::supported_display_lang(Language::EN)); |
79 | 1 | assert!(Weekday::supported_display_lang(Language::FR)); |
80 | 1 | let w_list = [ |
81 | 1 | (Weekday::Monday, "Monday", "Lundi", "1"), |
82 | 1 | (Weekday::Tuesday, "Tuesday", "Mardi", "2"), |
83 | 1 | (Weekday::Wednesday, "Wednesday", "Mercredi", "3"), |
84 | 1 | (Weekday::Thursday, "Thursday", "Jeudi", "4"), |
85 | 1 | (Weekday::Friday, "Friday", "Vendredi", "5"), |
86 | 1 | (Weekday::Saturday, "Saturday", "Samedi", "6"), |
87 | 1 | (Weekday::Sunday, "Sunday", "Dimanche", "0"), |
88 | 1 | ]; |
89 | 8 | for item7 in w_list { |
90 | 7 | let w = item.0; |
91 | 7 | let s0_en = item.1; |
92 | 7 | let s0_fr = item.2; |
93 | 7 | let n0 = item.3; |
94 | 7 | let s1_en = w.preset_str(Language::EN, WEEKDAY_NAME_ONLY); |
95 | 7 | let s1_fr = w.preset_str(Language::FR, WEEKDAY_NAME_ONLY); |
96 | 7 | assert_eq!(s0_en, s1_en); |
97 | 7 | assert_eq!(s0_fr, s1_fr); |
98 | | const O: DisplayOptions = DisplayOptions { |
99 | | numerals: None, |
100 | | width: None, |
101 | | align: None, |
102 | | padding: None, |
103 | | case: None, |
104 | | sign: Sign::Never, |
105 | | }; |
106 | 7 | assert_eq!(w.fmt_numeric(NumericContent::DayOfWeek, O), n0); |
107 | | } |
108 | 1 | } |
109 | | } |