Skip to content

Commit 3524f8e

Browse files
Add DRY to baked providers (#5544)
#58
1 parent 02153c6 commit 3524f8e

File tree

597 files changed

+4245
-6999
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

597 files changed

+4245
-6999
lines changed

provider/baked/src/export.rs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl BakedExporter {
309309
marker: DataMarkerInfo,
310310
stats: Statistics,
311311
body: TokenStream,
312-
dry_body: TokenStream,
312+
dry_body: Option<TokenStream>,
313313
iterable_body: TokenStream,
314314
) -> Result<(), DataError> {
315315
let marker_unqualified = bake_marker(marker).into_iter().last().unwrap().to_string();
@@ -356,6 +356,28 @@ impl BakedExporter {
356356

357357
let maybe_msrv = maybe_msrv();
358358

359+
let dry = if let Some(dry_body) = dry_body {
360+
quote! {
361+
($provider:ty, DRY) => {
362+
#prefixed_macro_ident!($provider);
363+
#dry_body
364+
};
365+
($provider:ty, DRY, ITER) => {
366+
#prefixed_macro_ident!($provider);
367+
#dry_body
368+
#iterable_body
369+
};
370+
}
371+
} else {
372+
quote! {
373+
($provider:ty, DRY) => {
374+
};
375+
($provider:ty, DRY, ITER) => {
376+
#prefixed_macro_ident!($provider, ITER);
377+
};
378+
}
379+
};
380+
359381
self.write_to_file(
360382
Path::new(&format!("{ident}.rs.data")),
361383
quote! {
@@ -368,19 +390,11 @@ impl BakedExporter {
368390
const _: () = <$provider>::MUST_USE_MAKE_PROVIDER_MACRO;
369391
#body
370392
};
371-
($provider:ty, DRY) => {
372-
#prefixed_macro_ident!($provider);
373-
#dry_body
374-
};
375393
($provider:ty, ITER) => {
376394
#prefixed_macro_ident!($provider);
377395
#iterable_body
378396
};
379-
($provider:ty, DRY, ITER) => {
380-
#prefixed_macro_ident!($provider);
381-
#dry_body
382-
#iterable_body
383-
};
397+
#dry
384398
}
385399
#[doc(inline)]
386400
pub use #prefixed_macro_ident as #macro_ident;
@@ -417,6 +431,7 @@ impl DataExporter for BakedExporter {
417431
&self,
418432
marker: DataMarkerInfo,
419433
payload: &DataPayload<ExportMarker>,
434+
_metadata: FlushMetadata,
420435
) -> Result<(), DataError> {
421436
let maybe_msrv = maybe_msrv();
422437

@@ -469,7 +484,7 @@ impl DataExporter for BakedExporter {
469484
}
470485
}
471486
},
472-
quote! {
487+
Some(quote! {
473488
#maybe_msrv
474489
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
475490
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
@@ -480,7 +495,7 @@ impl DataExporter for BakedExporter {
480495
}
481496
}
482497
}
483-
},
498+
}),
484499
quote! {
485500
#maybe_msrv
486501
impl icu_provider::IterableDataProvider<#marker_bake> for $provider {
@@ -491,7 +506,7 @@ impl DataExporter for BakedExporter {
491506
})
492507
}
493508

