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
use crate::iter::IterableDataProviderCore;
use crate::prelude::*;
use icu_locid::LanguageIdentifier;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Debug;
use std::str::FromStr;
pub mod key {
use crate::resource::ResourceKey;
pub const HELLO_WORLD_V1: ResourceKey = resource_key!(icu4x, "helloworld", 1);
}
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct HelloWorldV1<'s> {
#[cfg_attr(feature = "provider_serde", serde(borrow))]
pub message: Cow<'s, str>,
}
impl Default for HelloWorldV1<'_> {
fn default() -> Self {
HelloWorldV1 {
message: Cow::Borrowed("(und) Hello World"),
}
}
}
#[derive(Debug, PartialEq, Default)]
pub struct HelloWorldProvider<'s> {
map: HashMap<LanguageIdentifier, Cow<'s, str>>,
}
impl<'s> HelloWorldProvider<'s> {
pub fn new_with_placeholder_data() -> HelloWorldProvider<'static> {
HelloWorldProvider {
map: [
("bn", "ওহে বিশ্ব"),
("cs", "Ahoj světe"),
("de", "Hallo Welt"),
("el", "Καλημέρα κόσμε"),
("en", "Hello World"),
("eo", "Saluton, Mondo"),
("fa", "سلام دنیا"),
("fi", "hei maailma"),
("is", "Halló, heimur"),
("ja", "こんにちは世界"),
("la", "Ave, munde"),
("ro", "Salut,lume!"),
("ru", "Привет, мир"),
("vi", "Xin chào thế giới"),
("zh", "你好世界"),
]
.iter()
.map(|(loc, value)| {
(
LanguageIdentifier::from_str(loc).unwrap(),
Cow::Borrowed(*value),
)
})
.collect(),
}
}
}
impl<'d, 's> DataProvider<'d, HelloWorldV1<'s>> for HelloWorldProvider<'s>
where
's: 'd,
{
fn load_payload(
&self,
req: &DataRequest,
) -> Result<DataResponse<'d, HelloWorldV1<'s>>, DataError> {
req.resource_path.key.match_key(key::HELLO_WORLD_V1)?;
let langid = req.try_langid()?;
let data = self
.map
.get(langid)
.map(|s| HelloWorldV1 { message: s.clone() })
.ok_or_else(|| DataError::UnavailableResourceOptions(req.clone()))?;
Ok(DataResponse {
metadata: DataResponseMetadata {
data_langid: Some(langid.clone()),
},
payload: DataPayload {
cow: Some(Cow::Owned(data)),
},
})
}
}
impl_dyn_provider!(HelloWorldProvider<'static>, HelloWorldV1<'static>, ERASED, 'd, 's);
#[cfg(feature = "provider_serde")]
impl_dyn_provider!(HelloWorldProvider<'s>, HelloWorldV1<'s>, SERDE_SE, 'd, 's);
impl<'d> IterableDataProviderCore for HelloWorldProvider<'d> {
fn supported_options_for_key(
&self,
resc_key: &ResourceKey,
) -> Result<Box<dyn Iterator<Item = ResourceOptions>>, DataError> {
resc_key.match_key(key::HELLO_WORLD_V1)?;
let list: Vec<ResourceOptions> = self
.map
.keys()
.map(|langid| ResourceOptions {
variant: None,
langid: Some(langid.clone()),
})
.collect();
Ok(Box::new(list.into_iter()))
}
}
impl crate::export::DataExporter<'_, dyn crate::erased::ErasedDataStruct>
for HelloWorldProvider<'static>
{
fn put_payload(
&mut self,
req: &DataRequest,
payload: &dyn crate::erased::ErasedDataStruct,
) -> Result<(), Box<dyn std::error::Error>> {
req.resource_path.key.match_key(key::HELLO_WORLD_V1)?;
let langid = req.try_langid()?;
let data: &HelloWorldV1 = payload.downcast_ref()?;
self.map.insert(langid.clone(), data.message.clone());
Ok(())
}
fn include_resource_options(&self, _resc_options: &ResourceOptions) -> bool {
true
}
}