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
363
//! # The XML `<localization>` format
//!
//! This is used in:
//! - the `locale/locale.xml` file

use std::{
    collections::{btree_map, BTreeMap},
    fmt,
    fs::File,
    io::{self, BufReader},
    ops::Deref,
    path::Path,
};

use displaydoc::Display;
use quick_xml::{events::Event as XmlEvent, Error as XmlError, Reader as XmlReader};
use thiserror::Error;

use super::common::exact::{expect_attribute, expect_end, expect_start, expect_text, Error};
use crate::common::exact::{expect_child_or_end, expect_text_or_end};

mod interner;
pub use interner::Interner;

#[derive(Debug, Display, Error)]
/// Some problem with loading a locale file
pub enum LocaleError {
    /// I/O Error
    Io(#[from] io::Error),
    /// Xml
    Xml(#[from] Error),
}

impl From<XmlError> for LocaleError {
    fn from(e: XmlError) -> Self {
        Self::Xml(Error::Xml(e))
    }
}

/// A key for [LocaleNode] children
pub type Key = interner::StringKey; //inystr::TinyAsciiStr<24>;

#[derive(Debug, Default)]
/// A node in the locale tree
pub struct LocaleNode {
    /// The translation at the current node
    pub value: Option<String>,
    /// The (optional) children with a numeric key
    pub int_children: BTreeMap<u32, LocaleNode>,
    /// The (optional) children with a non-numeric key
    pub str_children: BTreeMap<Key, LocaleNode>,
}

impl LocaleNode {
    /// Return all keys that correspond to this node
    ///
    /// This returns a flat map of locale values
    pub fn get_keys(&self, strs: &Interner) -> BTreeMap<String, String> {
        let mut keys = BTreeMap::new();
        for (key, value) in &self.str_children {
            value.add_keys(&mut keys, strs.lookup(*key).to_string(), strs);
        }
        for (key, value) in &self.int_children {
            value.add_keys(&mut keys, key.to_string(), strs);
        }
        keys
    }

    fn add_keys(&self, keys: &mut BTreeMap<String, String>, prefix: String, strs: &Interner) {
        for (key, value) in &self.str_children {
            let inner = format!("{}_{}", prefix, strs.lookup(*key));
            value.add_keys(keys, inner, strs);
        }
        for (key, value) in &self.int_children {
            let inner = format!("{}_{}", prefix, key);
            value.add_keys(keys, inner, strs);
        }
        if let Some(v) = &self.value {
            keys.insert(prefix, v.clone());
        }
    }
}

#[derive(Debug)]
/// The root of a loaded locale XML
pub struct LocaleRoot {
    /// The inner root node
    root_node: LocaleNode,
    /// The string interner
    strs: Interner,
}

impl LocaleRoot {
    /// Turn root into a reference
    pub fn as_ref(&self) -> LocaleNodeRef<'_, '_> {
        LocaleNodeRef {
            node: &self.root_node,
            strs: &self.strs,
        }
    }

    /// Get the string interner in this tree
    pub fn strs(&self) -> &Interner {
        &self.strs
    }

    /// Get a mutable reference to the string interner in this tree
    pub fn strs_mut(&mut self) -> &mut Interner {
        &mut self.strs
    }
}

/// Iterator over String subkeys
pub struct StrKey<'s> {
    key: Key,
    strs: &'s Interner,
}

impl fmt::Display for StrKey<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.deref().fmt(f)
    }
}

impl<'s> StrKey<'s> {
    fn new(key: Key, strs: &'s Interner) -> Self {
        Self { key, strs }
    }

    /// Get the interner [Key] for this value
    pub fn key(&self) -> Key {
        self.key
    }
}

impl<'s> Deref for StrKey<'s> {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.strs.lookup(self.key)
    }
}

/// Iterator over String subkeys
#[derive(Clone)]
pub struct StrNodeMap<'a, 's> {
    iter: btree_map::Iter<'a, Key, LocaleNode>,
    strs: &'s Interner,
}

impl<'a, 's> Iterator for StrNodeMap<'a, 's> {
    type Item = (StrKey<'s>, LocaleNodeRef<'a, 's>);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|(key, node)| {
            let k = StrKey::new(*key, self.strs);
            let r = LocaleNodeRef::new(node, self.strs);
            (k, r)
        })
    }
}

/// Iterator over int subkeys
#[derive(Clone)]
pub struct IntNodeMap<'a, 's> {
    iter: btree_map::Iter<'a, u32, LocaleNode>,
    strs: &'s Interner,
}

impl<'a, 's> Iterator for IntNodeMap<'a, 's> {
    type Item = (u32, LocaleNodeRef<'a, 's>);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|(key, node)| {
            let r = LocaleNodeRef {
                node,
                strs: self.strs,
            };
            (*key, r)
        })
    }
}

/// Reference to a [LocaleNode] with an [Interner]
#[derive(Clone)]
pub struct LocaleNodeRef<'a, 's> {
    node: &'a LocaleNode,
    strs: &'s Interner,
}

impl<'a, 's> LocaleNodeRef<'a, 's> {
    /// Get the string interner in this tree
    pub fn strs(&self) -> &'s Interner {
        self.strs
    }

    /// Get the actual node
    pub fn node(&self) -> &'a LocaleNode {
        self.node
    }

    /// Get the value of this [LocaleNode]
    pub fn value(&self) -> Option<&str> {
        self.node.value.as_deref()
    }

    /// Call [`LocaleNode::get_keys`] with the [`Interner`]
    pub fn get_keys(&self) -> BTreeMap<String, String> {
        self.node.get_keys(self.strs)
    }

    /// Get the string children of this [LocaleNode]
    pub fn str_child_iter(&self) -> StrNodeMap<'a, 's> {
        StrNodeMap {
            iter: self.node.str_children.iter(),
            strs: self.strs,
        }
    }

    /// Get an integer child
    pub fn get_int(&self, key: u32) -> Option<Self> {
        if let Some(node) = self.node.int_children.get(&key) {
            return Some(Self::new(node, self.strs));
        }
        None
    }

    /// Get an integer child
    pub fn get_str(&self, key: Key) -> Option<LocaleNodeRef<'a, 's>> {
        if let Some(node) = self.node.str_children.get(&key) {
            return Some(LocaleNodeRef::new(node, self.strs));
        }
        None
    }

    /// Get the integer children of this [LocaleNode]
    pub fn int_child_iter(&self) -> IntNodeMap<'a, 's> {
        IntNodeMap {
            iter: self.node.int_children.iter(),
            strs: self.strs,
        }
    }

    fn new(node: &'a LocaleNode, strs: &'s Interner) -> Self {
        Self { node, strs }
    }
}

const TAG_LOCALIZATION: &str = "localization";
const TAG_LOCALES: &str = "locales";
const TAG_LOCALE: &str = "locale";
const TAG_PHRASES: &str = "phrases";
const TAG_PHRASE: &str = "phrase";
const TAG_TRANSLATION: &str = "translation";

const ATTR_COUNT: &str = "count";
const ATTR_LOCALE: &str = "locale";
const ATTR_ID: &str = "id";

const LOCALE_EN_US: &str = "en_US";

/// Load a locale file
pub fn load_locale(path: &Path) -> Result<LocaleRoot, LocaleError> {
    let file = File::open(path)?;
    let file = BufReader::new(file);

    let mut root_node = LocaleNode {
        value: None,
        int_children: BTreeMap::new(),
        str_children: BTreeMap::new(),
    };
    let mut strs = Interner::with_capacity(0x4000);

    let mut reader = XmlReader::from_reader(file);
    reader.trim_text(true);

    let mut buf = Vec::new();

    // The `Reader` does not implement `Iterator` because it outputs borrowed data (`Cow`s)
    if let Ok(XmlEvent::Decl(_)) = reader.read_event(&mut buf) {}
    buf.clear();

    let _ = expect_start(TAG_LOCALIZATION, &mut reader, &mut buf)?;
    buf.clear();

    let e_locales = expect_start(TAG_LOCALES, &mut reader, &mut buf)?;
    let locale_count: usize = expect_attribute(ATTR_COUNT, &reader, &e_locales)?;
    let mut real_locale_count = 0;
    buf.clear();

    while expect_child_or_end(TAG_LOCALE, TAG_LOCALES, &mut reader, &mut buf)?.is_some() {
        buf.clear();

        let locale = expect_text(&mut reader, &mut buf)?;
        log::debug!("Found locale '{}'", locale);

        expect_end(TAG_LOCALE, &mut reader, &mut buf)?;
        buf.clear();

        real_locale_count += 1;
    }
    buf.clear();

    if real_locale_count != locale_count {
        log::warn!(
            "locale.xml specifies a locale count of {}, but has {}",
            locale_count,
            real_locale_count
        );
    }

    let e_locales = expect_start(TAG_PHRASES, &mut reader, &mut buf)?;
    let phrase_count: usize = expect_attribute(ATTR_COUNT, &reader, &e_locales)?;
    let mut real_phrase_count = 0;
    buf.clear();

    while let Some(e_phrase) = expect_child_or_end(TAG_PHRASE, TAG_PHRASES, &mut reader, &mut buf)?
    {
        let id: String = expect_attribute(ATTR_ID, &reader, &e_phrase)?;
        buf.clear();

        let mut translation = None;

        while let Some(e_translation) =
            expect_child_or_end(TAG_TRANSLATION, TAG_PHRASE, &mut reader, &mut buf)?
        {
            let locale: String = expect_attribute(ATTR_LOCALE, &reader, &e_translation)?;
            buf.clear();

            let trans = expect_text_or_end(TAG_TRANSLATION, &mut reader, &mut buf)?;
            if locale == LOCALE_EN_US {
                translation = Some(trans);
            }
            buf.clear();
        }
        buf.clear();

        let mut node = &mut root_node;
        for comp in id.split('_') {
            if let Ok(num) = comp.parse::<u32>() {
                node = node.int_children.entry(num).or_default();
            } else {
                let key = strs.intern(comp); // Key::from_str(comp)?;
                node = node.str_children.entry(key).or_default();
            }
        }
        if let Some(translation) = translation {
            node.value = Some(translation);
        }

        real_phrase_count += 1;
    }
    buf.clear();

    if phrase_count != real_phrase_count {
        log::warn!(
            "locale.xml specifies a count of {} phrases, but has {}",
            phrase_count,
            real_phrase_count
        );
    }

    Ok(LocaleRoot { root_node, strs })
}