• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zbraniecki / icu4x / 13958601093

19 Mar 2025 04:17PM UTC coverage: 74.164% (-1.5%) from 75.71%
13958601093

push

github

web-flow
Clean up properties docs (#6315)

58056 of 78281 relevant lines covered (74.16%)

819371.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.59
/provider/source/src/datetime/patterns.rs
1
// This file is part of ICU4X. For terms of use, please see the file
2
// called LICENSE at the top level of the ICU4X source tree
3
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4

5
use crate::cldr_serde;
6
use icu::datetime::provider::calendar::*;
7
use icu::datetime::provider::pattern;
8
use icu::datetime::provider::pattern::CoarseHourCycle;
9

10
impl From<&cldr_serde::ca::LengthPatterns> for patterns::LengthPatterns<'_> {
11
    fn from(other: &cldr_serde::ca::LengthPatterns) -> Self {
2,737✔
12
        // TODO(#308): Support numbering system variations. We currently throw them away.
13
        Self {
2,737✔
14
            full: other
2,737✔
15
                .full
16
                .get_pattern()
17
                .parse()
18
                .expect("Failed to parse pattern"),
19
            long: other
2,737✔
20
                .long
21
                .get_pattern()
22
                .parse()
23
                .expect("Failed to parse pattern"),
24
            medium: other
2,737✔
25
                .medium
26
                .get_pattern()
27
                .parse()
28
                .expect("Failed to parse pattern"),
29
            short: other
2,737✔
30
                .short
31
                .get_pattern()
32
                .parse()
33
                .expect("Failed to parse pattern"),
34
        }
×
35
    }
2,737✔
36
}
37

38
impl From<&cldr_serde::ca::DateTimeFormats> for patterns::LengthPatterns<'_> {
39
    fn from(other: &cldr_serde::ca::DateTimeFormats) -> Self {
×
40
        // TODO(#308): Support numbering system variations. We currently throw them away.
41
        Self {
×
42
            full: other
×
43
                .full
44
                .get_pattern()
45
                .parse()
46
                .expect("Failed to parse pattern"),
47
            long: other
×
48
                .long
49
                .get_pattern()
50
                .parse()
51
                .expect("Failed to parse pattern"),
52
            medium: other
×
53
                .medium
54
                .get_pattern()
55
                .parse()
56
                .expect("Failed to parse pattern"),
57
            short: other
×
58
                .short
59
                .get_pattern()
60
                .parse()
61
                .expect("Failed to parse pattern"),
62
        }
×
63
    }
×
64
}
65

66
impl From<&cldr_serde::ca::DateTimeFormats> for patterns::GenericLengthPatterns<'_> {
67
    fn from(other: &cldr_serde::ca::DateTimeFormats) -> Self {
5,466✔
68
        // TODO(#308): Support numbering system variations. We currently throw them away.
69
        Self {
5,466✔
70
            full: other
5,466✔
71
                .full
72
                .get_pattern()
73
                .parse()
74
                .expect("Failed to parse pattern"),
75
            long: other
5,466✔
76
                .long
77
                .get_pattern()
78
                .parse()
79
                .expect("Failed to parse pattern"),
80
            medium: other
5,466✔
81
                .medium
82
                .get_pattern()
83
                .parse()
84
                .expect("Failed to parse pattern"),
85
            short: other
5,466✔
86
                .short
87
                .get_pattern()
88
                .parse()
89
                .expect("Failed to parse pattern"),
90
        }
×
91
    }
5,466✔
92
}
93

94
impl From<&cldr_serde::ca::Dates> for DateLengths<'_> {
95
    fn from(other: &cldr_serde::ca::Dates) -> Self {
8✔
96
        let length_combinations_v1 = patterns::GenericLengthPatterns::from(&other.datetime_formats);
8✔
97

98
        Self {
8✔
99
            date: (&other.date_formats).into(),
8✔
100
            length_combinations: length_combinations_v1,
8✔
101
        }
102
    }
8✔
103
}
104