494-
fn flush(&self, marker: DataMarkerInfo) -> Result<(), DataError> {
509+
fn flush(&self, marker: DataMarkerInfo, metadata: FlushMetadata) -> Result<(), DataError> {
495510
let maybe_msrv = maybe_msrv();
496511

497512
let marker_bake = bake_marker(marker);
@@ -518,14 +533,14 @@ impl DataExporter for BakedExporter {
518533
}
519534
}
520535
},
521-
quote! {
536+
Some(quote! {
522537
#maybe_msrv
523538
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
524539
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
525540
Err(icu_provider::DataErrorKind::IdentifierNotFound.with_req(<#marker_bake as icu_provider::DataMarker>::INFO, req))
526541
}
527542
}
528-
},
543+
}),
529544
quote! {
530545
#maybe_msrv
531546
impl icu_provider::IterableDataProvider<#marker_bake> for $provider {
@@ -633,13 +648,17 @@ impl DataExporter for BakedExporter {
633648
}
634649
}
635650
},
636-
quote! {
637-
#maybe_msrv
638-
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
639-
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
640-
icu_provider::DataProvider::<#marker_bake>::load(self, req).map(|r| r.metadata)
651+
if metadata.supports_dry_provider {
652+
Some(quote! {
653+
#maybe_msrv
654+
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
655+
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
656+
icu_provider::DataProvider::<#marker_bake>::load(self, req).map(|r| r.metadata)
657+
}
641658
}
642-
}
659+
})
660+
} else {
661+
None
643662
},
644663
quote! {
645664
#maybe_msrv

provider/baked/tests/data/hello_world_v1_marker.rs.data

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ macro_rules! __impl_hello_world_v1_marker {
2727
}
2828
}
2929
};
30-
($ provider : ty , DRY) => {
30+
($ provider : ty , ITER) => {
3131
__impl_hello_world_v1_marker!($provider);
3232
#[clippy::msrv = "1.71.1"]
33-
impl icu_provider::DryDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
34-
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
35-
icu_provider::DataProvider::<icu_provider::hello_world::HelloWorldV1Marker>::load(self, req).map(|r| r.metadata)
33+
impl icu_provider::IterableDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
34+
fn iter_ids(&self) -> Result<std::collections::BTreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
35+
Ok(icu_provider_baked::DataStore::iter(&Self::DATA_HELLO_WORLD_V1_MARKER).collect())
3636
}
3737
}
3838
};
39-
($ provider : ty , ITER) => {
39+
($ provider : ty , DRY) => {
4040
__impl_hello_world_v1_marker!($provider);
4141
#[clippy::msrv = "1.71.1"]
42-
impl icu_provider::IterableDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
43-
fn iter_ids(&self) -> Result<std::collections::BTreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
44-
Ok(icu_provider_baked::DataStore::iter(&Self::DATA_HELLO_WORLD_V1_MARKER).collect())
42+
impl icu_provider::DryDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
43+
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
44+
icu_provider::DataProvider::<icu_provider::hello_world::HelloWorldV1Marker>::load(self, req).map(|r| r.metadata)
4545
}
4646
}
4747
};

provider/blob/benches/auxkey_bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ where
267267
)
268268
.unwrap();
269269
}
270-
exporter.flush(M::INFO).unwrap();
270+
exporter.flush(M::INFO, Default::default()).unwrap();
271271
}
272272

273273
fn make_blob_v1() -> Vec<u8> {

provider/blob/src/blob_data_provider.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ mod test {
181181
BlobExporter::new_v2_with_sink(Box::new(&mut blob))
182182
};
183183

184-
exporter.flush(HelloWorldV1Marker::INFO).unwrap();
184+
exporter
185+
.flush(HelloWorldV1Marker::INFO, Default::default())
186+
.unwrap();
185187

186188
exporter.close().unwrap();
187189
}
@@ -213,7 +215,9 @@ mod test {
213215
BlobExporter::new_v2_with_sink(Box::new(&mut blob))
214216
};
215217

216-
exporter.flush(HelloSingletonV1Marker::INFO).unwrap();
218+
exporter
219+
.flush(HelloSingletonV1Marker::INFO, Default::default())
220+
.unwrap();
217221

218222
exporter.close().unwrap();
219223
}

provider/blob/src/export/blob_exporter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl DataExporter for BlobExporter<'_> {
119119
Ok(())
120120
}
121121

122-
fn flush(&self, marker: DataMarkerInfo) -> Result<(), DataError> {
122+
fn flush(&self, marker: DataMarkerInfo, _metadata: FlushMetadata) -> Result<(), DataError> {
123123
self.all_markers
124124
.lock()
125125
.expect("poison")

provider/blob/tests/test_versions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ where
3434
)
3535
.unwrap();
3636
}
37-
exporter.flush(HelloWorldV1Marker::INFO).unwrap();
37+
exporter
38+
.flush(HelloWorldV1Marker::INFO, Default::default())
39+
.unwrap();
3840
exporter.close().unwrap();
3941
}
4042

