use super::DisplayNamesPreferences;
use super::load_one;
use super::{
impl_writeable_for_single_display_name_borrowed, impl_writeable_for_single_display_name_owned,
};
use crate::provider::names::{
LocaleNamesRegionMediumLightV1, LocaleNamesRegionMediumTinyV1, LocaleNamesRegionShortLightV1,
LocaleNamesRegionShortTinyV1,
};
use icu_locale_core::subtags::Region;
use icu_provider::DataPayloadOr;
use icu_provider::prelude::*;
#[inline]
fn make_attributes(subtag: &Region) -> &DataMarkerAttributes {
DataMarkerAttributes::from_str_or_panic(subtag.as_str())
}
#[inline]
fn make_locale(prefs: DisplayNamesPreferences) -> DataLocale {
LocaleNamesRegionMediumTinyV1::make_locale(prefs.locale_preferences)
}
macro_rules! table_row {
(try_new_tiny) => {
"| [`try_new_tiny`](Self::try_new_tiny) | \"United States\" | ❌ |"
};
(try_new_short_tiny) => {
"| [`try_new_short_tiny`](Self::try_new_short_tiny) | \"US\" | ❌ |"
};
(try_new_light) => {
"| [`try_new_light`](Self::try_new_light) | \"United States\" | \"Andorra\" |"
};
(try_new_short_light) => {
"| [`try_new_short_light`](Self::try_new_short_light) | \"US\" | \"Andorra\" |"
};
}
#[doc = concat!(table_row!(try_new_tiny), "\n")]
#[doc = concat!(table_row!(try_new_short_tiny), "\n")]
#[doc = concat!(table_row!(try_new_light), "\n")]
#[doc = concat!(table_row!(try_new_short_light), "\n")]
#[derive(Debug)]
pub struct RegionDisplayName {
pub(crate) payload: DataPayloadOr<LocaleNamesRegionMediumLightV1, Region>,
}
impl RegionDisplayName {
#[cfg(feature = "compiled_data")]
pub fn new_light_with_fallback(prefs: DisplayNamesPreferences, region: Region) -> Self {
Self::try_new_light(prefs, region).unwrap_or(Self {
payload: DataPayloadOr::from_other(region),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, region: Region) -> result: Result<Self, DataError>,
functions: [
try_new_light,
try_new_light_with_buffer_provider,
try_new_light_unstable,
Self
]
);
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_light)]
pub fn try_new_light_unstable<D>(
provider: &D,
prefs: DisplayNamesPreferences,
region: Region,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<LocaleNamesRegionMediumLightV1>
+ DataProvider<LocaleNamesRegionMediumTinyV1>,
{
let attrs = make_attributes(®ion);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesRegionMediumLightV1, _, _>(provider, &locale, attrs)?
.map_or_else(
|| load_one::<LocaleNamesRegionMediumTinyV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map(DataPayloadOr::from_payload)
.ok_or_else(|| DataErrorKind::IdentifierNotFound.into_error())?;
Ok(Self { payload })
}
#[cfg(feature = "compiled_data")]
pub fn new_tiny_with_fallback(prefs: DisplayNamesPreferences, region: Region) -> Self {
Self::try_new_tiny(prefs, region).unwrap_or(Self {
payload: DataPayloadOr::from_other(region),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, region: Region) -> result: Result<Self, DataError>,
functions: [
try_new_tiny,
try_new_tiny_with_buffer_provider,
try_new_tiny_unstable,
Self
]
);
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_tiny)]
pub fn try_new_tiny_unstable<D>(
provider: &D,
prefs: DisplayNamesPreferences,
region: Region,
) -> Result<Self, DataError>
where
D: ?Sized + DataProvider<LocaleNamesRegionMediumTinyV1>,
{
let attrs = make_attributes(®ion);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesRegionMediumTinyV1, _, _>(provider, &locale, attrs)?
.map(DataPayloadOr::from_payload)
.ok_or_else(|| DataErrorKind::IdentifierNotFound.into_error())?;
Ok(Self { payload })
}
#[cfg(feature = "compiled_data")]
pub fn new_short_tiny_with_fallback(prefs: DisplayNamesPreferences, region: Region) -> Self {
Self::try_new_short_tiny(prefs, region).unwrap_or(Self {
payload: DataPayloadOr::from_other(region),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, region: Region) -> result: Result<Self, DataError>,
functions: [
try_new_short_tiny,
try_new_short_tiny_with_buffer_provider,
try_new_short_tiny_unstable,
Self
]
);
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_short_tiny)]
pub fn try_new_short_tiny_unstable<D>(
provider: &D,
prefs: DisplayNamesPreferences,
region: Region,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<LocaleNamesRegionShortTinyV1>
+ DataProvider<LocaleNamesRegionMediumTinyV1>,
{
let attrs = make_attributes(®ion);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesRegionShortTinyV1, _, _>(provider, &locale, attrs)?
.map_or_else(
|| load_one::<LocaleNamesRegionMediumTinyV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map(DataPayloadOr::from_payload)
.ok_or_else(|| DataErrorKind::IdentifierNotFound.into_error())?;
Ok(Self { payload })
}
#[cfg(feature = "compiled_data")]
pub fn new_short_light_with_fallback(prefs: DisplayNamesPreferences, region: Region) -> Self {
Self::try_new_short_light(prefs, region).unwrap_or(Self {
payload: DataPayloadOr::from_other(region),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, region: Region) -> result: Result<Self, DataError>,
functions: [
try_new_short_light,
try_new_short_light_with_buffer_provider,
try_new_short_light_unstable,
Self
]
);
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_short_light)]
pub fn try_new_short_light_unstable<D>(
provider: &D,
prefs: DisplayNamesPreferences,
region: Region,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<LocaleNamesRegionShortLightV1>
+ DataProvider<LocaleNamesRegionShortTinyV1>
+ DataProvider<LocaleNamesRegionMediumLightV1>
+ DataProvider<LocaleNamesRegionMediumTinyV1>,
{
let attrs = make_attributes(®ion);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesRegionShortLightV1, _, _>(provider, &locale, attrs)?
.map_or_else(
|| load_one::<LocaleNamesRegionShortTinyV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map_or_else(
|| load_one::<LocaleNamesRegionMediumLightV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map_or_else(
|| load_one::<LocaleNamesRegionMediumTinyV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map(DataPayloadOr::from_payload)
.ok_or_else(|| DataErrorKind::IdentifierNotFound.into_error())?;
Ok(Self { payload })
}
#[inline]
fn borrow_str(&self) -> &str {
match self.payload.get() {
Ok(s) => s,
Err(subtag) => subtag.as_str(),
}
}
pub fn as_borrowed(&self) -> RegionDisplayNameBorrowed<'_> {
RegionDisplayNameBorrowed {
value: self.borrow_str(),
}
}
}
impl_writeable_for_single_display_name_owned!(RegionDisplayName);
#[derive(Debug, Clone, Copy)]
pub struct RegionDisplayNameBorrowed<'a> {
value: &'a str,
}
impl_writeable_for_single_display_name_borrowed!(RegionDisplayNameBorrowed);
#[cfg(test)]
mod tests {
use super::*;
use icu_locale_core::{locale, subtags::region};
#[test]
fn test_region_display_name_owned_table() {
let prefs_en = DisplayNamesPreferences::from(locale!("en"));
let inputs = [region!("US"), region!("AD")];
macro_rules! check_row {
($constructor:ident) => {
let items = inputs.iter().map(|id| {
RegionDisplayName::$constructor(prefs_en, *id)
.map(|name| Ok::<_, ()>(name.to_string()))
});
assert_eq!(
super::super::format_table_row(stringify!($constructor), items),
table_row!($constructor)
);
};
}
check_row!(try_new_tiny);
check_row!(try_new_short_tiny);
check_row!(try_new_light);
check_row!(try_new_short_light);
}
}