105
impl From<&cldr_serde::ca::Dates> for TimeLengths<'_> {
106
    fn from(other: &cldr_serde::ca::Dates) -> Self {
2,729✔
107
        let length_combinations_v1 = patterns::GenericLengthPatterns::from(&other.datetime_formats);
2,729✔
108
        let skeletons_v1 = DateSkeletonPatterns::from(&other.datetime_formats.available_formats);
2,729✔
109

110
        let pattern_str_full = other.time_formats.full.get_pattern();
2,729✔
111
        let pattern_str_long = other.time_formats.long.get_pattern();
2,729✔
112
        let pattern_str_medium = other.time_formats.medium.get_pattern();
2,729✔
113
        let pattern_str_short = other.time_formats.short.get_pattern();
2,729✔
114

115
        let pattern_full = pattern_str_full
2,729✔
116
            .parse()
117
            .expect("Failed to create a full Pattern from bytes.");
2,729✔
118
        let pattern_long = pattern_str_long
2,729✔
119
            .parse()
120
            .expect("Failed to create a long Pattern from bytes.");
2,729✔
121
        let pattern_medium = pattern_str_medium
2,729✔
122
            .parse()
123
            .expect("Failed to create a medium Pattern from bytes.");
2,729✔
124
        let pattern_short = pattern_str_short
2,729✔
125
            .parse()
126
            .expect("Failed to create a short Pattern from bytes.");
2,729✔
127

128
        let mut preferred_hour_cycle: Option<CoarseHourCycle> = None;
2,729✔
129
        let arr = [
2,729✔
130
            pattern::CoarseHourCycle::determine(&pattern_full),
2,729✔
131
            pattern::CoarseHourCycle::determine(&pattern_long),
2,729✔
132
            pattern::CoarseHourCycle::determine(&pattern_medium),
2,729✔
133
            pattern::CoarseHourCycle::determine(&pattern_short),
2,729✔
134
        ];
135
        let iter = arr.iter().flatten();
2,729✔
136
        for hour_cycle in iter {
13,645✔
137
            if let Some(preferred_hour_cycle) = preferred_hour_cycle {
10,916✔
138
                assert_eq!(
8,187✔
139
                    *hour_cycle, preferred_hour_cycle,
140
                    "A locale contained a mix of coarse hour cycle types"
141
                );
142
            } else {
143
                preferred_hour_cycle = Some(*hour_cycle);
2,729✔
144
            }
145
        }
146

147
        let preferred_hour_cycle =
148
            preferred_hour_cycle.expect("Could not find a preferred hour cycle.");
2,729✔
149
        let alt_hour_cycle = if preferred_hour_cycle == CoarseHourCycle::H11H12 {
2,729✔
150
            CoarseHourCycle::H23H24
1,200✔
151
        } else {
152
            CoarseHourCycle::H11H12
1,529✔
153
        };
154

155
        let (time_h11_h12, time_h23_h24) = {
2,729✔
156
            let time = (&other.time_formats).into();
2,729✔
157
            let alt_time = patterns::LengthPatterns {
2,729✔
158
                full: alt_hour_cycle
2,729✔
159
                    .apply_on_pattern(
160
                        &length_combinations_v1,
161
                        &skeletons_v1,
162
                        pattern_str_full,
2,729✔
163
                        pattern_full,
2,729✔
164
                    )
165
                    .as_ref()
166
                    .expect("Failed to apply a coarse hour cycle to a full pattern.")
167
                    .into(),
168
                long: alt_hour_cycle
2,729✔
169
                    .apply_on_pattern(
170
                        &length_combinations_v1,
171
                        &skeletons_v1,
172
                        pattern_str_long,
2,729✔
173
                        pattern_long,
2,729✔
174
                    )
175
                    .as_ref()
176
                    .expect("Failed to apply a coarse hour cycle to a long pattern.")
177
                    .into(),
178
                medium: alt_hour_cycle
2,729✔
179
                    .apply_on_pattern(
180
                        &length_combinations_v1,
181
                        &skeletons_v1,
182
                        pattern_str_medium,
2,729✔
183
                        pattern_medium,
2,729✔
184
                    )
185
                    .as_ref()
186
                    .expect("Failed to apply a coarse hour cycle to a medium pattern.")
187
                    .into(),
188
                short: alt_hour_cycle
2,729✔
189
                    .apply_on_pattern(
190
                        &length_combinations_v1,
191
                        &skeletons_v1,
192
                        pattern_str_short,
2,729✔
193
                        pattern_short,
2,729✔
194
                    )
195
                    .as_ref()
196
                    .expect("Failed to apply a coarse hour cycle to a short pattern.")
197
                    .into(),
198
            };
2,729✔
199

200
            match preferred_hour_cycle {
2,729✔
201
                CoarseHourCycle::H11H12 => (time, alt_time),
1,200✔
202
                CoarseHourCycle::H23H24 => (alt_time, time),
1,529✔
203
            }
204
        };
×
205

206
        Self {
2,729✔
207
            time_h11_h12,
208
            time_h23_h24,
209
            preferred_hour_cycle,
210
        }
211
    }
2,729✔
212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc