Skip to content

Commit

Permalink
Add DRY to baked providers (#5544)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Sep 19, 2024
1 parent 02153c6 commit 3524f8e
Show file tree
Hide file tree
Showing 597 changed files with 4,245 additions and 6,999 deletions.
61 changes: 40 additions & 21 deletions provider/baked/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl BakedExporter {
marker: DataMarkerInfo,
stats: Statistics,
body: TokenStream,
dry_body: TokenStream,
dry_body: Option<TokenStream>,
iterable_body: TokenStream,
) -> Result<(), DataError> {
let marker_unqualified = bake_marker(marker).into_iter().last().unwrap().to_string();
Expand Down Expand Up @@ -356,6 +356,28 @@ impl BakedExporter {

let maybe_msrv = maybe_msrv();

let dry = if let Some(dry_body) = dry_body {
quote! {
($provider:ty, DRY) => {
#prefixed_macro_ident!($provider);
#dry_body
};
($provider:ty, DRY, ITER) => {
#prefixed_macro_ident!($provider);
#dry_body
#iterable_body
};
}
} else {
quote! {
($provider:ty, DRY) => {
};
($provider:ty, DRY, ITER) => {
#prefixed_macro_ident!($provider, ITER);
};
}
};

self.write_to_file(
Path::new(&format!("{ident}.rs.data")),
quote! {
Expand All @@ -368,19 +390,11 @@ impl BakedExporter {
const _: () = <$provider>::MUST_USE_MAKE_PROVIDER_MACRO;
#body
};
($provider:ty, DRY) => {
#prefixed_macro_ident!($provider);
#dry_body
};
($provider:ty, ITER) => {
#prefixed_macro_ident!($provider);
#iterable_body
};
($provider:ty, DRY, ITER) => {
#prefixed_macro_ident!($provider);
#dry_body
#iterable_body
};
#dry
}
#[doc(inline)]
pub use #prefixed_macro_ident as #macro_ident;
Expand Down Expand Up @@ -417,6 +431,7 @@ impl DataExporter for BakedExporter {
&self,
marker: DataMarkerInfo,
payload: &DataPayload<ExportMarker>,
_metadata: FlushMetadata,
) -> Result<(), DataError> {
let maybe_msrv = maybe_msrv();

Expand Down Expand Up @@ -469,7 +484,7 @@ impl DataExporter for BakedExporter {
}
}
},
quote! {
Some(quote! {
#maybe_msrv
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
Expand All @@ -480,7 +495,7 @@ impl DataExporter for BakedExporter {
}
}
}
},
}),
quote! {
#maybe_msrv
impl icu_provider::IterableDataProvider<#marker_bake> for $provider {
Expand All @@ -491,7 +506,7 @@ impl DataExporter for BakedExporter {
})
}

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

let marker_bake = bake_marker(marker);
Expand All @@ -518,14 +533,14 @@ impl DataExporter for BakedExporter {
}
}
},
quote! {
Some(quote! {
#maybe_msrv
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
Err(icu_provider::DataErrorKind::IdentifierNotFound.with_req(<#marker_bake as icu_provider::DataMarker>::INFO, req))
}
}
},
}),
quote! {
#maybe_msrv
impl icu_provider::IterableDataProvider<#marker_bake> for $provider {
Expand Down Expand Up @@ -633,13 +648,17 @@ impl DataExporter for BakedExporter {
}
}
},
quote! {
#maybe_msrv
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
icu_provider::DataProvider::<#marker_bake>::load(self, req).map(|r| r.metadata)
if metadata.supports_dry_provider {
Some(quote! {
#maybe_msrv
impl icu_provider::DryDataProvider<#marker_bake> for $provider {
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
icu_provider::DataProvider::<#marker_bake>::load(self, req).map(|r| r.metadata)
}
}
}
})
} else {
None
},
quote! {
#maybe_msrv
Expand Down
16 changes: 8 additions & 8 deletions provider/baked/tests/data/hello_world_v1_marker.rs.data
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ macro_rules! __impl_hello_world_v1_marker {
}
}
};
($ provider : ty , DRY) => {
($ provider : ty , ITER) => {
__impl_hello_world_v1_marker!($provider);
#[clippy::msrv = "1.71.1"]
impl icu_provider::DryDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
icu_provider::DataProvider::<icu_provider::hello_world::HelloWorldV1Marker>::load(self, req).map(|r| r.metadata)
impl icu_provider::IterableDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
fn iter_ids(&self) -> Result<std::collections::BTreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
Ok(icu_provider_baked::DataStore::iter(&Self::DATA_HELLO_WORLD_V1_MARKER).collect())
}
}
};
($ provider : ty , ITER) => {
($ provider : ty , DRY) => {
__impl_hello_world_v1_marker!($provider);
#[clippy::msrv = "1.71.1"]
impl icu_provider::IterableDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
fn iter_ids(&self) -> Result<std::collections::BTreeSet<icu_provider::DataIdentifierCow<'static>>, icu_provider::DataError> {
Ok(icu_provider_baked::DataStore::iter(&Self::DATA_HELLO_WORLD_V1_MARKER).collect())
impl icu_provider::DryDataProvider<icu_provider::hello_world::HelloWorldV1Marker> for $provider {
fn dry_load(&self, req: icu_provider::DataRequest) -> Result<icu_provider::DataResponseMetadata, icu_provider::DataError> {
icu_provider::DataProvider::<icu_provider::hello_world::HelloWorldV1Marker>::load(self, req).map(|r| r.metadata)
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion provider/blob/benches/auxkey_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ where
)
.unwrap();
}
exporter.flush(M::INFO).unwrap();
exporter.flush(M::INFO, Default::default()).unwrap();
}

fn make_blob_v1() -> Vec<u8> {
Expand Down
8 changes: 6 additions & 2 deletions provider/blob/src/blob_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ mod test {
BlobExporter::new_v2_with_sink(Box::new(&mut blob))
};

exporter.flush(HelloWorldV1Marker::INFO).unwrap();
exporter
.flush(HelloWorldV1Marker::INFO, Default::default())
.unwrap();

exporter.close().unwrap();
}
Expand Down Expand Up @@ -213,7 +215,9 @@ mod test {
BlobExporter::new_v2_with_sink(Box::new(&mut blob))
};

exporter.flush(HelloSingletonV1Marker::INFO).unwrap();
exporter
.flush(HelloSingletonV1Marker::INFO, Default::default())
.unwrap();

exporter.close().unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion provider/blob/src/export/blob_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl DataExporter for BlobExporter<'_> {
Ok(())
}

