use super::DisplayNamesPreferences;
use super::{
impl_writeable_for_single_display_name_borrowed, impl_writeable_for_single_display_name_owned,
load_one,
};
use crate::provider::names::{
LocaleNamesScriptMediumHeavyV1, LocaleNamesScriptMediumLightV1, LocaleNamesScriptMediumTinyV1,
LocaleNamesScriptShortHeavyV1,
};
use icu_locale_core::subtags::Script;
use icu_provider::{DataPayloadOr, prelude::*};
#[inline]
fn make_attributes(subtag: &Script) -> &DataMarkerAttributes {
DataMarkerAttributes::from_str_or_panic(subtag.as_str())
}
#[inline]
fn make_locale(prefs: DisplayNamesPreferences) -> DataLocale {
LocaleNamesScriptMediumTinyV1::make_locale(prefs.locale_preferences)
}
macro_rules! table_row {
(try_new_tiny) => {
"| [`try_new_tiny`](Self::try_new_tiny) | \"Latin\" | ❌ | ❌ |"
};
(try_new_light) => {
"| [`try_new_light`](Self::try_new_light) | \"Latin\" | \"Unknown Script\" | ❌ |"
};
(try_new_heavy) => {
"| [`try_new_heavy`](Self::try_new_heavy) | \"Latin\" | \"Unknown Script\" | \"Sumero-Akkadian Cuneiform\" |"
};
(try_new_short_heavy) => {
"| [`try_new_short_heavy`](Self::try_new_short_heavy) | \"Latin\" | \"Unknown Script\" | \"S-A Cuneiform\" |"
};
}
#[doc = concat!(table_row!(try_new_tiny), "\n")]
#[doc = concat!(table_row!(try_new_light), "\n")]
#[doc = concat!(table_row!(try_new_heavy), "\n")]
#[doc = concat!(table_row!(try_new_short_heavy), "\n")]
#[derive(Debug)]
pub struct ScriptDisplayName {
pub(crate) payload: DataPayloadOr<LocaleNamesScriptMediumLightV1, Script>,
}
impl ScriptDisplayName {
#[cfg(feature = "compiled_data")]
pub fn new_light_with_fallback(prefs: DisplayNamesPreferences, script: Script) -> Self {
Self::try_new_light(prefs, script).unwrap_or(Self {
payload: DataPayloadOr::from_other(script),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, script: Script) -> 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,
script: Script,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<LocaleNamesScriptMediumLightV1>
+ DataProvider<LocaleNamesScriptMediumTinyV1>,
{
let attrs = make_attributes(&script);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesScriptMediumLightV1, _, _>(provider, &locale, attrs)?
.map_or_else(
|| load_one::<LocaleNamesScriptMediumTinyV1, _, _>(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, script: Script) -> Self {
Self::try_new_tiny(prefs, script).unwrap_or(Self {
payload: DataPayloadOr::from_other(script),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, script: Script) -> 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,
script: Script,
) -> Result<Self, DataError>
where
D: ?Sized + DataProvider<LocaleNamesScriptMediumTinyV1>,
{
let attrs = make_attributes(&script);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesScriptMediumTinyV1, _, _>(provider, &locale, attrs)?
.map(DataPayloadOr::from_payload)
.ok_or_else(|| DataErrorKind::IdentifierNotFound.into_error())?;
Ok(Self { payload })
}
#[cfg(feature = "compiled_data")]
pub fn new_heavy_with_fallback(prefs: DisplayNamesPreferences, script: Script) -> Self {
Self::try_new_heavy(prefs, script).unwrap_or(Self {
payload: DataPayloadOr::from_other(script),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, script: Script) -> result: Result<Self, DataError>,
functions: [
try_new_heavy,
try_new_heavy_with_buffer_provider,
try_new_heavy_unstable,
Self
]
);
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_heavy)]
pub fn try_new_heavy_unstable<D>(
provider: &D,
prefs: DisplayNamesPreferences,
script: Script,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<LocaleNamesScriptMediumHeavyV1>
+ DataProvider<LocaleNamesScriptMediumLightV1>
+ DataProvider<LocaleNamesScriptMediumTinyV1>,
{
let attrs = make_attributes(&script);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesScriptMediumHeavyV1, _, _>(provider, &locale, attrs)?
.map_or_else(
|| load_one::<LocaleNamesScriptMediumLightV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map_or_else(
|| load_one::<LocaleNamesScriptMediumTinyV1, _, _>(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_heavy_with_fallback(prefs: DisplayNamesPreferences, script: Script) -> Self {
Self::try_new_short_heavy(prefs, script).unwrap_or(Self {
payload: DataPayloadOr::from_other(script),
})
}
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, script: Script) -> result: Result<Self, DataError>,
functions: [
try_new_short_heavy,
try_new_short_heavy_with_buffer_provider,
try_new_short_heavy_unstable,
Self
]
);
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_short_heavy)]
pub fn try_new_short_heavy_unstable<D>(
provider: &D,
prefs: DisplayNamesPreferences,
script: Script,
) -> Result<Self, DataError>
where
D: ?Sized
+ DataProvider<LocaleNamesScriptShortHeavyV1>
+ DataProvider<LocaleNamesScriptMediumHeavyV1>
+ DataProvider<LocaleNamesScriptMediumLightV1>
+ DataProvider<LocaleNamesScriptMediumTinyV1>,
{
let attrs = make_attributes(&script);
let locale = make_locale(prefs);
let payload = load_one::<LocaleNamesScriptShortHeavyV1, _, _>(provider, &locale, attrs)?
.map_or_else(
|| load_one::<LocaleNamesScriptMediumHeavyV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map_or_else(
|| load_one::<LocaleNamesScriptMediumLightV1, _, _>(provider, &locale, attrs),
|p| Ok(Some(p)),
)?
.map_or_else(
|| load_one::<LocaleNamesScriptMediumTinyV1, _, _>(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) -> ScriptDisplayNameBorrowed<'_> {
ScriptDisplayNameBorrowed {
value: self.borrow_str(),
}
}
}
impl_writeable_for_single_display_name_owned!(ScriptDisplayName);
#[derive(Debug, Clone, Copy)]
pub struct ScriptDisplayNameBorrowed<'a> {
value: &'a str,
}
impl_writeable_for_single_display_name_borrowed!(ScriptDisplayNameBorrowed);
#[cfg(test)]
mod tests {
use super::*;
use icu_locale_core::{locale, subtags::script};
#[test]
fn test_script_display_name_owned_table() {
let prefs_en = DisplayNamesPreferences::from(locale!("en"));
let inputs = [script!("Latn"), script!("Zzzz"), script!("Xsux")];
macro_rules! check_row {
($constructor:ident) => {
let items = inputs.iter().map(|id| {
ScriptDisplayName::$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_light);
check_row!(try_new_heavy);
check_row!(try_new_short_heavy);
}
}