Debian Codegraph @

debian/rust-vendor/icu_datetime/src/external_loaders.rs

raw
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 //! Internal traits and structs for loading data from other crates.
6
7 use icu_calendar::preferences::CalendarPreferences;
8 use icu_decimal::options::DecimalFormatterOptions;
9 use icu_decimal::{DecimalFormatter, DecimalFormatterPreferences};
10 use icu_provider::prelude::*;
11
12 use crate::scaffold::FormattableAnyCalendar;
13
14 /// Trait for loading a [`DecimalFormatter`].
15 ///
16 /// Implemented on the provider-specific loader types in this module.
17 pub(crate) trait DecimalFormatterLoader {
18 fn load(
19 &self,
20 prefs: DecimalFormatterPreferences,
21 options: DecimalFormatterOptions,
22 ) -> Result<DecimalFormatter, DataError>;
23 }
24
25 /// Trait for loading an [`AnyCalendar`](icu_calendar::AnyCalendar).
26 ///
27 /// Implemented on the provider-specific loader types in this module.
28 pub(crate) trait FormattableAnyCalendarLoader {
29 fn load(&self, prefs: CalendarPreferences) -> Result<FormattableAnyCalendar, DataError>;
30 }
31
32 /// Loader for types from other crates using compiled data.
33 #[cfg(feature = "compiled_data")]
34 pub(crate) struct ExternalLoaderCompiledData;
35
36 #[cfg(feature = "compiled_data")]
37 impl DecimalFormatterLoader for ExternalLoaderCompiledData {
38 #[inline]
39 fn load(
40 &self,
41 prefs: DecimalFormatterPreferences,
42 options: DecimalFormatterOptions,
43 ) -> Result<DecimalFormatter, DataError> {
44 DecimalFormatter::try_new(prefs, options)
45 }
46 }
47
48 #[cfg(feature = "compiled_data")]
49 impl FormattableAnyCalendarLoader for ExternalLoaderCompiledData {
50 #[inline]
51 fn load(&self, prefs: CalendarPreferences) -> Result<FormattableAnyCalendar, DataError> {
52 FormattableAnyCalendar::try_new(prefs)
53 }
54 }
55
56 /// Loader for types from other crates using [`BufferProvider`].
57 #[cfg(feature = "serde")]
58 pub(crate) struct ExternalLoaderBuffer<'a, P: ?Sized>(pub &'a P);
59
60 #[cfg(feature = "serde")]
61 impl<P> DecimalFormatterLoader for ExternalLoaderBuffer<'_, P>
62 where
63 P: ?Sized + BufferProvider,
64 {
65 #[inline]
66 fn load(
67 &self,
68 prefs: DecimalFormatterPreferences,
69 options: DecimalFormatterOptions,
70 ) -> Result<DecimalFormatter, DataError> {
71 DecimalFormatter::try_new_with_buffer_provider(self.0, prefs, options)
72 }
73 }
74
75 #[cfg(feature = "serde")]
76 impl<P> FormattableAnyCalendarLoader for ExternalLoaderBuffer<'_, P>
77 where
78 P: ?Sized + BufferProvider,
79 {
80 #[inline]
81 fn load(&self, prefs: CalendarPreferences) -> Result<FormattableAnyCalendar, DataError> {
82 FormattableAnyCalendar::try_new_with_buffer_provider(self.0, prefs)
83 }
84 }
85
86 /// Loader for types from other crates using [`DataProvider`].
87 pub(crate) struct ExternalLoaderUnstable<'a, P: ?Sized>(pub &'a P);
88
89 impl<P> DecimalFormatterLoader for ExternalLoaderUnstable<'_, P>
90 where
91 P: ?Sized
92 + DataProvider<icu_decimal::provider::DecimalSymbolsV1>
93 + DataProvider<icu_decimal::provider::DecimalDigitsV1>,
94 {
95 #[inline]
96 fn load(
97 &self,
98 prefs: DecimalFormatterPreferences,
99 options: DecimalFormatterOptions,
100 ) -> Result<DecimalFormatter, DataError> {
101 DecimalFormatter::try_new_unstable(self.0, prefs, options)
102 }
103 }
104
105 impl<P> FormattableAnyCalendarLoader for ExternalLoaderUnstable<'_, P>
106 where
107 P: DataProvider<icu_calendar::provider::CalendarJapaneseModernV1>
108 + DataProvider<icu_calendar::provider::CalendarPreferredV1>
109 + ?Sized,
110 {
111 #[inline]
112 fn load(&self, prefs: CalendarPreferences) -> Result<FormattableAnyCalendar, DataError> {
113 FormattableAnyCalendar::try_new_unstable(self.0, prefs)
114 }
115 }