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
pub mod errors; pub mod extensions; pub mod parser; use errors::LocaleError; pub use extensions::{ExtensionType, ExtensionsMap, UnicodeExtensionKey}; use std::convert::TryFrom; pub use unic_langid::LanguageIdentifier; #[derive(Debug, Default, PartialEq, Clone)] pub struct Locale { pub langid: LanguageIdentifier, pub extensions: extensions::ExtensionsMap, } impl Locale { pub fn new() -> Self { Default::default() } pub fn from_parts<S: AsRef<str>>( language: Option<S>, script: Option<S>, region: Option<S>, variants: Option<&[S]>, extensions: Option<extensions::ExtensionsMap>, ) -> Result<Self, LocaleError> { let langid = LanguageIdentifier::from_parts(language, script, region, variants)?; Ok(Locale { langid, extensions: extensions.unwrap_or_default(), }) } pub fn matches(&self, other: &Self, self_as_range: bool, other_as_range: bool) -> bool { if !self.extensions.get_private().is_empty() || !other.extensions.get_private().is_empty() { return false; } self.langid .matches(&other.langid, self_as_range, other_as_range) } pub fn get_language(&self) -> &str { self.langid.get_language() } pub fn set_language(&mut self, language: Option<&str>) -> Result<(), LocaleError> { self.langid .set_language(language) .map_err(std::convert::Into::into) } pub fn get_script(&self) -> &Option<String> { self.langid.get_script() } pub fn set_script(&mut self, script: Option<&str>) -> Result<(), LocaleError> { self.langid .set_script(script) .map_err(std::convert::Into::into) } pub fn get_region(&self) -> &Option<String> { self.langid.get_region() } pub fn set_region(&mut self, region: Option<&str>) -> Result<(), LocaleError> { self.langid .set_region(region) .map_err(std::convert::Into::into) } pub fn get_variants(&self) -> &[String] { self.langid.get_variants() } pub fn set_variants(&mut self, variants: &[&str]) -> Result<(), LocaleError> { self.langid .set_variants(variants) .map_err(std::convert::Into::into) } pub fn set_extension( &mut self, extension: ExtensionType, key: &str, value: Option<&str>, ) -> Result<(), LocaleError> { match extension { ExtensionType::Unicode => { let k = UnicodeExtensionKey::try_from(key)?; self.extensions.set_unicode_value(k, value) } _ => unimplemented!(), } } } impl TryFrom<&str> for Locale { type Error = LocaleError; fn try_from(source: &str) -> Result<Self, Self::Error> { parser::parse_locale(source).map_err(std::convert::Into::into) } } impl TryFrom<String> for Locale { type Error = LocaleError; fn try_from(source: String) -> Result<Self, Self::Error> { parser::parse_locale(&source).map_err(std::convert::Into::into) } } impl From<LanguageIdentifier> for Locale { fn from(langid: LanguageIdentifier) -> Self { Locale { langid, extensions: ExtensionsMap::default(), } } } impl Into<LanguageIdentifier> for Locale { fn into(self) -> LanguageIdentifier { self.langid } } impl AsRef<LanguageIdentifier> for Locale { fn as_ref(&self) -> &LanguageIdentifier { &self.langid } } impl std::fmt::Display for Locale { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let mut subtags = vec![self.langid.to_string()]; let ext = self.extensions.to_string(); if !ext.is_empty() { subtags.push(ext); } write!(f, "{}", subtags.join("-")) } } pub fn canonicalize(input: &str) -> Result<String, LocaleError> { let locale = Locale::try_from(input)?; Ok(locale.to_string()) }