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

mod options;
mod parser;

/// A Locale object.
///
/// Locale object stores information encoded in a language tag and provides
/// methods allowing for parsing, serializing and manipulating locale fields.
///
/// All data is validated and canonicalized on input, which means that
/// the output is always canonicalized.
///
/// # Currently supported subtags are:
/// * `language` (e.g. "en")
/// * `script` (e.g. "Latn")
/// * `region` (e.g. "US")
/// * `variants` (e.g. "windows")
/// * `extensions` (e.g. "-u-ca-gregorian-hc-h12")
///
/// The API parses correctly the remaining fields of the BCP47 language tag, but
/// at the moment does not provide any API for operating on them.
///
/// # Examples
///
/// ## Parsing
/// Locale supports a `From` trait from `String` and `&str`:
///
/// ```
/// use fluent_locale::Locale;
///
/// let loc = Locale::from("en-latn-us");
///
/// assert_eq!(loc.to_string(), "en-Latn-US");
/// ```
///
/// Locale can also accept options, similarly to ECMA402 Intl.Locale:
///
/// ```
/// use fluent_locale::Locale;
/// use std::collections::BTreeMap;
///
/// let mut opts = BTreeMap::new();
/// opts.insert("hour-cycle", "h12");
/// let loc = Locale::new("en", Some(opts)).unwrap();
///
/// assert_eq!(loc.to_string(), "en-u-hc-h12");
/// ```
///
/// ## Serializing
/// Locale supports `Display` trait allowing for:
///
/// ```
/// use fluent_locale::Locale;
/// use std::collections::BTreeMap;
///
/// let mut opts = BTreeMap::new();
/// opts.insert("hour-cycle", "h12");
/// let loc = Locale::new("en-Latn-US-u-hc-h23", Some(opts)).unwrap();
///
/// assert_eq!(loc.to_string(), "en-Latn-US-u-hc-h12");
/// ```
///
/// ## Manipulating
/// During the lifetime of `Locale`, its fields can be modified via getter/setter
/// methods:
///
/// ```
/// use fluent_locale::Locale;
///
/// let mut loc = Locale::from("en-Latn-US");
/// loc.set_region("GB").unwrap();
///
/// assert_eq!(loc.to_string(), "en-Latn-GB");
/// ```
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Locale {
    language: Option<String>,
    extlangs: Option<Vec<String>>,
    script: Option<String>,
    region: Option<String>,
    variants: Option<Vec<String>>,
    extensions: Option<BTreeMap<String, BTreeMap<String, String>>>,
    privateuse: Vec<String>,
}

impl Locale {
    pub fn new(loc_str: &str, opts: Option<BTreeMap<&str, &str>>) -> Result<Locale, parser::Error> {
        let mut locale = parser::parse_language_tag(loc_str)?;

        if let Some(opts) = opts {
            options::apply_options(&mut locale, opts);
        }

        Ok(locale)
    }

    pub fn set_language(&mut self, value: &str) -> parser::Result<()> {
        if !value.is_empty() && value != "und" {
            self.language = Some(parser::parse_language_subtag(value)?);
        } else {
            self.language = None;
        }
        Ok(())
    }

    pub fn get_language(&self) -> &str {
        if let Some(ref language) = self.language {
            return language.as_str();
        }
        ""
    }

    pub fn set_script(&mut self, value: &str) -> parser::Result<()> {
        if !value.is_empty() {
            self.script = Some(parser::parse_script_subtag(value)?);
        } else {
            self.script = None;
        }
        Ok(())
    }

    pub fn get_script(&self) -> &str {
        if let Some(ref script) = self.script {
            return script.as_str();
        }
        ""
    }

    pub fn set_region(&mut self, value: &str) -> parser::Result<()> {
        if !value.is_empty() {
            self.region = Some(parser::parse_region_subtag(value)?);
        } else {
            self.region = None;
        }
        Ok(())
    }

    pub fn get_region(&self) -> &str {
        if let Some(ref region) = self.region {
            return region.as_str();
        }
        ""
    }

    pub fn add_variant(&mut self, value: String) {
        if let Some(ref mut variants) = self.variants {
            variants.push(value);
        } else {
            self.variants = Some(vec![value]);
        }
    }

    pub fn remove_variant(&mut self, value: String) {
        if let Some(ref mut variants) = self.variants {
            if let Some(position) = variants.iter().position(|x| *x == *value) {
                variants.remove(position);
            }
        }
    }

    pub fn get_variants(&self) -> Vec<&String> {
        self.variants
            .as_ref()
            .map_or(Vec::new(), |v| v.iter().collect())
    }

    pub fn clear_variants(&mut self) {
        self.variants = None;
    }

    pub fn has_privateuse(&self) -> bool {
        !self.privateuse.is_empty()
    }

    pub fn get_extensions(&self) -> BTreeMap<String, &BTreeMap<String, String>> {
        self.extensions.as_ref().map_or(BTreeMap::new(), |map| {
            map.iter()
                .map(|(key, value)| (key.clone(), value))
                .collect()
        })
    }

    pub fn add_extension(&mut self, ext_name: String, key: String, value: String) {
        if let Some(ref mut extensions) = self.extensions {
            let ext = extensions.entry(ext_name).or_insert_with(BTreeMap::new);
            ext.insert(key, value);
        } else {
            let mut exts = BTreeMap::new();
            let mut ext = BTreeMap::new();
            ext.insert(key, value);
            exts.insert(ext_name, ext);
            self.extensions = Some(exts);
        }
    }

    pub fn matches(&self, other: &Locale, available_range: bool, requested_range: bool) -> bool {
        if !self.privateuse.is_empty() || other.has_privateuse() {
            return false;
        }

        if (!available_range || !self.language.is_none())
            && (!requested_range || !other.get_language().is_empty())
            && self.get_language() != other.get_language()
        {
            return false;
        }

        if (!available_range || !self.script.is_none())
            && (!requested_range || !other.get_script().is_empty())
            && self.get_script() != other.get_script()
        {
            return false;
        }

        if (!available_range || !self.region.is_none())
            && (!requested_range || !other.get_region().is_empty())
            && self.get_region() != other.get_region()
        {
            return false;
        }

        if (!available_range || !self.variants.is_none())
            && (!requested_range || !other.get_variants().is_empty())
            && self.get_variants() != other.get_variants()
        {
            return false;
        }

        true
    }
}

impl From<String> for Locale {
    fn from(s: String) -> Self {
        Locale::new(s.as_str(), None).unwrap_or_else(|_| Locale::new("", None).unwrap())
    }
}

impl<'a> From<&'a str> for Locale {
    fn from(s: &'a str) -> Self {
        Locale::new(s, None).unwrap_or_else(|_| Locale::new("", None).unwrap())
    }
}

impl fmt::Display for Locale {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut subtags = vec![];

        subtags.push(self.get_language());

        if let Some(ref script) = self.script {
            subtags.push(script);
        }

        if let Some(ref region) = self.region {
            subtags.push(region);
        }

        if let Some(ref variants) = self.variants {
            for variant in variants {
                subtags.push(variant);
            }
        }

        if let Some(ref extensions) = self.extensions {
            for (name, ext) in extensions {
                subtags.push(parser::ext_key_for_name(name));

                for (key, value) in ext {
                    subtags.push(options::option_key_for_name(key));
                    subtags.push(value);
                }
            }
        }

        write!(f, "{}", subtags.join("-"))
    }
}