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
// Copyright 2024 New Vector Ltd.
// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

use std::{
    collections::{BTreeMap, BTreeSet},
    ops::Deref,
};

use icu_plurals::PluralCategory;
use serde::{
    de::{MapAccess, Visitor},
    ser::SerializeMap,
    Deserialize, Deserializer, Serialize, Serializer,
};

use crate::sprintf::Message;

fn plural_category_as_str(category: PluralCategory) -> &'static str {
    match category {
        PluralCategory::Zero => "zero",
        PluralCategory::One => "one",
        PluralCategory::Two => "two",
        PluralCategory::Few => "few",
        PluralCategory::Many => "many",
        PluralCategory::Other => "other",
    }
}

pub type TranslationTree = Tree;

#[derive(Debug, Clone, Deserialize, Default)]
pub struct Metadata {
    #[serde(skip)]
    // We don't want to deserialize it, as we're resetting it every time
    // This then generates the `context` field when serializing
    pub context_locations: BTreeSet<String>,
    pub description: Option<String>,
}

impl Serialize for Metadata {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let context = self
            .context_locations
            .iter()
            .map(String::as_str)
            .collect::<Vec<&str>>()
            .join(", ");

        let mut map = serializer.serialize_map(None)?;

        if !context.is_empty() {
            map.serialize_entry("context", &context)?;
        }

        if let Some(description) = &self.description {
            map.serialize_entry("description", description)?;
        }

        map.end()
    }
}

impl Metadata {
    fn add_location(&mut self, location: String) {
        self.context_locations.insert(location);
    }
}

#[derive(Debug, Clone, Default)]
pub struct Tree {
    inner: BTreeMap<String, Node>,
}

#[derive(Debug, Clone)]
pub struct Node {
    metadata: Option<Metadata>,
    value: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Value {
    Tree(Tree),
    Leaf(Message),
}

impl<'de> Deserialize<'de> for Tree {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct TreeVisitor;

        impl<'de> Visitor<'de> for TreeVisitor {
            type Value = Tree;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("map")
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                let mut tree: BTreeMap<String, Node> = BTreeMap::new();
                let mut metadata_map: BTreeMap<String, Metadata> = BTreeMap::new();

                while let Some(key) = map.next_key::<String>()? {
                    if let Some(name) = key.strip_prefix('@') {
                        let metadata = map.next_value::<Metadata>()?;
                        metadata_map.insert(name.to_owned(), metadata);
                    } else {
                        let value = map.next_value::<Value>()?;
                        tree.insert(
                            key,
                            Node {
                                metadata: None,
                                value,
                            },
                        );
                    }
                }

                for (key, meta) in metadata_map {
                    if let Some(node) = tree.get_mut(&key) {
                        node.metadata = Some(meta);
                    }
                }

                Ok(Tree { inner: tree })
            }
        }

        deserializer.deserialize_any(TreeVisitor)
    }
}

impl Serialize for Tree {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(None)?;

        for (key, value) in &self.inner {
            map.serialize_entry(key, &value.value)?;
            if let Some(meta) = &value.metadata {
                map.serialize_entry(&format!("@{key}"), meta)?;
            }
        }

        map.end()
    }
}

impl Tree {
    /// Get a message from the tree by key.
    ///
    /// Returns `None` if the requested key is not found.
    #[must_use]
    pub fn message(&self, key: &str) -> Option<&Message> {
        let keys = key.split('.');
        let node = self.walk_path(keys)?;
        let message = node.value.as_message()?;
        Some(message)
    }

    /// Get a pluralized message from the tree by key and plural category.
    ///
    /// If the key doesn't have plural variants, this will return the message
    /// itself. Returns the "other" category if the requested category is
    /// not found. Returns `None` if the requested key is not found.
    #[must_use]
    pub fn pluralize(&self, key: &str, category: PluralCategory) -> Option<&Message> {
        let keys = key.split('.');
        let node = self.walk_path(keys)?;

        let subtree = match &node.value {
            Value::Leaf(message) => return Some(message),
            Value::Tree(tree) => tree,
        };

        let node = if let Some(node) = subtree.inner.get(plural_category_as_str(category)) {
            node
        } else {
            // Fallback to the "other" category
            subtree.inner.get("other")?
        };

        let message = node.value.as_message()?;
        Some(message)
    }

