Coverage Report

Created: 2025-10-19 21:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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::HasEpagemonae;
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.epagomenae() {
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.08k
            (TextContent::MonthName, Some(dict)) => {
65
7.08k
                let months: [&str; 12] = [
66
7.08k
                    dict.vendemiaire,
67
7.08k
                    dict.brumaire,
68
7.08k
                    dict.frimaire,
69
7.08k
                    dict.nivose,
70
7.08k
                    dict.pluviose,
71
7.08k
                    dict.ventose,
72
7.08k
                    dict.germinal,
73
7.08k
                    dict.floreal,
74
7.08k
                    dict.prairial,
75
7.08k
                    dict.messidor,
76
7.08k
                    dict.thermidor,
77
7.08k
                    dict.fructidor,
78
7.08k
                ];
79
7.08k
                let name = match self.try_month() {
80
7.06k
                    Some(m) => months[(m as usize) - 1],
81
20
                    None => "",
82
                };
83
7.08k
                fmt_string(name, opt)
84
            }
85
0
            (TextContent::DayOfMonthName, _) => fmt_string("", opt),
86
7.08k
            (TextContent::DayOfWeekName, Some(dict)) => {
87
7.08k
                let weekdays: [&str; 10] = [
88
7.08k
                    dict.primidi,
89
7.08k
                    dict.duodi,
90
7.08k
                    dict.tridi,
91
7.08k
                    dict.quartidi,
92
7.08k
                    dict.quintidi,
93
7.08k
                    dict.sextidi,
94
7.08k
                    dict.septidi,
95
7.08k
                    dict.octidi,
96
7.08k
                    dict.nonidi,
97
7.08k
                    dict.decadi,
98
7.08k
                ];
99
7.08k
                let name = match self.weekday() {
100
7.06k
                    Some(m) => weekdays[(m as usize) - 1],
101
20
                    None => "",
102
                };
103
7.08k
                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.44k
                    fmt_string(dict.before_republic_full, opt)
111
                } else {
112
2.67k
                    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
996
                    fmt_string(dict.before_republic_abr, opt)
118
                } else {
119
1.05k
                    fmt_string(dict.after_republic_abr, opt)
120
                }
121
            }
122
6.73k
            (TextContent::ComplementaryDayName, Some(dict)) => {
123
6.73k
                let sansculottides: [&str; 6] = [
124
6.73k
                    dict.fete_de_la_vertu,
125
6.73k
                    dict.fete_du_genie,
126
6.73k
                    dict.fete_du_travail,
127
6.73k
                    dict.fete_de_lopinion,
128
6.73k
                    dict.fete_des_recompenses,
129
6.73k
                    dict.fete_de_la_revolution,
130
6.73k
                ];
131
6.73k
                let name = match self.epagomenae() {
132
5.71k
                    Some(d) => sansculottides[(d as usize) - 1],
133
1.02k
                    None => "",
134
                };
135
6.73k
                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.epagomenae().is_some() {
145
82
            self.preset_str(Language::EN, LONG_COMPL)
146
        } else {
147
5.03k
            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
}