icu_provider_source/currency/
displayname.rsuse crate::cldr_serde;
use crate::SourceDataProvider;
use icu::experimental::dimension::provider::currency_displayname::*;
use icu_provider::prelude::*;
use std::borrow::Cow;
use std::collections::HashSet;
impl DataProvider<CurrencyDisplaynameV1Marker> for crate::SourceDataProvider {
fn load(
&self,
req: DataRequest,
) -> Result<DataResponse<CurrencyDisplaynameV1Marker>, DataError> {
self.check_req::<CurrencyDisplaynameV1Marker>(req)?;
let currencies_resource: &cldr_serde::currencies::data::Resource =
self.cldr()?
.numbers()
.read_and_parse(req.id.locale, "currencies.json")?;
let currency = currencies_resource
.main
.value
.numbers
.currencies
.get(req.id.marker_attributes.as_str())
.ok_or_else(|| {
DataErrorKind::IdentifierNotFound
.into_error()
.with_debug_context("No data for currency")
})?;
Ok(DataResponse {
metadata: Default::default(),
payload: DataPayload::from_owned(CurrencyDisplaynameV1 {
display_name: Cow::Owned(
currency
.display_name
.as_deref()
.ok_or_else(|| {
DataErrorKind::IdentifierNotFound
.into_error()
.with_debug_context("No display name found for the currency")
})?
.to_string(),
),
}),
})
}
}
impl crate::IterableDataProviderCached<CurrencyDisplaynameV1Marker> for SourceDataProvider {
fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
let mut result = HashSet::new();
let numbers = self.cldr()?.numbers();
let locales = numbers.list_locales()?;
for locale in locales {
let currencies_resource: &cldr_serde::currencies::data::Resource = self
.cldr()?
.numbers()
.read_and_parse(&locale, "currencies.json")?;
let currencies = ¤cies_resource.main.value.numbers.currencies;
for (currency, patterns) in currencies {
if patterns.display_name.is_none() {
continue;
}
if let Ok(attributes) = DataMarkerAttributes::try_from_string(currency.clone()) {
result.insert(DataIdentifierCow::from_owned(attributes, locale.clone()));
}
}
}
Ok(result)
}
}
#[test]
fn test_basic() {
use icu::locale::langid;
let provider = SourceDataProvider::new_testing();
let en: DataPayload<CurrencyDisplaynameV1Marker> = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
DataMarkerAttributes::from_str_or_panic("USD"),
&langid!("en").into(),
),
..Default::default()
})
.unwrap()
.payload;
let display_name = en.get().to_owned().display_name;
assert_eq!(display_name, "US Dollar");
let fr: DataPayload<CurrencyDisplaynameV1Marker> = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
DataMarkerAttributes::from_str_or_panic("USD"),
&langid!("fr").into(),
),
..Default::default()
})
.unwrap()
.payload;
let display_name = fr.get().to_owned().display_name;
assert_eq!(display_name, "dollar des États-Unis");
}