    #[doc(hidden)]
    pub fn set_if_not_defined<K: Deref<Target = str>, I: IntoIterator<Item = K>>(
        &mut self,
        path: I,
        value: Message,
        location: Option<String>,
    ) -> bool {
        // We're temporarily moving the tree out of the struct to be able to nicely
        // iterate on it
        let mut fake_root = Node {
            metadata: None,
            value: Value::Tree(Tree {
                inner: std::mem::take(&mut self.inner),
            }),
        };

        let mut node = &mut fake_root;
        for key in path {
            match &mut node.value {
                Value::Tree(tree) => {
                    node = tree.inner.entry(key.deref().to_owned()).or_insert(Node {
                        metadata: None,
                        value: Value::Tree(Tree::default()),
                    });
                }
                Value::Leaf(_) => {
                    panic!()
                }
            }
        }

        let replaced = match &node.value {
            Value::Tree(tree) => {
                assert!(
                    tree.inner.is_empty(),
                    "Trying to overwrite a non-empty tree"
                );

                node.value = Value::Leaf(value);
                true
            }
            Value::Leaf(_) => {
                // Do not overwrite existing values
                false
            }
        };

        if let Some(location) = location {
            node.metadata
                .get_or_insert(Metadata::default())
                .add_location(location);
        }

        // Restore the original tree at the end of the function
        match fake_root {
            Node {
                value: Value::Tree(tree),
                ..
            } => self.inner = tree.inner,
            _ => panic!("Tried to replace the root node"),
        };

        replaced
    }

    fn walk_path<K: Deref<Target = str>, I: IntoIterator<Item = K>>(
        &self,
        path: I,
    ) -> Option<&Node> {
        let mut iterator = path.into_iter();
        let next = iterator.next()?;
        self.walk_path_inner(next, iterator)
    }

    fn walk_path_inner<K: Deref<Target = str>, I: Iterator<Item = K>>(
        &self,
        next_key: K,
        mut path: I,
    ) -> Option<&Node> {
        let next = self.inner.get(&*next_key)?;

        match path.next() {
            Some(next_key) => match &next.value {
                Value::Tree(tree) => tree.walk_path_inner(next_key, path),
                Value::Leaf(_) => None,
            },
            None => Some(next),
        }
    }
}

impl Value {
    fn as_message(&self) -> Option<&Message> {
        match self {
            Value::Leaf(message) => Some(message),
            Value::Tree(_) => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sprintf::{arg_list, ArgumentList};

    #[test]
    fn test_it_works() {
        let tree = serde_json::json!({
            "hello": "world",
            "damals": {
              "about_x_hours_ago": {
                "one":   "about one hour ago",
                "other": "about %(count)s hours ago"
              }
            }
        });

        let result: Result<TranslationTree, _> = serde_json::from_value(tree);
        assert!(result.is_ok());
        let tree = result.unwrap();
        let message = tree.message("hello");
        assert!(message.is_some());
        let message = message.unwrap();
        assert_eq!(message.format(&ArgumentList::default()).unwrap(), "world");

        let message = tree.message("damals.about_x_hours_ago.one");
        assert!(message.is_some());
        let message = message.unwrap();
        assert_eq!(message.format(&arg_list!()).unwrap(), "about one hour ago");

        let message = tree.pluralize("damals.about_x_hours_ago", PluralCategory::Other);
        assert!(message.is_some());
        let message = message.unwrap();
        assert_eq!(
            message.format(&arg_list!(count = 2)).unwrap(),
            "about 2 hours ago"
        );
    }
}