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
#![cfg_attr(feature = "nightly", feature(map_get_key_value))]
extern crate serde_json;
extern crate regex;
use std::collections::HashMap;
use std::path::Path;
use std::fs::File;
use regex::Regex;
type Context = HashMap<String, HashMap<String, String>>;
#[derive(Debug)]
pub struct JSONGetTextBuilder {
default_key: String,
context: Context,
}
impl JSONGetTextBuilder {
pub fn new(default_key: &str) -> JSONGetTextBuilder {
JSONGetTextBuilder {
default_key: default_key.to_string(),
context: HashMap::new(),
}
}
pub fn add_json_string_to_context(&mut self, key: &str, json: &str) -> Result<&Self, String> {
if self.context.contains_key(key) {
return Err("The key exists.".to_string());
}
let map: HashMap<String, String> = serde_json::from_str(json).map_err(|err| { err.to_string() })?;
self.context.insert(key.to_string(), map);
Ok(self)
}
pub fn add_json_bytes_to_context(&mut self, key: &str, json: &[u8]) -> Result<&Self, String> {
if self.context.contains_key(key) {
return Err("The key exists.".to_string());
}
let map: HashMap<String, String> = serde_json::from_slice(json).map_err(|err| { err.to_string() })?;
self.context.insert(key.to_string(), map);
Ok(self)
}
pub fn add_json_file_to_context<P: AsRef<Path>>(&mut self, key: &str, path: P) -> Result<&Self, String> {
if self.context.contains_key(key) {
return Err("The key exists.".to_string());
}
let file = File::open(path).map_err(|err| { err.to_string() })?;
let map: HashMap<String, String> = serde_json::from_reader(&file).map_err(|err| { err.to_string() })?;
self.context.insert(key.to_string(), map);
Ok(self)
}
pub fn add_map_to_context<P: AsRef<Path>>(&mut self, key: &str, map: HashMap<String, String>) -> Result<&Self, String> {
if self.context.contains_key(key) {
return Err("The key exists.".to_string());
}
self.context.insert(key.to_string(), map);
Ok(self)
}
pub fn build(self) -> Result<JSONGetText, String> {
JSONGetText::from_context_inner(self.default_key, self.context)
}
}
#[derive(Debug)]
pub struct JSONGetText {
default_key: String,
context: Context,
}
impl JSONGetText {
pub fn build(default_key: &str) -> JSONGetTextBuilder {
JSONGetTextBuilder::new(default_key)
}
pub fn from_context(default_key: &str, context: Context) -> Result<JSONGetText, String> {
JSONGetText::from_context_inner(default_key.to_string(), context)
}
fn from_context_inner(default_key: String, mut context: Context) -> Result<JSONGetText, String> {
if !context.contains_key(&default_key) {
return Err("Cannot find the default key in the context.".to_string());
}
if !context.contains_key(&default_key) {
return Err("Context should contain the default key.".to_string());
}
{
let default_map = context.remove(&default_key).unwrap();
for (key, map) in context.iter_mut() {
{
let map_keys = map.keys();
for map_key in map_keys {
if !default_map.contains_key(map_key) {
return Err(format! {"The text `{}` in the key `{}` is not in the map of the default key.", map_key, key});
}
}
}
let map_keys = default_map.keys();
for map_key in map_keys {
if !map.contains_key(map_key) {
map.insert(map_key.clone(), default_map.get(map_key).unwrap().clone());
}
}
}
context.insert(default_key.clone(), default_map);
}
Ok(JSONGetText {
default_key: default_key,
context,
})
}
pub fn get_keys(&self) -> Vec<&str> {
let mut vec = Vec::new();
for key in self.context.keys() {
vec.push(key.as_str());
}
vec
}
pub fn get_default_key(&self) -> &str {
&self.default_key
}
pub fn get(&self, key: &str) -> &HashMap<String, String> {
match self.context.get(key) {
Some(m) => m,
None => self.context.get(&self.default_key).unwrap()
}
}
pub fn get_text(&self, text: &str) -> Option<&str> {
let map = self.context.get(&self.default_key).unwrap();
map.get(text).map(|s| s.as_str())
}
pub fn get_text_with_key(&self, key: &str, text: &str) -> Option<&str> {
let map = match self.context.get(key) {
Some(m) => m,
None => self.context.get(&self.default_key).unwrap()
};
map.get(text).map(|s| s.as_str())
}
#[cfg(feature = "nightly")]
pub fn get_multiple_text(&self, text_array: &[&str]) -> Option<HashMap<&str, &str>> {
let map = self.context.get(&self.default_key).unwrap();
let mut new_map = HashMap::new();
for &text in text_array.iter() {
let (key, value) = map.get_key_value(text)?;
new_map.insert(key.as_str(), value.as_str());
}
Some(new_map)
}
#[cfg(feature = "nightly")]
pub fn get_multiple_text_with_key(&self, key: &str, text_array: &[&str]) -> Option<HashMap<&str, &str>> {
let map = match self.context.get(key) {
Some(m) => m,
None => self.context.get(&self.default_key).unwrap()
};
let mut new_map = HashMap::new();
for &text in text_array.iter() {
let (key, value) = map.get_key_value(text)?;
new_map.insert(key.as_str(), value.as_str());
}
Some(new_map)
}
pub fn get_filtered_text(&self, regex: &Regex) -> Option<HashMap<&str, &str>> {
let map = self.context.get(&self.default_key).unwrap();
let mut new_map = HashMap::new();
for (key, value) in map.iter() {
if !regex.is_match(key) {
continue;
}
new_map.insert(key.as_str(), value.as_str());
}
Some(new_map)
}
pub fn get_filtered_text_with_key(&self, key: &str, regex: &Regex) -> Option<HashMap<&str, &str>> {
let map = match self.context.get(key) {
Some(m) => m,
None => self.context.get(&self.default_key).unwrap()
};
let mut new_map = HashMap::new();
for (key, value) in map.iter() {
if !regex.is_match(key) {
continue;
}
new_map.insert(key.as_str(), value.as_str());
}
Some(new_map)
}
}
#[macro_export]
macro_rules! static_json_gettext_build {
( $default_key:expr, $($key:expr, $path:expr), * ) => {
{
use self::json_gettext::JSONGetText;
let mut builder = JSONGetText::build($default_key);
$(
{
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));
builder.add_json_bytes_to_context($key, data).unwrap();
}
)*
builder.build()
}
};
}
#[macro_export]
macro_rules! get_text {
( $ctx:ident, $text:expr ) => {
{
$ctx.get_text($text)
}
};
( $ctx:ident, $key:expr, $text:expr ) => {
{
$ctx.get_text_with_key($key, $text)
}
};
( $ctx:ident, $key:expr, $text:expr, $($text_array:expr), + ) => {
{
let mut text_array = vec![$text];
$(
{
text_array.push($text_array);
}
)*
$ctx.get_multiple_text_with_key($key, &text_array)
}
};
}