Skip to content

Commit

Permalink
Make datagen work without CARGO_PKG_RUST_VERSION environment variab…
Browse files Browse the repository at this point in the history
…le (#4292)

This is for better compatibility with other build systems.
  • Loading branch information
robertbastian authored Nov 15, 2023
1 parent 0e307c1 commit 6e93719
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- CLDR 44 compatibility fixes (https://github.com/unicode-org/icu4x/pull/4134, https://github.com/unicode-org/icu4x/pull/4156, https://github.com/unicode-org/icu4x/pull/4158)
- Fix `supported_locales` for collator keys (https://github.com/unicode-org/icu4x/pull/4169)
- CLI: Fix behavior of `--segmenter-lstm-root` such that it does not override `icuexportdata-root` (https://github.com/unicode-org/icu4x/pull/4277)
- Make datagen work without `CARGO_PKG_RUST_VERSION` environment variable (https://github.com/unicode-org/icu4x/pull/4292)
- Utilities
- `databake`
- Add implementations for `HashSet`, `HashMap`, `BTreeSet`, `BTreeMap` (https://github.com/unicode-org/icu4x/pull/4268, https://github.com/unicode-org/icu4x/pull/4274, https://github.com/unicode-org/icu4x/pull/4295)
Expand Down
33 changes: 25 additions & 8 deletions provider/datagen/src/baked_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ macro_rules! move_out {
// TokenStream isn't Send/Sync
type SyncTokenStream = String;

const MSRV: &str = std::env!("CARGO_PKG_RUST_VERSION");
// Produces an MSRV clippy annotation if the `CARGO_PKG_RUST_VERSION` is set.
fn maybe_msrv() -> TokenStream {
std::option_env!("CARGO_PKG_RUST_VERSION")
.map(|msrv| {
quote! {
#[clippy::msrv = #msrv]
}
})
.unwrap_or_default()
}

/// Options for configuring the output of [`BakedExporter`].
#[non_exhaustive]
Expand Down Expand Up @@ -288,6 +297,8 @@ impl BakedExporter {

let prefixed_macro_ident = format!("__impl_{ident}").parse::<TokenStream>().unwrap();

let maybe_msrv = maybe_msrv();

self.write_to_file(
PathBuf::from(format!("macros/{}.rs.data", ident)),
quote! {
Expand All @@ -298,7 +309,7 @@ impl BakedExporter {
#[macro_export]
macro_rules! #prefixed_macro_ident {
($provider:ty) => {
#[clippy::msrv = #MSRV]
#maybe_msrv
const _: () = <$provider>::MUST_USE_MAKE_PROVIDER_MACRO;
#body
}
Expand Down Expand Up @@ -355,15 +366,17 @@ impl DataExporter for BakedExporter {

let bake = payload.tokenize(&self.dependencies);

let maybe_msrv = maybe_msrv();

self.write_impl_macro(quote! {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl $provider {
// Exposing singleton structs as consts allows us to get rid of fallibility
#[doc(hidden)]
pub const #singleton_ident: &'static <#marker as icu_provider::DataMarker>::Yokeable = &#bake;
}

#[clippy::msrv = #MSRV]
#maybe_msrv
impl icu_provider::DataProvider<#marker> for $provider {
fn load(
&self,
Expand Down Expand Up @@ -450,6 +463,8 @@ impl BakedExporter {
.remove(&key)
.unwrap_or_default();

let maybe_msrv = maybe_msrv();

let body = if values.is_empty() {
quote!(Err(icu_provider::DataErrorKind::MissingLocale.with_req(<#marker as icu_provider::KeyedDataMarker>::KEY, req)))
} else {
Expand Down Expand Up @@ -559,7 +574,7 @@ impl BakedExporter {

self.write_impl_macro(
quote! {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl icu_provider::DataProvider<#marker> for $provider {
fn load(
&self,
Expand Down Expand Up @@ -624,6 +639,8 @@ impl BakedExporter {
)
}));

let maybe_msrv = maybe_msrv();

// macros.rs is the interface for built-in data. It exposes one macro per data key.
self.write_to_file(
PathBuf::from("macros.rs"),
Expand All @@ -643,7 +660,7 @@ impl BakedExporter {
#[macro_export]
macro_rules! __make_provider {
($name:ty) => {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl $name {
#[doc(hidden)]
#[allow(dead_code)]
Expand Down Expand Up @@ -686,7 +703,7 @@ impl BakedExporter {
#[allow(unused_macros)]
macro_rules! impl_any_provider {
($provider:ty) => {
#[clippy::msrv = #MSRV]
#maybe_msrv
impl icu_provider::AnyProvider for $provider {
fn load_any(&self, key: icu_provider::DataKey, req: icu_provider::DataRequest) -> Result<icu_provider::AnyResponse, icu_provider::DataError> {
match key.hashed() {
Expand All @@ -703,7 +720,7 @@ impl BakedExporter {
}

// For backwards compatibility
#[clippy::msrv = #MSRV]
#maybe_msrv
pub struct BakedDataProvider;
impl_data_provider!(BakedDataProvider);
},
Expand Down

0 comments on commit 6e93719

Please sign in to comment.