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

zbraniecki / icu4x / 6815798908

09 Nov 2023 05:17PM UTC coverage: 72.607% (-2.4%) from 75.01%
6815798908

push

github

web-flow
Implement `Any/BufferProvider` for some smart pointers (#4255)

Allows storing them as a `Box<dyn Any/BufferProvider>` without using a
wrapper type that implements the trait.

44281 of 60987 relevant lines covered (72.61%)

201375.86 hits per line

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

72.94
/components/datetime/src/raw/zoned_datetime.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 icu_calendar::provider::WeekDataV1Marker;
6
use icu_decimal::{
7
    options::{FixedDecimalFormatterOptions, GroupingStrategy},
8
    provider::DecimalSymbolsV1Marker,
9
    FixedDecimalFormatter,
10
};
11
use icu_plurals::{provider::OrdinalV1Marker, PluralRules};
12
use icu_provider::prelude::*;
13

14
use crate::{
15
    format::{datetime, zoned_datetime::FormattedZonedDateTime},
16
    input::{DateTimeInput, TimeZoneInput},
17
    input::{ExtractedDateTimeInput, ExtractedTimeZoneInput},
18
    pattern::runtime::PatternPlurals,
19
    provider::{
20
        self,
21
        calendar::{
22
            patterns::PatternPluralsFromPatternsV1Marker, ErasedDateSymbolsV1Marker,
23
            TimeLengthsV1Marker, TimeSymbolsV1Marker,
24
        },
25
    },
26
    raw,
27
    time_zone::{TimeZoneFormatter, TimeZoneFormatterOptions},
28
    DateTimeError,
29
};
30

31
/// This is the internal "raw" version of [crate::ZonedDateTimeFormatter], i.e. a version of ZonedDateTimeFormatter
32
/// without the generic parameter. The actual implementation of [crate::ZonedDateTimeFormatter] should live here.
33
#[derive(Debug)]
×
34
pub(crate) struct ZonedDateTimeFormatter {
35
    pub datetime_format: raw::DateTimeFormatter,
×
36
    pub time_zone_format: TimeZoneFormatter,
×
37
}
38

39
impl ZonedDateTimeFormatter {
40
    #[inline(never)]
41
    #[cfg(feature = "compiled_data")]
42
    pub fn try_new(
4✔
43
        patterns: DataPayload<PatternPluralsFromPatternsV1Marker>,
44
        symbols_data_fn: impl FnOnce() -> Result<DataPayload<ErasedDateSymbolsV1Marker>, DataError>,
45
        locale: &DataLocale,
46
        time_zone_format_options: TimeZoneFormatterOptions,
47
    ) -> Result<Self, DateTimeError> {
48
        let required = datetime::analyze_patterns(&patterns.get().0, true)
4✔
49
            .map_err(|field| DateTimeError::UnsupportedField(field.symbol))?;
×
50

51
        let req = DataRequest {
4✔
52
            locale,
53
            metadata: Default::default(),
4✔
54
        };
55

56
        let week_data = if required.week_data {
4✔
57
            Some(icu_calendar::week::WeekCalculator::try_new(locale)?)
×
58
        } else {
59
            None
4✔
60
        };
61

62
        let ordinal_rules = if let PatternPlurals::MultipleVariants(_) = &patterns.get().0 {
4✔
63
            Some(PluralRules::try_new_ordinal(locale)?)
×
64
        } else {
×
65
            None
4✔
66
        };
67

68
        let date_symbols_data = if required.date_symbols_data {
8✔
69
            Some(symbols_data_fn()?)
4✔
70
        } else {
×
71
            None
×
72
        };
73

74
        let time_symbols_data = if required.time_symbols_data {
7✔
75
            Some(crate::provider::Baked.load(req)?.take_payload()?)
3✔
76
        } else {
×
77
            None
1✔
78
        };
79

80
        let mut fixed_decimal_format_options = FixedDecimalFormatterOptions::default();
4✔
81
        fixed_decimal_format_options.grouping_strategy = GroupingStrategy::Never;
4✔
82

83
        let fixed_decimal_format =
84
            FixedDecimalFormatter::try_new(locale, fixed_decimal_format_options)?;
4✔
85

86
        let datetime_format = raw::DateTimeFormatter::new(
4✔
87
            patterns,
4✔
88
            date_symbols_data,
4✔
89
            time_symbols_data,
4✔
90
            week_data,
4✔
91
            ordinal_rules,
4✔
92
            fixed_decimal_format,
4✔
93
        );
94

95
        let time_zone_format = TimeZoneFormatter::try_new_for_pattern(
4✔
96
            &crate::provider::Baked,
97
            locale,
98
            datetime_format
4✔
99
                // Only dates have plural variants so we can use any of the patterns for the time segment.
100
                .patterns
101
                .clone(),
102
            &time_zone_format_options,
103
        )?;
×
104

105
        Ok(Self {
4✔
106
            datetime_format,
4✔
107
            time_zone_format,
4✔
108
        })
109
    }
4✔
110

111
    #[inline(never)]
112
    pub fn try_new_unstable<P>(
1✔
113
        provider: &P,
114
        patterns: DataPayload<PatternPluralsFromPatternsV1Marker>,
115
        symbols_data_fn: impl FnOnce() -> Result<DataPayload<ErasedDateSymbolsV1Marker>, DataError>,
116
        locale: &DataLocale,
117
        time_zone_format_options: TimeZoneFormatterOptions,
118
    ) -> Result<Self, DateTimeError>
119
    where
120
        P: DataProvider<TimeSymbolsV1Marker>
121
            + DataProvider<TimeLengthsV1Marker>
122
            + DataProvider<WeekDataV1Marker>
123
            + DataProvider<provider::time_zones::TimeZoneFormatsV1Marker>
124
            + DataProvider<provider::time_zones::ExemplarCitiesV1Marker>
125
            + DataProvider<provider::time_zones::MetazoneGenericNamesLongV1Marker>
126
            + DataProvider<provider::time_zones::MetazoneGenericNamesShortV1Marker>
127
            + DataProvider<provider::time_zones::MetazoneSpecificNamesLongV1Marker>
128
            + DataProvider<provider::time_zones::MetazoneSpecificNamesShortV1Marker>
129
            + DataProvider<OrdinalV1Marker>
130
            + DataProvider<DecimalSymbolsV1Marker>
131
            + ?Sized,
132
    {
133
        let required = datetime::analyze_patterns(&patterns.get().0, true)
1✔
134
            .map_err(|field| DateTimeError::UnsupportedField(field.symbol))?;
×
135

136
        let req = DataRequest {
1✔
137
            locale,
138
            metadata: Default::default(),
1✔
139
        };
140

141
        let week_data = if required.week_data {
1✔
142
            Some(icu_calendar::week::WeekCalculator::try_new_unstable(
×
143
                provider, locale,
144
            )?)
×
145
        } else {
146
            None
1✔
147
        };
148

149
        let ordinal_rules = if let PatternPlurals::MultipleVariants(_) = &patterns.get().0 {
1✔
150
            Some(PluralRules::try_new_ordinal_unstable(provider, locale)?)
×
151
        } else {
×
152
            None
1✔
153
        };
154

155
        let date_symbols_data = if required.date_symbols_data {
2✔
156
            Some(symbols_data_fn()?)
1✔
157
        } else {
×
158
            None
×
159
        };
160

161
        let time_symbols_data = if required.time_symbols_data {
2✔
162
            Some(provider.load(req)?.take_payload()?)
1✔
163
        } else {
×
164
            None
×
165
        };
166

167
        let mut fixed_decimal_format_options = FixedDecimalFormatterOptions::default();
1✔
168
        fixed_decimal_format_options.grouping_strategy = GroupingStrategy::Never;
1✔
169

170
        let fixed_decimal_format = FixedDecimalFormatter::try_new_unstable(
1✔
171
            provider,
172
            locale,
173
            fixed_decimal_format_options,
174
        )?;
×
175

176
        let datetime_format = raw::DateTimeFormatter::new(
1✔
177
            patterns,
1✔
178
            date_symbols_data,
1✔
179
            time_symbols_data,
1✔
180
            week_data,
1✔
181
            ordinal_rules,
1✔
182
            fixed_decimal_format,
1✔
183
        );
184

185
        let time_zone_format = TimeZoneFormatter::try_new_for_pattern(
1✔
186
            provider,
187
            locale,
188
            datetime_format
1✔
189
                // Only dates have plural variants so we can use any of the patterns for the time segment.
190
                .patterns
191
                .clone(),
192
            &time_zone_format_options,
193
        )?;
×
194

195
        Ok(Self {
1✔
196
            datetime_format,
1✔
197
            time_zone_format,
1✔
198
        })
199
    }
1✔
200

201
    /// Takes a [`ZonedDateTimeInput`] implementer and returns an instance of a [`FormattedZonedDateTime`]
202
    /// that contains all information necessary to display a formatted zoned datetime and operate on it.
203
    #[inline]
204
    pub fn format<'l>(
1✔
205
        &'l self,
206
        date: &impl DateTimeInput,
207
        time_zone: &impl TimeZoneInput,
208
    ) -> FormattedZonedDateTime<'l> {
209
        // Todo: optimize extraction #2143
210
        FormattedZonedDateTime {
1✔
211
            zoned_datetime_format: self,
212
            datetime: ExtractedDateTimeInput::extract_from(date),
1✔
213
            time_zone: ExtractedTimeZoneInput::extract_from(time_zone),
1✔
214
        }
215
    }
1✔
216
}
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

© 2025 Coveralls, Inc