#![allow(clippy::exhaustive_structs)] use crate as icu_provider;
use crate::prelude::*;
use alloc::borrow::Cow;
use alloc::collections::BTreeSet;
use alloc::string::String;
use core::fmt::Debug;
use writeable::Writeable;
use yoke::*;
use zerofrom::*;
#[derive(Debug, PartialEq, Clone, Yokeable, ZeroFrom)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
any(feature = "deserialize_json", feature = "export"),
derive(serde::Serialize)
)]
#[cfg_attr(feature = "export", derive(databake::Bake))]
#[cfg_attr(feature = "export", databake(path = icu_provider::hello_world))]
pub struct HelloWorldV1<'data> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub message: Cow<'data, str>,
}
impl Default for HelloWorldV1<'_> {
fn default() -> Self {
HelloWorldV1 {
message: Cow::Borrowed("(und) Hello World"),
}
}
}
#[derive(Debug)]
pub struct HelloWorldV1Marker;
impl DynamicDataMarker for HelloWorldV1Marker {
type DataStruct = HelloWorldV1<'static>;
}
impl DataMarker for HelloWorldV1Marker {
const INFO: icu_provider::DataMarkerInfo =
DataMarkerInfo::from_path(icu_provider::marker::data_marker_path!("core/helloworld@1"));
}
#[derive(Debug, PartialEq, Default)]
pub struct HelloWorldProvider;
impl HelloWorldProvider {
const DATA: &'static [(&'static str, &'static str, &'static str)] = &[
("bn", "", "ওহে বিশ্ব"),
("cs", "", "Ahoj světe"),
("de", "", "Hallo Welt"),
("de-AT", "", "Servus Welt"),
("el", "", "Καλημέρα κόσμε"),
("en", "", "Hello World"),
("en-001", "", "Hello from 🗺️"),
("en-002", "", "Hello from 🌍"),
("en-019", "", "Hello from 🌎"),
("en-142", "", "Hello from 🌏"),
("en-GB", "", "Hello from 🇬🇧"),
("en-GB-u-sd-gbeng", "", "Hello from 🏴"),
("en", "reverse", "Olleh Dlrow"),
("eo", "", "Saluton, Mondo"),
("fa", "", "سلام دنیا"),
("fi", "", "hei maailma"),
("is", "", "Halló, heimur"),
("ja", "", "こんにちは世界"),
("ja", "reverse", "界世はちにんこ"),
("la", "", "Ave, munde"),
("pt", "", "Olá, mundo"),
("ro", "", "Salut, lume"),
("ru", "", "Привет, мир"),
("sr", "", "Поздрав свете"),
("sr-Latn", "", "Pozdrav svete"),
("vi", "", "Xin chào thế giới"),
("zh", "", "你好世界"),
];
#[cfg(feature = "deserialize_json")]
pub fn into_json_provider(self) -> HelloWorldJsonProvider {
HelloWorldJsonProvider
}
}
impl DataProvider<HelloWorldV1Marker> for HelloWorldProvider {
fn load(&self, req: DataRequest) -> Result<DataResponse<HelloWorldV1Marker>, DataError> {
#[allow(clippy::indexing_slicing)] let data = Self::DATA
.iter()
.find(|(l, a, _)| {
req.id.locale.strict_cmp(l.as_bytes()).is_eq()
&& *a == req.id.marker_attributes.as_str()
})
.map(|(_, _, v)| v)
.ok_or_else(|| {
DataErrorKind::IdentifierNotFound.with_req(HelloWorldV1Marker::INFO, req)
})?;
Ok(DataResponse {
metadata: Default::default(),
payload: DataPayload::from_static_str(data),
})
}
}
impl DryDataProvider<HelloWorldV1Marker> for HelloWorldProvider {
fn dry_load(&self, req: DataRequest) -> Result<DataResponseMetadata, DataError> {
self.load(req).map(|r| r.metadata)
}
}
impl DataPayload<HelloWorldV1Marker> {
pub fn from_static_str(s: &'static str) -> DataPayload<HelloWorldV1Marker> {
DataPayload::from_owned(HelloWorldV1 {
message: Cow::Borrowed(s),
})
}
}
icu_provider::dynutil::impl_dynamic_data_provider!(
HelloWorldProvider,
[HelloWorldV1Marker,],
AnyMarker
);
#[cfg(feature = "deserialize_json")]
#[derive(Debug)]
pub struct HelloWorldJsonProvider;
#[cfg(feature = "deserialize_json")]
impl DynamicDataProvider<BufferMarker> for HelloWorldJsonProvider {
fn load_data(
&self,
marker: DataMarkerInfo,
req: DataRequest,
) -> Result<DataResponse<BufferMarker>, DataError> {
marker.match_marker(HelloWorldV1Marker::INFO)?;
let result = HelloWorldProvider.load(req)?;
Ok(DataResponse {
metadata: DataResponseMetadata {
buffer_format: Some(icu_provider::buf::BufferFormat::Json),
..result.metadata
},
#[allow(clippy::unwrap_used)] payload: DataPayload::from_owned_buffer(
serde_json::to_string(result.payload.get())
.unwrap()
.into_bytes()
.into_boxed_slice(),
),
})
}
}
impl IterableDataProvider<HelloWorldV1Marker> for HelloWorldProvider {
fn iter_ids(&self) -> Result<BTreeSet<DataIdentifierCow>, DataError> {
#[allow(clippy::unwrap_used)] Ok(Self::DATA
.iter()
.map(|(l, a, _)| {
DataIdentifierCow::from_borrowed_and_owned(
DataMarkerAttributes::from_str_or_panic(a),
l.parse().unwrap(),
)
})
.collect())
}
}
#[cfg(feature = "export")]
icu_provider::export::make_exportable_provider!(HelloWorldProvider, [HelloWorldV1Marker,]);
#[derive(Debug)]
pub struct HelloWorldFormatter {
data: DataPayload<HelloWorldV1Marker>,
}
#[derive(Debug)]
pub struct FormattedHelloWorld<'l> {
data: &'l HelloWorldV1<'l>,
}
impl HelloWorldFormatter {
pub fn try_new(locale: &DataLocale) -> Result<Self, DataError> {
Self::try_new_unstable(&HelloWorldProvider, locale)
}
icu_provider::gen_any_buffer_data_constructors!((locale) -> error: DataError,
functions: [
try_new: skip,
try_new_with_any_provider,
try_new_with_buffer_provider,
try_new_unstable,
Self,
]);
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<P>(provider: &P, locale: &DataLocale) -> Result<Self, DataError>
where
P: DataProvider<HelloWorldV1Marker>,
{
let data = provider
.load(DataRequest {
id: crate::request::DataIdentifierBorrowed::for_locale(locale),
..Default::default()
})?
.payload;
Ok(Self { data })
}
#[allow(clippy::needless_lifetimes)] pub fn format<'l>(&'l self) -> FormattedHelloWorld<'l> {
FormattedHelloWorld {
data: self.data.get(),
}
}
pub fn format_to_string(&self) -> String {
self.format().write_to_string().into_owned()
}
}
impl Writeable for FormattedHelloWorld<'_> {
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
self.data.message.write_to(sink)
}
fn write_to_string(&self) -> Cow<str> {
self.data.message.clone()
}
fn writeable_length_hint(&self) -> writeable::LengthHint {
self.data.message.writeable_length_hint()
}
}
writeable::impl_display_with_writeable!(FormattedHelloWorld<'_>);
#[cfg(feature = "export")]
#[test]
fn test_iter() {
use crate::IterableDataProvider;
use icu_locale_core::locale;
assert_eq!(
HelloWorldProvider.iter_ids().unwrap(),
BTreeSet::from_iter([
DataIdentifierCow::from_locale(locale!("bn").into()),
DataIdentifierCow::from_locale(locale!("cs").into()),
DataIdentifierCow::from_locale(locale!("de").into()),
DataIdentifierCow::from_locale(locale!("de-AT").into()),
DataIdentifierCow::from_locale(locale!("el").into()),
DataIdentifierCow::from_locale(locale!("en").into()),
DataIdentifierCow::from_locale(locale!("en-001").into()),
DataIdentifierCow::from_locale(locale!("en-002").into()),
DataIdentifierCow::from_locale(locale!("en-019").into()),
DataIdentifierCow::from_locale(locale!("en-142").into()),
DataIdentifierCow::from_locale(locale!("en-GB").into()),
DataIdentifierCow::from_locale(locale!("en-GB-u-sd-gbeng").into()),
DataIdentifierCow::from_borrowed_and_owned(
DataMarkerAttributes::from_str_or_panic("reverse"),
locale!("en").into()
),
DataIdentifierCow::from_locale(locale!("eo").into()),
DataIdentifierCow::from_locale(locale!("fa").into()),
DataIdentifierCow::from_locale(locale!("fi").into()),
DataIdentifierCow::from_locale(locale!("is").into()),
DataIdentifierCow::from_locale(locale!("ja").into()),
DataIdentifierCow::from_borrowed_and_owned(
DataMarkerAttributes::from_str_or_panic("reverse"),
locale!("ja").into()
),
DataIdentifierCow::from_locale(locale!("la").into()),
DataIdentifierCow::from_locale(locale!("pt").into()),
DataIdentifierCow::from_locale(locale!("ro").into()),
DataIdentifierCow::from_locale(locale!("ru").into()),
DataIdentifierCow::from_locale(locale!("sr").into()),
DataIdentifierCow::from_locale(locale!("sr-Latn").into()),
DataIdentifierCow::from_locale(locale!("vi").into()),
DataIdentifierCow::from_locale(locale!("zh").into()),
])
);
}