Skip to content

Commit

Permalink
Add helpers for converting LanguageIdentifier to LocaleFamily
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Apr 30, 2024
1 parent 006e2c2 commit b3abb3e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
7 changes: 3 additions & 4 deletions provider/datagen/src/bin/icu4x-datagen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,9 @@ fn main() -> eyre::Result<()> {
} else {
let locale_families = match preprocessed_locales {
Some(PreprocessedLocales::All) => vec![LocaleFamily::full()],
Some(PreprocessedLocales::LanguageIdentifiers(lids)) => lids
.into_iter()
.map(LocaleFamily::with_descendants)
.collect(),
Some(PreprocessedLocales::LanguageIdentifiers(lids)) => {
lids.into_iter().map(LocaleFamily::auto).collect()
}
None => cli
.locales
.into_iter()
Expand Down
57 changes: 57 additions & 0 deletions provider/datagen/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,67 @@ impl LocaleFamily {
/// - Descendants: "en-GB", "en-ZA", ...
///
/// Stylized on the CLI as: "en-US"
#[inline]
pub const fn with_descendants(langid: LanguageIdentifier) -> Self {
Self {
langid: Some(langid),
annotations: LocaleFamilyAnnotations::with_descendants(),
}
}

/// Alias for [`LocaleFamily::with_descendants`] that works with more types,
/// such as [`Locale`].
///
/// # Examples
///
/// ```
/// use icu::locid::Locale;
/// use icu_datagen::LocaleFamily;
/// use writeable::assert_writeable_eq;
///
/// let locale = "en-US-u-hc-h23".parse::<Locale>().unwrap();
/// let family = LocaleFamily::auto(locale);
///
/// assert_writeable_eq!(family, "en-US");
/// ```
///
/// [`Locale`]: icu_locid::Locale
#[inline]
pub fn auto(langid: impl Into<LanguageIdentifier>) -> Self {
Self::with_descendants(langid.into())
}

/// Convenience function to turn an iterator of language identifiers into
/// an iterator of [`LocaleFamily`] using the [`auto()`] constructor.
///
/// # Examples
///
/// ```
/// use icu::locid::locale;
/// use icu_datagen::blob_exporter::*;
/// use icu_datagen::prelude::*;
///
/// DatagenDriver::new()
/// .with_keys([icu::list::provider::AndListV1Marker::KEY])
/// .with_locales_and_fallback(
/// LocaleFamily::auto_iter([locale!("en-US"), locale!("de-CH")]),
/// Default::default(),
/// )
/// .export(
/// &DatagenProvider::new_latest_tested(),
/// BlobExporter::new_with_sink(Box::new(&mut Vec::new())),
/// )
/// .unwrap();
/// ```
///
/// [`auto()`]: Self::auto
#[inline]
pub fn auto_iter<L: Into<LanguageIdentifier>>(
langids: impl IntoIterator<Item = L>,
) -> impl IntoIterator<Item = LocaleFamily> {
langids.into_iter().map(Into::into).map(Self::auto)
}

/// The family containing all ancestors of the selected locale.
///
/// This family type does not include regional variants unless the selected locale is itself
Expand All @@ -154,6 +208,7 @@ impl LocaleFamily {
/// - Ancestors: "und", "en"
///
/// Stylized on the CLI as: "^en-US"
#[inline]
pub const fn without_descendants(langid: LanguageIdentifier) -> Self {
Self {
langid: Some(langid),
Expand All @@ -173,6 +228,7 @@ impl LocaleFamily {
/// but it does _not_ contain the ancestors "en" and "und".
///
/// Stylized on the CLI as: "%en-US"
#[inline]
pub const fn without_ancestors(langid: LanguageIdentifier) -> Self {
Self {
langid: Some(langid),
Expand All @@ -185,6 +241,7 @@ impl LocaleFamily {
/// For example, the family `::single("en-001")` contains only "en-001".
///
/// Stylized on the CLI as: "@en-US"
#[inline]
pub const fn single(langid: LanguageIdentifier) -> Self {
Self {
langid: Some(langid),
Expand Down
2 changes: 1 addition & 1 deletion provider/datagen/src/provider/tests/make_testdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn make_testdata() {
DatagenDriver::new()
.with_keys(crate::all_keys())
.with_locales_and_fallback(
LOCALES.iter().cloned().map(LocaleFamily::with_descendants),
LocaleFamily::auto_iter(LOCALES.iter().cloned()),
Default::default(),
)
.with_segmenter_models([
Expand Down
17 changes: 7 additions & 10 deletions provider/datagen/tests/test-options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,6 @@ make_exportable_provider!(
]
);

fn families(
langids: impl IntoIterator<Item = LanguageIdentifier>,
) -> impl IntoIterator<Item = LocaleFamily> {
langids.into_iter().map(LocaleFamily::with_descendants)
}

#[derive(Default)]
struct TestingExporter(FrozenMap<DataLocale, String>);

Expand Down Expand Up @@ -430,7 +424,10 @@ fn explicit_preferred() {
.with_fallback_mode(FallbackMode::PreferredForExporter),
DatagenDriver::new()
.with_keys([HelloWorldV1Marker::KEY])
.with_locales_and_fallback(families(SELECTED_LOCALES), Default::default()),
.with_locales_and_fallback(
LocaleFamily::auto_iter(SELECTED_LOCALES),
Default::default(),
),
&TestingProvider::with_decimal_symbol_like_data(),
);

Expand Down Expand Up @@ -479,7 +476,7 @@ fn explicit_hybrid() {
.with_fallback_mode(FallbackMode::Hybrid),
DatagenDriver::new()
.with_keys([HelloWorldV1Marker::KEY])
.with_locales_and_fallback(families(SELECTED_LOCALES), {
.with_locales_and_fallback(LocaleFamily::auto_iter(SELECTED_LOCALES), {
let mut options = FallbackOptions::default();
options.deduplication_strategy = Some(DeduplicationStrategy::None);
options
Expand Down Expand Up @@ -532,7 +529,7 @@ fn explicit_runtime() {
.with_fallback_mode(FallbackMode::RuntimeManual),
DatagenDriver::new()
.with_keys([HelloWorldV1Marker::KEY])
.with_locales_and_fallback(families(SELECTED_LOCALES), {
.with_locales_and_fallback(LocaleFamily::auto_iter(SELECTED_LOCALES), {
let mut options = FallbackOptions::default();
options.deduplication_strategy = Some(DeduplicationStrategy::Maximal);
options
Expand Down Expand Up @@ -580,7 +577,7 @@ fn explicit_runtime_retain_base() {
let exported = export_to_map_1_5(
DatagenDriver::new()
.with_keys([HelloWorldV1Marker::KEY])
.with_locales_and_fallback(families(SELECTED_LOCALES), {
.with_locales_and_fallback(LocaleFamily::auto_iter(SELECTED_LOCALES), {
let mut options = FallbackOptions::default();
options.deduplication_strategy = Some(DeduplicationStrategy::RetainBaseLanguages);
options
Expand Down

0 comments on commit b3abb3e

Please sign in to comment.