provider/core/src/export/mod.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::collections::HashSet;
1717
/// An object capable of exporting data payloads in some form.
1818
pub trait DataExporter: Sync {
1919
/// Save a `payload` corresponding to the given marker and locale.
20+
///
2021
/// Takes non-mut self as it can be called concurrently.
2122
fn put_payload(
2223
&self,
@@ -26,21 +27,22 @@ pub trait DataExporter: Sync {
2627
) -> Result<(), DataError>;
2728

2829
/// Function called for singleton markers.
30+
///
2931
/// Takes non-mut self as it can be called concurrently.
3032
fn flush_singleton(
3133
&self,
3234
marker: DataMarkerInfo,
3335
payload: &DataPayload<ExportMarker>,
36+
metadata: FlushMetadata,
3437
) -> Result<(), DataError> {
3538
self.put_payload(marker, Default::default(), payload)?;
36-
self.flush(marker)
39+
self.flush(marker, metadata)
3740
}
3841

3942
/// Function called after a non-singleton marker has been fully enumerated.
40-
/// Does not include built-in fallback.
4143
///
4244
/// Takes non-mut self as it can be called concurrently.
43-
fn flush(&self, _marker: DataMarkerInfo) -> Result<(), DataError> {
45+
fn flush(&self, _marker: DataMarkerInfo, _metadata: FlushMetadata) -> Result<(), DataError> {
4446
Ok(())
4547
}
4648

@@ -52,6 +54,15 @@ pub trait DataExporter: Sync {
5254
}
5355
}
5456

57+
/// Metadata for [`DataExporter::flush`]
58+
#[non_exhaustive]
59+
#[derive(Debug, Copy, Clone, Default)]
60+
pub struct FlushMetadata {
61+
/// Whether the data was generated in such a way that a [`DryDataProvider`] implementation
62+
/// makes sense.
63+
pub supports_dry_provider: bool,
64+
}
65+
5566
impl DataExporter for Box<dyn DataExporter> {
5667
fn put_payload(
5768
&self,
@@ -66,12 +77,13 @@ impl DataExporter for Box<dyn DataExporter> {
6677
&self,
6778
marker: DataMarkerInfo,
6879
payload: &DataPayload<ExportMarker>,
80+
metadata: FlushMetadata,
6981
) -> Result<(), DataError> {
70-
(**self).flush_singleton(marker, payload)
82+
(**self).flush_singleton(marker, payload, metadata)
7183
}
7284

73-
fn flush(&self, marker: DataMarkerInfo) -> Result<(), DataError> {
74-
(**self).flush(marker)
85+
fn flush(&self, marker: DataMarkerInfo, metadata: FlushMetadata) -> Result<(), DataError> {
86+
(**self).flush(marker, metadata)
7587
}
7688

7789
fn close(&mut self) -> Result<(), DataError> {
@@ -170,14 +182,15 @@ impl DataExporter for MultiExporter {
170182
&self,
171183
marker: DataMarkerInfo,
172184
payload: &DataPayload<ExportMarker>,
185+
metadata: FlushMetadata,
173186
) -> Result<(), DataError> {
174187
self.0
175188
.iter()
176-
.try_for_each(|e| e.flush_singleton(marker, payload))
189+
.try_for_each(|e| e.flush_singleton(marker, payload, metadata))
177190
}
178191

179-
fn flush(&self, marker: DataMarkerInfo) -> Result<(), DataError> {
180-
self.0.iter().try_for_each(|e| e.flush(marker))
192+
fn flush(&self, marker: DataMarkerInfo, metadata: FlushMetadata) -> Result<(), DataError> {
193+
self.0.iter().try_for_each(|e| e.flush(marker, metadata))
181194
}
182195

183196
fn close(&mut self) -> Result<(), DataError> {

provider/data/calendar/data/chinese_cache_v1_marker.rs.data

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provider/data/calendar/data/dangi_cache_v1_marker.rs.data

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

provider/data/calendar/data/islamic_observational_cache_v1_marker.rs.data

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)