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
|
// 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 ).
use crate::error::DataError;
use crate::key::DataKey;
use crate::marker::{DataMarker, KeyedDataMarker};
use crate::request::DataRequest;
use crate::response::DataResponse;
/// A data provider that loads data for a specific data type.
///
/// Unlike [`DataProvider`], there may be multiple keys corresponding to the same data type.
/// This is often the case when returning `dyn` trait objects such as [`AnyMarker`].
///
/// [`AnyMarker`]: crate::any::AnyMarker
pub trait DynamicDataProvider<M>
where
M: DataMarker,
{
/// Query the provider for data, returning the result.
///
/// Returns [`Ok`] if the request successfully loaded data. If data failed to load, returns an
/// Error with more information.
fn load_data(&self, key: DataKey, req: DataRequest) -> Result<DataResponse<M>, DataError>;
}
/// A data provider that loads data for a specific [`DataKey`].
pub trait DataProvider<M>
where
M: KeyedDataMarker,
{
/// Query the provider for data, returning the result.
///
/// Returns [`Ok`] if the request successfully loaded data. If data failed to load, returns an
/// Error with more information.
fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError>;
}
impl<M, P> DynamicDataProvider<M> for alloc::boxed::Box<P>
where
M: DataMarker,
P: DynamicDataProvider<M> + ?Sized,
{
fn load_data(&self, key: DataKey, req: DataRequest) -> Result<DataResponse<M>, DataError> {
(**self).load_data(key, req)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::hello_world::*;
use crate::prelude::*;
use alloc::borrow::Cow;
use alloc::string::String;
use core::fmt::Debug;
use serde::{Deserialize, Serialize};
// This tests DataProvider borrow semantics with a dummy data provider based on a
// JSON string. It also exercises most of the data provider code paths.
/// Key for HelloAlt, used for testing mismatched types
const HELLO_ALT_KEY: DataKey = crate::data_key!("core/helloalt@1");
/// A data struct serialization-compatible with HelloWorldV1 used for testing mismatched types
#[derive(
Serialize, Deserialize, Debug, Clone, Default, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom,
)]
struct HelloAlt {
#[zerofrom(clone)]
message: String,
}
/// Marker type for [`HelloAlt`].
struct HelloAltMarker {}
impl DataMarker for HelloAltMarker {
type Yokeable = HelloAlt;
}
impl KeyedDataMarker for HelloAltMarker {
const KEY: DataKey = HELLO_ALT_KEY;
}
#[derive(Deserialize, Debug, Clone, Default, PartialEq)]
struct HelloCombined<'data> {
#[serde(borrow)]
pub hello_v1: HelloWorldV1<'data>,
pub hello_alt: HelloAlt,
}
/// A DataProvider that owns its data, returning an Rc-variant DataPayload.
/// Supports only key::HELLO_WORLD_V1. Uses `impl_dynamic_data_provider!()`.
#[derive(Debug)]
struct DataWarehouse {
hello_v1: HelloWorldV1<'static>,
hello_alt: HelloAlt,
}
impl DataProvider<HelloWorldV1Marker> for DataWarehouse {
fn load(&self, _: DataRequest) -> Result<DataResponse<HelloWorldV1Marker>, DataError> {
Ok(DataResponse {
metadata: DataResponseMetadata::default(),
payload: Some(DataPayload::from_owned(self.hello_v1.clone())),
})
}
}
crate::impl_dynamic_data_provider!(DataWarehouse, [HelloWorldV1Marker,], AnyMarker);
/// A DataProvider that supports both key::HELLO_WORLD_V1 and HELLO_ALT.
#[derive(Debug)]
struct DataProvider2 {
data: DataWarehouse,
}
impl From<DataWarehouse> for DataProvider2 {
fn from(warehouse: DataWarehouse) -> Self {
DataProvider2 { data: warehouse }
}
}
impl DataProvider<HelloWorldV1Marker> for DataProvider2 {
fn load(&self, _: DataRequest) -> Result<DataResponse<HelloWorldV1Marker>, DataError> {
Ok(DataResponse {
metadata: DataResponseMetadata::default(),
payload: Some(DataPayload::from_owned(self.data.hello_v1.clone())),
})
}
}
impl DataProvider<HelloAltMarker> for DataProvider2 {
fn load(&self, _: DataRequest) -> Result<DataResponse<HelloAltMarker>, DataError> {
Ok(DataResponse {
metadata: DataResponseMetadata::default(),
payload: Some(DataPayload::from_owned(self.data.hello_alt.clone())),
})
}
}
crate::impl_dynamic_data_provider!(
DataProvider2,
[HelloWorldV1Marker, HelloAltMarker,],
AnyMarker
);
const DATA: &str = r#"{
"hello_v1": {
"message": "Hello V1"
},
"hello_alt": {
"message": "Hello Alt"
}
}"#;
fn get_warehouse(data: &'static str) -> DataWarehouse {
let data: HelloCombined = serde_json::from_str(data).expect("Well-formed data");
DataWarehouse {
hello_v1: data.hello_v1,
hello_alt: data.hello_alt,
}
}
fn get_payload_v1<P: DataProvider<HelloWorldV1Marker> + ?Sized>(
provider: &P,
) -> Result<DataPayload<HelloWorldV1Marker>, DataError> {
provider.load(Default::default())?.take_payload()
}
fn get_payload_alt<P: DataProvider<HelloAltMarker> + ?Sized>(
provider: &P,
) -> Result<DataPayload<HelloAltMarker>, DataError> {
provider.load(Default::default())?.take_payload()
}
#[test]
fn test_warehouse_owned() {
let warehouse = get_warehouse(DATA);
let hello_data = get_payload_v1(&warehouse).unwrap();
assert!(matches!(
hello_data.get(),
HelloWorldV1 {
message: Cow::Borrowed(_),
}
));
}
#[test]
fn test_warehouse_owned_dyn_erased() {
let warehouse = get_warehouse(DATA);
let hello_data = get_payload_v1(&warehouse.as_any_provider().as_downcasting()).unwrap();
assert!(matches!(
hello_data.get(),
HelloWorldV1 {
message: Cow::Borrowed(_),
}
));
}
#[test]
fn test_warehouse_owned_dyn_generic() {
let warehouse = get_warehouse(DATA);
let hello_data =
get_payload_v1(&warehouse as &dyn DataProvider<HelloWorldV1Marker>).unwrap();
assert!(matches!(
hello_data.get(),
HelloWorldV1 {
message: Cow::Borrowed(_),
}
));
}
#[test]
fn test_warehouse_owned_dyn_erased_alt() {
let warehouse = get_warehouse(DATA);
let response = get_payload_alt(&warehouse.as_any_provider().as_downcasting());
assert!(matches!(
response,
Err(DataError {
kind: DataErrorKind::MissingDataKey,
..
})
));
}
#[test]
fn test_provider2() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
let hello_data = get_payload_v1(&provider).unwrap();
assert!(matches!(
hello_data.get(),
HelloWorldV1 {
message: Cow::Borrowed(_),
}
));
}
#[test]
fn test_provider2_dyn_erased() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
let hello_data = get_payload_v1(&provider.as_any_provider().as_downcasting()).unwrap();
assert!(matches!(
hello_data.get(),
HelloWorldV1 {
message: Cow::Borrowed(_),
}
));
}
#[test]
fn test_provider2_dyn_erased_alt() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
let hello_data = get_payload_alt(&provider.as_any_provider().as_downcasting()).unwrap();
assert!(matches!(hello_data.get(), HelloAlt { .. }));
}
#[test]
fn test_provider2_dyn_generic() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
let hello_data =
get_payload_v1(&provider as &dyn DataProvider<HelloWorldV1Marker>).unwrap();
assert!(matches!(
hello_data.get(),
HelloWorldV1 {
message: Cow::Borrowed(_),
}
));
}
#[test]
fn test_provider2_dyn_generic_alt() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
let hello_data = get_payload_alt(&provider as &dyn DataProvider<HelloAltMarker>).unwrap();
assert!(matches!(hello_data.get(), HelloAlt { .. }));
}
#[test]
fn test_mismatched_types() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
// Request is for v2, but type argument is for v1
let response: Result<DataResponse<HelloWorldV1Marker>, DataError> = AnyProvider::load_any(
&provider.as_any_provider(),
HELLO_ALT_KEY,
Default::default(),
)
.unwrap()
.downcast();
assert!(matches!(
response,
Err(DataError {
kind: DataErrorKind::MismatchedType(_),
..
})
));
}
fn check_v1_v2<P>(d: &P)
where
P: DataProvider<HelloWorldV1Marker> + DataProvider<HelloAltMarker> + ?Sized,
{
let v1: DataPayload<HelloWorldV1Marker> =
d.load(Default::default()).unwrap().take_payload().unwrap();
let v2: DataPayload<HelloAltMarker> =
d.load(Default::default()).unwrap().take_payload().unwrap();
if v1.get().message == v2.get().message {
panic!()
}
}
#[test]
fn test_v1_v2_generic() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
check_v1_v2(&provider);
}
#[test]
fn test_v1_v2_dyn_erased() {
let warehouse = get_warehouse(DATA);
let provider = DataProvider2::from(warehouse);
check_v1_v2(&provider.as_any_provider().as_downcasting());
}
}
|