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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
// 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 ).
#[allow(unused_imports)] // feature-specific
use alloc::boxed::Box;
use icu_provider::prelude::*;
#[allow(unused_imports)] // feature-specific
use icu_provider::MaybeSendSync;
use icu_provider_adapters::empty::EmptyDataProvider;
#[allow(unused_imports)] // feature-specific
use yoke::{trait_hack::YokeTraitHack, Yokeable};
#[allow(unused_imports)] // feature-specific
use zerofrom::ZeroFrom;
pub enum ICU4XDataProviderInner {
Destroyed,
Empty,
#[cfg(feature = "compiled_data")]
Compiled,
#[cfg(feature = "buffer_provider")]
Buffer(Box<dyn BufferProvider + 'static>),
}
#[diplomat::bridge]
pub mod ffi {
use super::ICU4XDataProviderInner;
use crate::errors::ffi::ICU4XError;
use alloc::boxed::Box;
#[allow(unused_imports)] // feature-gated
use icu_provider_adapters::fallback::LocaleFallbackProvider;
#[allow(unused_imports)] // feature-gated
use icu_provider_adapters::fork::predicates::MissingLocalePredicate;
#[diplomat::opaque]
/// An ICU4X data provider, capable of loading ICU4X data keys from some source.
#[diplomat::rust_link(icu_provider, Mod)]
pub struct ICU4XDataProvider(pub ICU4XDataProviderInner);
#[cfg(feature = "buffer_provider")]
fn convert_buffer_provider<D: icu_provider::BufferProvider + 'static>(
x: D,
) -> ICU4XDataProvider {
ICU4XDataProvider(super::ICU4XDataProviderInner::Buffer(Box::new(x)))
}
impl ICU4XDataProvider {
/// Constructs an [`ICU4XDataProvider`] that uses compiled data.
///
/// Requires the `compiled_data` feature.
///
/// This provider cannot be modified or combined with other providers, so `enable_fallback`,
/// `enabled_fallback_with`, `fork_by_locale`, and `fork_by_key` will return `Err`s.
#[cfg(feature = "compiled_data")]
pub fn create_compiled() -> Box<ICU4XDataProvider> {
Box::new(Self(ICU4XDataProviderInner::Compiled))
}
/// Constructs an `FsDataProvider` and returns it as an [`ICU4XDataProvider`].
/// Requires the `provider_fs` Cargo feature.
/// Not supported in WASM.
#[diplomat::rust_link(icu_provider_fs::FsDataProvider, Struct)]
#[cfg(all(
feature = "provider_fs",
not(any(target_arch = "wasm32", target_os = "none"))
))]
#[diplomat::attr(dart, disable)]
pub fn create_fs(path: &str) -> Result<Box<ICU4XDataProvider>, ICU4XError> {
// #2520
// In the future we can start using OsString APIs to support non-utf8 paths
core::str::from_utf8(path.as_bytes())
.map_err(|e| ICU4XError::DataIoError.log_original(&e))?;
Ok(Box::new(convert_buffer_provider(
icu_provider_fs::FsDataProvider::try_new(path)?,
)))
}
/// Deprecated
///
/// Use `create_compiled()`.
#[cfg(all(
feature = "provider_test",
any(feature = "any_provider", feature = "buffer_provider")
))]
#[diplomat::attr(dart, disable)]
pub fn create_test() -> Box<ICU4XDataProvider> {
Self::create_compiled()
}
/// Constructs a `BlobDataProvider` and returns it as an [`ICU4XDataProvider`].
#[diplomat::rust_link(icu_provider_blob::BlobDataProvider, Struct)]
#[cfg(feature = "buffer_provider")]
pub fn create_from_byte_slice(
blob: &'static [u8],
) -> Result<Box<ICU4XDataProvider>, ICU4XError> {
Ok(Box::new(convert_buffer_provider(
icu_provider_blob::BlobDataProvider::try_new_from_static_blob(blob)?,
)))
}
/// Constructs an empty [`ICU4XDataProvider`].
#[diplomat::rust_link(icu_provider_adapters::empty::EmptyDataProvider, Struct)]
#[diplomat::rust_link(
icu_provider_adapters::empty::EmptyDataProvider::new,
FnInStruct,
hidden
)]
pub fn create_empty() -> Box<ICU4XDataProvider> {
Box::new(ICU4XDataProvider(ICU4XDataProviderInner::Empty))
}
/// Creates a provider that tries the current provider and then, if the current provider
/// doesn't support the data key, another provider `other`.
///
/// This takes ownership of the `other` provider, leaving an empty provider in its place.
///
/// The providers must be the same type (Any or Buffer). This condition is satisfied if
/// both providers originate from the same constructor, such as `create_from_byte_slice`
/// or `create_fs`. If the condition is not upheld, a runtime error occurs.
#[diplomat::rust_link(icu_provider_adapters::fork::ForkByKeyProvider, Typedef)]
#[diplomat::rust_link(
icu_provider_adapters::fork::predicates::MissingDataKeyPredicate,
Struct,
hidden
)]
pub fn fork_by_key(&mut self, other: &mut ICU4XDataProvider) -> Result<(), ICU4XError> {
#[allow(unused_imports)]
use ICU4XDataProviderInner::*;
*self = match (
core::mem::replace(&mut self.0, Destroyed),
core::mem::replace(&mut other.0, Destroyed),
) {
(Destroyed, _) | (_, Destroyed) => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
#[cfg(feature = "compiled_data")]
(Compiled, _) | (_, Compiled) => Err(icu_provider::DataError::custom(
"The compiled provider cannot be modified",
))?,
(Empty, Empty) => ICU4XDataProvider(ICU4XDataProviderInner::Empty),
#[cfg(feature = "buffer_provider")]
(Empty, b) | (b, Empty) => ICU4XDataProvider(b),
#[cfg(feature = "buffer_provider")]
(Buffer(a), Buffer(b)) => convert_buffer_provider(
icu_provider_adapters::fork::ForkByKeyProvider::new(a, b),
),
};
Ok(())
}
/// Same as `fork_by_key` but forks by locale instead of key.
#[diplomat::rust_link(
icu_provider_adapters::fork::predicates::MissingLocalePredicate,
Struct
)]
pub fn fork_by_locale(&mut self, other: &mut ICU4XDataProvider) -> Result<(), ICU4XError> {
#[allow(unused_imports)]
use ICU4XDataProviderInner::*;
*self = match (
core::mem::replace(&mut self.0, Destroyed),
core::mem::replace(&mut other.0, Destroyed),
) {
(Destroyed, _) | (_, Destroyed) => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
#[cfg(feature = "compiled_data")]
(Compiled, _) | (_, Compiled) => Err(icu_provider::DataError::custom(
"The compiled provider cannot be modified",
))?,
(Empty, Empty) => ICU4XDataProvider(ICU4XDataProviderInner::Empty),
#[cfg(feature = "buffer_provider")]
(Empty, b) | (b, Empty) => ICU4XDataProvider(b),
#[cfg(feature = "buffer_provider")]
(Buffer(a), Buffer(b)) => convert_buffer_provider(
icu_provider_adapters::fork::ForkByErrorProvider::new_with_predicate(
a,
b,
MissingLocalePredicate,
),
),
};
Ok(())
}
/// Enables locale fallbacking for data requests made to this provider.
///
/// Note that the test provider (from `create_test`) already has fallbacking enabled.
#[diplomat::rust_link(
icu_provider_adapters::fallback::LocaleFallbackProvider::try_new,
FnInStruct
)]
#[diplomat::rust_link(
icu_provider_adapters::fallback::LocaleFallbackProvider,
Struct,
compact
)]
pub fn enable_locale_fallback(&mut self) -> Result<(), ICU4XError> {
use ICU4XDataProviderInner::*;
*self = match core::mem::replace(&mut self.0, Destroyed) {
Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
#[cfg(feature = "compiled_data")]
Compiled => Err(icu_provider::DataError::custom(
"The compiled provider cannot be modified",
))?,
Empty => Err(icu_provider::DataErrorKind::MissingDataKey.into_error())?,
#[cfg(feature = "buffer_provider")]
Buffer(inner) => convert_buffer_provider(
LocaleFallbackProvider::try_new_with_buffer_provider(inner)?,
),
};
Ok(())
}
#[diplomat::rust_link(
icu_provider_adapters::fallback::LocaleFallbackProvider::new_with_fallbacker,
FnInStruct
)]
#[diplomat::rust_link(
icu_provider_adapters::fallback::LocaleFallbackProvider,
Struct,
compact
)]
#[allow(unused_variables)] // feature-gated
#[cfg(feature = "icu_locid_transform")]
pub fn enable_locale_fallback_with(
&mut self,
fallbacker: &crate::fallbacker::ffi::ICU4XLocaleFallbacker,
) -> Result<(), ICU4XError> {
use ICU4XDataProviderInner::*;
*self = match core::mem::replace(&mut self.0, Destroyed) {
Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
#[cfg(feature = "compiled_data")]
Compiled => Err(icu_provider::DataError::custom(
"The compiled provider cannot be modified",
))?,
Empty => Err(icu_provider::DataErrorKind::MissingDataKey.into_error())?,
#[cfg(feature = "buffer_provider")]
Buffer(inner) => convert_buffer_provider(
LocaleFallbackProvider::new_with_fallbacker(inner, fallbacker.0.clone()),
),
};
Ok(())
}
}
}
macro_rules! load {
() => {
fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
use ICU4XDataProviderInner::*;
match self {
Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
Empty => EmptyDataProvider::new().load(req),
#[cfg(feature = "buffer_provider")]
Buffer(buffer_provider) => buffer_provider.as_deserializing().load(req),
#[cfg(feature = "compiled_data")]
Compiled => unreachable!(),
}
}
};
}
#[macro_export]
macro_rules! call_constructor {
($compiled:path [$pre_transform:ident => $transform:expr], $any:path, $buffer:path, $provider:expr $(, $args:expr)* $(,)?) => {
match &$provider.0 {
$crate::provider::ICU4XDataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
$crate::provider::ICU4XDataProviderInner::Empty => $any(&icu_provider_adapters::empty::EmptyDataProvider::new(), $($args,)*),
#[cfg(feature = "buffer_provider")]
$crate::provider::ICU4XDataProviderInner::Buffer(buffer_provider) => $buffer(buffer_provider, $($args,)*),
#[cfg(feature = "compiled_data")]
$crate::provider::ICU4XDataProviderInner::Compiled => { let $pre_transform = $compiled($($args,)*); $transform },
}
};
($compiled:path, $any:path, $buffer:path, $provider:expr $(, $args:expr)* $(,)?) => {
match &$provider.0 {
$crate::provider::ICU4XDataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
$crate::provider::ICU4XDataProviderInner::Empty => $any(&icu_provider_adapters::empty::EmptyDataProvider::new(), $($args,)*),
#[cfg(feature = "buffer_provider")]
$crate::provider::ICU4XDataProviderInner::Buffer(buffer_provider) => $buffer(buffer_provider, $($args,)*),
#[cfg(feature = "compiled_data")]
$crate::provider::ICU4XDataProviderInner::Compiled => $compiled($($args,)*),
}
};
}
#[macro_export]
macro_rules! call_constructor_unstable {
($compiled:path [$pre_transform:ident => $transform:expr], $unstable:path, $provider:expr $(, $args:expr)* $(,)?) => {
match &$provider.0 {
$crate::provider::ICU4XDataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
$crate::provider::ICU4XDataProviderInner::Empty => $unstable(&icu_provider_adapters::empty::EmptyDataProvider::new(), $($args,)*),
#[cfg(feature = "buffer_provider")]
$crate::provider::ICU4XDataProviderInner::Buffer(buffer_provider) => $unstable(&icu_provider::AsDeserializingBufferProvider::as_deserializing(buffer_provider), $($args,)*),
#[cfg(feature = "compiled_data")]
$crate::provider::ICU4XDataProviderInner::Compiled => { let $pre_transform = $compiled($($args,)*); $transform },
}
};
($compiled:path, $unstable:path, $provider:expr $(, $args:expr)* $(,)?) => {
match &$provider.0 {
$crate::provider::ICU4XDataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
$crate::provider::ICU4XDataProviderInner::Empty => $unstable(&icu_provider_adapters::empty::EmptyDataProvider::new(), $($args,)*),
#[cfg(feature = "buffer_provider")]
$crate::provider::ICU4XDataProviderInner::Buffer(buffer_provider) => $unstable(&icu_provider::AsDeserializingBufferProvider::as_deserializing(buffer_provider), $($args,)*),
#[cfg(feature = "compiled_data")]
$crate::provider::ICU4XDataProviderInner::Compiled => $compiled($($args,)*),
}
};
}
#[cfg(not(feature = "buffer_provider"))]
impl<M> DataProvider<M> for ICU4XDataProviderInner
where
M: KeyedDataMarker,
{
load!();
}
#[cfg(feature = "buffer_provider")]
impl<M> DataProvider<M> for ICU4XDataProviderInner
where
M: KeyedDataMarker,
// Actual bound:
// for<'de> <M::Yokeable as Yokeable<'de>>::Output: Deserialize<'de>,
// Necessary workaround bound (see `yoke::trait_hack` docs):
for<'de> YokeTraitHack<<M::Yokeable as Yokeable<'de>>::Output>: serde::Deserialize<'de>,
{
load!();
}
|