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
use std::collections::HashMap;

use regex::Regex;

use super::{Context, JSONGetTextBuilder};
use crate::{JSONGetTextBuildError, JSONGetTextValue};

/// A wrapper for context and a default key. **Keys** are usually considered as locales.
#[derive(Debug)]
pub struct JSONGetText<'a> {
    default_key: String,
    context:     Context<'a>,
}

impl<'a> JSONGetText<'a> {
    /// Create a new `JSONGetTextBuilder` instance. You need to decide your default key at the stage.
    #[inline]
    pub fn build<S: Into<String>>(default_key: S) -> JSONGetTextBuilder<'a> {
        JSONGetTextBuilder::new(default_key)
    }

    /// Create a new JSONGetText instance with context and a default key.
    pub(crate) fn from_context_with_default_key<S: AsRef<str> + Into<String>>(
        default_key: S,
        mut context: Context<'a>,
    ) -> Result<JSONGetText<'a>, JSONGetTextBuildError> {
        if !context.contains_key(default_key.as_ref()) {
            return Err(JSONGetTextBuildError::DefaultKeyNotFound);
        }

        let default_key = default_key.into();

        let default_map = context.remove(&default_key).unwrap();

        let mut inner_context = HashMap::new();

        {
            for (key, mut map) in context {
                {
                    for map_key in map.keys() {
                        if !default_map.contains_key(map_key) {
                            return Err(JSONGetTextBuildError::TextInKeyNotInDefaultKey {
                                key,
                                text: map_key.clone(),
                            });
                        }
                    }
                }

                {
                    for map_key in default_map.keys() {
                        if !map.contains_key(map_key) {
                            map.insert(map_key.clone(), default_map.get(map_key).unwrap().clone());
                        }
                    }
                }

                inner_context.insert(key, map);
            }

            inner_context.insert(default_key.clone().into(), default_map);
        }

        Ok(JSONGetText {
            default_key,
            context: inner_context,
        })
    }

    /// Get all keys in context.
    pub fn get_keys(&self) -> Vec<&str> {
        self.context.keys().map(|key| key.as_str()).collect()
    }

    /// Returns `true` if the context contains a value for the specified key.
    #[inline]
    pub fn contains_key<K: AsRef<str>>(&self, key: K) -> bool {
        self.context.contains_key(key.as_ref())
    }

    /// Get the default key.
    #[inline]
    pub fn get_default_key(&self) -> &str {
        &self.default_key
    }

    /// Get a string map from context by a key.
    #[inline]
    pub fn get<K: AsRef<str>>(&self, key: K) -> &HashMap<String, JSONGetTextValue<'a>> {
        match self.context.get(key.as_ref()) {
            Some(m) => m,
            None => self.context.get(&self.default_key).unwrap(),
        }
    }

    /// Get text from context.
    #[inline]
    pub fn get_text<T: AsRef<str>>(&'a self, text: T) -> Option<JSONGetTextValue<'a>> {
        let map = self.context.get(&self.default_key).unwrap();

        map.get(text.as_ref()).map(|v| v.clone_borrowed())
    }

    /// Get text from context with a specific key.
    #[inline]
    pub fn get_text_with_key<K: AsRef<str>, T: AsRef<str>>(
        &'a self,
        key: K,
        text: T,
    ) -> Option<JSONGetTextValue<'a>> {
        let map = self
            .context
            .get(key.as_ref())
            .unwrap_or_else(|| self.context.get(&self.default_key).unwrap());

        map.get(text.as_ref()).map(|v| v.clone_borrowed())
    }

    /// Get multiple text from context. The output map is usually used for serialization.
    pub fn get_multiple_text<'b, T: AsRef<str> + ?Sized>(
        &self,
        text_array: &[&'b T],
    ) -> Option<HashMap<&'b str, JSONGetTextValue>> {
        let map = self.context.get(&self.default_key).unwrap();

        let mut new_map = HashMap::new();

        for &text in text_array.iter() {
            let text = text.as_ref();
            let value = map.get(text)?;
            new_map.insert(text, value.clone_borrowed());
        }

        Some(new_map)
    }

    /// Get multiple text from context with a specific key. The output map is usually used for serialization.
    pub fn get_multiple_text_with_key<'b, K: AsRef<str>, T: AsRef<str> + ?Sized>(
        &'a self,
        key: K,
        text_array: &[&'b T],
    ) -> Option<HashMap<&'b str, JSONGetTextValue<'a>>> {
        let map = self
            .context
            .get(key.as_ref())
            .unwrap_or_else(|| self.context.get(&self.default_key).unwrap());

        let mut new_map = HashMap::new();

        for &text in text_array.iter() {
            let text = text.as_ref();
            let value = map.get(text)?;
            new_map.insert(text, value.clone_borrowed());
        }

        Some(new_map)
    }

    /// Get filtered text from context by a Regex instance. The output map is usually used for serialization.
    pub fn get_filtered_text(
        &'a self,
        regex: &Regex,
    ) -> Option<HashMap<&'a str, JSONGetTextValue<'a>>> {
        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.clone_borrowed());
        }

        Some(new_map)
    }

    /// Get filtered text from context with a specific key by a Regex instance. The output map is usually used for serialization.
    pub fn get_filtered_text_with_key<K: AsRef<str>>(
        &'a self,
        key: K,
        regex: &Regex,
    ) -> Option<HashMap<&'a str, JSONGetTextValue<'a>>> {
        let map = self
            .context
            .get(key.as_ref())
            .unwrap_or_else(|| 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.clone_borrowed());
        }

        Some(new_map)
    }
}