icu_datetime/
external_loaders.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Internal traits and structs for loading data from other crates.

use icu_calendar::{AnyCalendar, AnyCalendarPreferences};
use icu_decimal::options::FixedDecimalFormatterOptions;
use icu_decimal::{FixedDecimalFormatter, FixedDecimalFormatterPreferences};
use icu_provider::prelude::*;

/// Trait for loading a FixedDecimalFormatter.
///
/// Implemented on the provider-specific loader types in this module.
pub(crate) trait FixedDecimalFormatterLoader {
    fn load(
        &self,
        prefs: FixedDecimalFormatterPreferences,
        options: FixedDecimalFormatterOptions,
    ) -> Result<FixedDecimalFormatter, DataError>;
}

/// Trait for loading an AnyCalendar.
///
/// Implemented on the provider-specific loader types in this module.
pub(crate) trait AnyCalendarLoader {
    fn load(&self, prefs: AnyCalendarPreferences) -> Result<AnyCalendar, DataError>;
}

/// Loader for types from other crates using compiled data.
#[cfg(feature = "compiled_data")]
pub(crate) struct ExternalLoaderCompiledData;

#[cfg(feature = "compiled_data")]
impl FixedDecimalFormatterLoader for ExternalLoaderCompiledData {
    #[inline]
    fn load(
        &self,
        prefs: FixedDecimalFormatterPreferences,
        options: FixedDecimalFormatterOptions,
    ) -> Result<FixedDecimalFormatter, DataError> {
        FixedDecimalFormatter::try_new(prefs, options)
    }
}

#[cfg(feature = "compiled_data")]
impl AnyCalendarLoader for ExternalLoaderCompiledData {
    #[inline]
    fn load(&self, prefs: AnyCalendarPreferences) -> Result<AnyCalendar, DataError> {
        AnyCalendar::try_new(prefs)
    }
}

/// Loader for types from other crates using [`AnyProvider`].
pub(crate) struct ExternalLoaderAny<'a, P: ?Sized>(pub &'a P);

impl<P> FixedDecimalFormatterLoader for ExternalLoaderAny<'_, P>
where
    P: ?Sized + AnyProvider,
{
    #[inline]
    fn load(
        &self,
        prefs: FixedDecimalFormatterPreferences,
        options: FixedDecimalFormatterOptions,
    ) -> Result<FixedDecimalFormatter, DataError> {
        FixedDecimalFormatter::try_new_with_any_provider(self.0, prefs, options)
    }
}

impl<P> AnyCalendarLoader for ExternalLoaderAny<'_, P>
where
    P: ?Sized + AnyProvider,
{
    #[inline]
    fn load(&self, prefs: AnyCalendarPreferences) -> Result<AnyCalendar, DataError> {
        AnyCalendar::try_new_with_any_provider(self.0, prefs)
    }
}

/// Loader for types from other crates using [`BufferProvider`].
#[cfg(feature = "serde")]
pub(crate) struct ExternalLoaderBuffer<'a, P: ?Sized>(pub &'a P);

#[cfg(feature = "serde")]
impl<P> FixedDecimalFormatterLoader for ExternalLoaderBuffer<'_, P>
where
    P: ?Sized + BufferProvider,
{
    #[inline]
    fn load(
        &self,
        prefs: FixedDecimalFormatterPreferences,
        options: FixedDecimalFormatterOptions,
    ) -> Result<FixedDecimalFormatter, DataError> {
        FixedDecimalFormatter::try_new_with_buffer_provider(self.0, prefs, options)
    }
}

#[cfg(feature = "serde")]
impl<P> AnyCalendarLoader for ExternalLoaderBuffer<'_, P>
where
    P: ?Sized + BufferProvider,
{
    #[inline]
    fn load(&self, prefs: AnyCalendarPreferences) -> Result<AnyCalendar, DataError> {
        AnyCalendar::try_new_with_buffer_provider(self.0, prefs)
    }
}

/// Loader for types from other crates using [`DataProvider`].
pub(crate) struct ExternalLoaderUnstable<'a, P: ?Sized>(pub &'a P);

impl<P> FixedDecimalFormatterLoader for ExternalLoaderUnstable<'_, P>
where
    P: ?Sized
        + DataProvider<icu_decimal::provider::DecimalSymbolsV2Marker>
        + DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>,
{
    #[inline]
    fn load(
        &self,
        prefs: FixedDecimalFormatterPreferences,
        options: FixedDecimalFormatterOptions,
    ) -> Result<FixedDecimalFormatter, DataError> {
        FixedDecimalFormatter::try_new_unstable(self.0, prefs, options)
    }
}

impl<P> AnyCalendarLoader for ExternalLoaderUnstable<'_, P>
where
    P: DataProvider<icu_calendar::provider::JapaneseErasV1Marker>
        + DataProvider<icu_calendar::provider::JapaneseExtendedErasV1Marker>
        + DataProvider<icu_calendar::provider::ChineseCacheV1Marker>
        + DataProvider<icu_calendar::provider::DangiCacheV1Marker>
        + DataProvider<icu_calendar::provider::IslamicObservationalCacheV1Marker>
        + DataProvider<icu_calendar::provider::IslamicUmmAlQuraCacheV1Marker>
        + ?Sized,
{
    #[inline]
    fn load(&self, prefs: AnyCalendarPreferences) -> Result<AnyCalendar, DataError> {
        AnyCalendar::try_new_unstable(self.0, prefs)
    }
}