1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
//! Data provider returning multilingual "Hello World" strings for testing.
#![allow(clippy::exhaustive_structs)] // data struct module
use crate as icu_provider;
use crate::prelude::*;
use alloc::borrow::Cow;
use alloc::string::String;
use core::fmt::Debug;
use writeable::Writeable;
use yoke::*;
use zerofrom::*;
/// A struct containing "Hello World" in the requested language.
#[derive(Debug, PartialEq, Clone, Yokeable, ZeroFrom)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
any(feature = "deserialize_json", feature = "datagen"),
derive(serde::Serialize)
)]
#[cfg_attr(feature = "datagen", derive(databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_provider::hello_world))]
pub struct HelloWorldV1<'data> {
/// The translation of "Hello World".
#[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"),
}
}
}
/// Marker type for [`HelloWorldV1`].
#[cfg_attr(feature = "datagen", derive(Default, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_provider::hello_world))]
#[derive(Debug)]
pub struct HelloWorldV1Marker;
impl DataMarker for HelloWorldV1Marker {
type Yokeable = HelloWorldV1<'static>;
}
impl KeyedDataMarker for HelloWorldV1Marker {
const KEY: DataKey = icu_provider::data_key!("core/helloworld@1");
}
/// A data provider returning Hello World strings in different languages.
///
/// Mostly useful for testing.
///
/// # Examples
///
/// ```
/// use icu_locid::langid;
/// use icu_provider::hello_world::*;
/// use icu_provider::prelude::*;
///
/// let german_hello_world: DataPayload<HelloWorldV1Marker> =
/// HelloWorldProvider
/// .load(DataRequest {
/// locale: &langid!("de").into(),
/// metadata: Default::default(),
/// })
/// .expect("Loading should succeed")
/// .take_payload()
/// .expect("Data should be present");
///
/// assert_eq!("Hallo Welt", german_hello_world.get().message);
/// ```
///
/// Load the reverse string using an auxiliary key:
///
/// ```
/// use icu_provider::hello_world::*;
/// use icu_provider::prelude::*;
///
/// let reverse_hello_world: DataPayload<HelloWorldV1Marker> =
/// HelloWorldProvider
/// .load(DataRequest {
/// locale: &"en-x-reverse".parse().unwrap(),
/// metadata: Default::default(),
/// })
/// .expect("Loading should succeed")
/// .take_payload()
/// .expect("Data should be present");
///
/// assert_eq!("Olleh Dlrow", reverse_hello_world.get().message);
/// ```
#[derive(Debug, PartialEq, Default)]
pub struct HelloWorldProvider;
impl HelloWorldProvider {
// Data from https://en.wiktionary.org/wiki/Hello_World#Translations
// Keep this sorted!
const DATA: &'static [(&'static str, &'static str)] = &[
("bn", "ওহে বিশ্ব"),
("cs", "Ahoj světe"),
("de", "Hallo Welt"),
("de-AT", "Servus Welt"),
("el", "Καλημέρα κόσμε"),
("en", "Hello World"),
("en-001", "Hello from 🗺️"), // WORLD
("en-002", "Hello from 🌍"), // AFRICA
("en-019", "Hello from 🌎"), // AMERICAS
("en-142", "Hello from 🌏"), // ASIA
("en-GB", "Hello from 🇬🇧"), // GREAT BRITAIN
("en-GB-u-sd-gbeng", "Hello from 🏴"), // ENGLAND
("en-x-reverse", "Olleh Dlrow"),
("eo", "Saluton, Mondo"),
("fa", "سلام دنیا"),
("fi", "hei maailma"),
("is", "Halló, heimur"),
("ja", "こんにちは世界"),
("ja-x-reverse", "界世はちにんこ"),
("la", "Ave, munde"),
("pt", "Olá, mundo"),
("ro", "Salut, lume"),
("ru", "Привет, мир"),
("sr", "Поздрав свете"),
("sr-Latn", "Pozdrav svete"),
("vi", "Xin chào thế giới"),
("zh", "你好世界"),
];
/// Converts this provider into a [`BufferProvider`] that uses JSON serialization.
#[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)] // binary_search
let data = Self::DATA
.binary_search_by(|(k, _)| req.locale.strict_cmp(k.as_bytes()).reverse())
.map(|i| Self::DATA[i].1)
.map_err(|_| DataErrorKind::MissingLocale.with_req(HelloWorldV1Marker::KEY, req))?;
Ok(DataResponse {
metadata: Default::default(),
payload: Some(DataPayload::from_static_str(data)),
})
}
}
impl DataPayload<HelloWorldV1Marker> {
/// Make a [`DataPayload`]`<`[`HelloWorldV1Marker`]`>` from a static string slice.
pub fn from_static_str(s: &'static str) -> DataPayload<HelloWorldV1Marker> {
DataPayload::from_owned(HelloWorldV1 {
message: Cow::Borrowed(s),
})
}
}
// AnyProvider support.
#[cfg(not(feature = "datagen"))]
icu_provider::impl_dynamic_data_provider!(HelloWorldProvider, [HelloWorldV1Marker,], AnyMarker);
#[cfg(feature = "deserialize_json")]
/// A data provider returning Hello World strings in different languages as JSON blobs.
///
/// Mostly useful for testing.
///
/// # Examples
///
/// ```
/// use icu_locid::langid;
/// use icu_provider::hello_world::*;
/// use icu_provider::prelude::*;
///
/// let german_hello_world = HelloWorldProvider
/// .into_json_provider()
/// .load_buffer(HelloWorldV1Marker::KEY, DataRequest {
/// locale: &langid!("de").into(),
/// metadata: Default::default(),
/// })
/// .expect("Loading should succeed")
/// .take_payload()
/// .expect("Data should be present");
///
/// assert_eq!(german_hello_world.get(), br#"{"message":"Hallo Welt"}"#);
#[derive(Debug)]
pub struct HelloWorldJsonProvider;
#[cfg(feature = "deserialize_json")]
impl BufferProvider for HelloWorldJsonProvider {
fn load_buffer(
&self,
key: DataKey,
req: DataRequest,
) -> Result<DataResponse<BufferMarker>, DataError> {
key.match_key(HelloWorldV1Marker::KEY)?;
let result = HelloWorldProvider.load(req)?;
let (mut metadata, old_payload) =
DataResponse::<HelloWorldV1Marker>::take_metadata_and_payload(result)?;
metadata.buffer_format = Some(icu_provider::buf::BufferFormat::Json);
#[allow(clippy::unwrap_used)] // HelloWorldV1::serialize is infallible
Ok(DataResponse {
metadata,
payload: Some(DataPayload::from_owned_buffer(
serde_json::to_string(old_payload.get())
.unwrap()
.into_bytes()
.into_boxed_slice(),
)),
})
}
}
#[cfg(feature = "datagen")]
impl icu_provider::datagen::IterableDataProvider<HelloWorldV1Marker> for HelloWorldProvider {
fn supported_locales(&self) -> Result<Vec<DataLocale>, DataError> {
#[allow(clippy::unwrap_used)] // datagen
Ok(Self::DATA.iter().map(|(s, _)| s.parse().unwrap()).collect())
}
}
#[cfg(feature = "datagen")]
icu_provider::make_exportable_provider!(HelloWorldProvider, [HelloWorldV1Marker,]);
/// A type that formats localized "hello world" strings.
///
/// This type is intended to take the shape of a typical ICU4X formatter API.
///
/// # Examples
///
/// ```
/// use icu_locid::locale;
/// use icu_provider::hello_world::{HelloWorldFormatter, HelloWorldProvider};
/// use writeable::assert_writeable_eq;
///
/// let fmt = HelloWorldFormatter::try_new_unstable(
/// &HelloWorldProvider,
/// &locale!("eo").into(),
/// )
/// .expect("locale exists");
///
/// assert_writeable_eq!(fmt.format(), "Saluton, Mondo");
/// ```
#[derive(Debug)]
pub struct HelloWorldFormatter {
data: DataPayload<HelloWorldV1Marker>,
}
/// A formatted hello world message. Implements [`Writeable`].
///
/// For an example, see [`HelloWorldFormatter`].
#[derive(Debug)]
pub struct FormattedHelloWorld<'l> {
data: &'l HelloWorldV1<'l>,
}
impl HelloWorldFormatter {
/// Creates a new [`HelloWorldFormatter`] for the specified locale.
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
pub fn try_new(locale: &DataLocale) -> Result<Self, DataError> {
Self::try_new_unstable(&HelloWorldProvider, locale)
}
icu_provider::gen_any_buffer_data_constructors!(locale: include, options: skip, error: DataError,
#[cfg(skip)]
functions: [
try_new,
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 {
locale,
metadata: Default::default(),
})?
.take_payload()?;
Ok(Self { data })
}
/// Formats a hello world message, returning a [`FormattedHelloWorld`].
#[allow(clippy::needless_lifetimes)] // documentary example
pub fn format<'l>(&'l self) -> FormattedHelloWorld<'l> {
FormattedHelloWorld {
data: self.data.get(),
}
}
/// Formats a hello world message, returning a [`String`].
pub fn format_to_string(&self) -> String {
self.format().write_to_string().into_owned()
}
}
impl<'l> Writeable for FormattedHelloWorld<'l> {
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 = "datagen")]
#[test]
fn test_iter() {
use crate::datagen::IterableDataProvider;
use icu_locid::locale;
assert_eq!(
HelloWorldProvider.supported_locales().unwrap(),
vec![
locale!("bn").into(),
locale!("cs").into(),
locale!("de").into(),
locale!("de-AT").into(),
locale!("el").into(),
locale!("en").into(),
locale!("en-001").into(),
locale!("en-002").into(),
locale!("en-019").into(),
locale!("en-142").into(),
locale!("en-GB").into(),
locale!("en-GB-u-sd-gbeng").into(),
"en-x-reverse".parse().unwrap(),
locale!("eo").into(),
locale!("fa").into(),
locale!("fi").into(),
locale!("is").into(),
locale!("ja").into(),
"ja-x-reverse".parse().unwrap(),
locale!("la").into(),
locale!("pt").into(),
locale!("ro").into(),
locale!("ru").into(),
locale!("sr").into(),
locale!("sr-Latn").into(),
locale!("vi").into(),
locale!("zh").into()
]
);
}