fn flush(&self, marker: DataMarkerInfo) -> Result<(), DataError> {
fn flush(&self, marker: DataMarkerInfo, _metadata: FlushMetadata) -> Result<(), DataError> {
self.all_markers
.lock()
.expect("poison")
Expand Down
4 changes: 3 additions & 1 deletion provider/blob/tests/test_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ where
)
.unwrap();
}
exporter.flush(HelloWorldV1Marker::INFO).unwrap();
exporter
.flush(HelloWorldV1Marker::INFO, Default::default())
.unwrap();
exporter.close().unwrap();
}

Expand Down
31 changes: 22 additions & 9 deletions provider/core/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::collections::HashSet;
/// An object capable of exporting data payloads in some form.
pub trait DataExporter: Sync {
/// Save a `payload` corresponding to the given marker and locale.
///
/// Takes non-mut self as it can be called concurrently.
fn put_payload(
&self,
Expand All @@ -26,21 +27,22 @@ pub trait DataExporter: Sync {
) -> Result<(), DataError>;

/// Function called for singleton markers.
///
/// Takes non-mut self as it can be called concurrently.
fn flush_singleton(
&self,
marker: DataMarkerInfo,
payload: &DataPayload<ExportMarker>,
metadata: FlushMetadata,
) -> Result<(), DataError> {
self.put_payload(marker, Default::default(), payload)?;
self.flush(marker)
self.flush(marker, metadata)
}

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

Expand All @@ -52,6 +54,15 @@ pub trait DataExporter: Sync {
}
}

/// Metadata for [`DataExporter::flush`]
#[non_exhaustive]
#[derive(Debug, Copy, Clone, Default)]
pub struct FlushMetadata {
/// Whether the data was generated in such a way that a [`DryDataProvider`] implementation
/// makes sense.
pub supports_dry_provider: bool,
}

impl DataExporter for Box<dyn DataExporter> {
fn put_payload(
&self,
Expand All @@ -66,12 +77,13 @@ impl DataExporter for Box<dyn DataExporter> {
&self,
marker: DataMarkerInfo,
payload: &DataPayload<ExportMarker>,
metadata: FlushMetadata,
) -> Result<(), DataError> {
(**self).flush_singleton(marker, payload)
(**self).flush_singleton(marker, payload, metadata)
}

fn flush(&self, marker: DataMarkerInfo) -> Result<(), DataError> {
(**self).flush(marker)
fn flush(&self, marker: DataMarkerInfo, metadata: FlushMetadata) -> Result<(), DataError> {
(**self).flush(marker, metadata)
}

fn close(&mut self) -> Result<(), DataError> {
Expand Down Expand Up @@ -170,14 +182,15 @@ impl DataExporter for MultiExporter {
&self,
marker: DataMarkerInfo,
payload: &DataPayload<ExportMarker>,
metadata: FlushMetadata,
) -> Result<(), DataError> {
self.0
.iter()
.try_for_each(|e| e.flush_singleton(marker, payload))
.try_for_each(|e| e.flush_singleton(marker, payload, metadata))
}

fn flush(&self, marker: DataMarkerInfo) -> Result<(), DataError> {
self.0.iter().try_for_each(|e| e.flush(marker))
fn flush(&self, marker: DataMarkerInfo, metadata: FlushMetadata) -> Result<(), DataError> {
self.0.iter().try_for_each(|e| e.flush(marker, metadata))
}

fn close(&mut self) -> Result<(), DataError> {
Expand Down
18 changes: 9 additions & 9 deletions provider/data/calendar/data/chinese_cache_v1_marker.rs.data

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions provider/data/calendar/data/dangi_cache_v1_marker.rs.data

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3524f8e

Please sign in to comment.