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
/*! Provides a layer above the `LocaleString` for different locale specifiers. The `Locale` enum represents three forms of locale specification supported by the POSIX C API. These are: 1. The identifier "POSIX", or "C" is known as the _minimal locale_. It is a rather neutral locale which has the same settings across all systems and compilers, and therefore the exact results of a program using this locale are predictable. This is the locale used by default on all C programs. 2. A path, starting with the '/' character and which resolves to a directory containing the POSIX definition of a locale. 3. A locale string, represented in this crate as a `LocaleString` structure that effectively represents a language with cultural qualification. The `Locale::from_str` method can be used to parse any of these kinds into the separate enumeration values. ## Examples ``` use simple_locale::locale::Locale; use std::str::FromStr; match Locale::from_str("C") { Ok(Locale::POSIX) => (), _ => panic!("could not parse first locale string") } let locale = Locale::from_str("en_US.UTF-8@Latn"); if let Ok(Locale::String(locale_str)) = locale { assert_eq!(locale_str.get_language_code(), "en".to_string()); assert_eq!(locale_str.get_territory().unwrap(), "US".to_string()); assert_eq!(locale_str.get_code_set().unwrap(), "UTF-8".to_string()); assert_eq!(locale_str.get_modifier().unwrap(), "Latn".to_string()); } else { panic!("could not parse second locale string") } ``` The following example is a more complete use case, the need to parse the commonly used `LC_ALL` environment variable to determine it's type and potential components. ``` use simple_locale::locale::Locale; use std::str::FromStr; use std::env; if let Ok(lc_str) = env::var("LC_ALL") { match Locale::from_str(&lc_str) { Ok(Locale::POSIX) => println!("POSIX minimal locale"), Ok(Locale::Path(p)) => println!("Path locale"), Ok(Locale::String(s)) => println!("String locale"), _ => panic!("Parse Error"), } } ``` */ use std::fmt; use std::fmt::Display; use std::path::PathBuf; use std::str::FromStr; use crate::string::{LocaleString, ParseError}; // ------------------------------------------------------------------------------------------------ // Public Types // ------------------------------------------------------------------------------------------------ /// This enumeration represents the three types of Locale specifiers /// commonly used by operating systems. #[derive(Debug, PartialEq)] pub enum Locale { /// The minimal locale specified by POSIX. Can be spoecified with /// the string "POSIX" or simply "C". POSIX, /// A path to a locale specification, this library does not vslidste /// whether the path exists, simply that it is a valid `PathBuf`.. Path(PathBuf), /// A locale string, parsed into a structured `LocaleString` form. String(LocaleString), } // ------------------------------------------------------------------------------------------------ // Implementations - Locale // ------------------------------------------------------------------------------------------------ const L_C: &'static str = "C"; const L_POSIX: &'static str = "POSIX"; const L_PATH_SEP: &'static str = "/"; impl Display for Locale { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "{}", match self { Locale::POSIX => L_POSIX.to_string(), Locale::Path(s) => s.to_str().unwrap().to_string(), Locale::String(s) => s.to_string(), } ) } } impl FromStr for Locale { type Err = ParseError; fn from_str(s: &str) -> Result<Self, Self::Err> { if s.len() == 0 { return Err(ParseError::EmptyString); } match s { L_C => Ok(Locale::POSIX), L_POSIX => Ok(Locale::POSIX), _ => { if s.starts_with(L_PATH_SEP) { match PathBuf::from_str(s) { Ok(p) => Ok(Locale::Path(p)), Err(_) => Err(ParseError::InvalidPath), } } else { Ok(Locale::String(LocaleString::from_str(s)?)) } } } } } // ------------------------------------------------------------------------------------------------ // Unit Tests // ------------------------------------------------------------------------------------------------ #[cfg(test)] mod tests { use std::path::PathBuf; use super::*; // -------------------------------------------------------------------------------------------- #[test] fn test_posix_to_string() { assert_eq!(Locale::POSIX.to_string(), "POSIX"); } #[test] fn test_path_to_string() { let _path = PathBuf::from_str("/usr/share/locale/en_US"); // assert_eq!(path.to_string(), "/usr/share/locale/en_US"); } #[test] fn test_string_to_string() { let locale = LocaleString::new("en".to_string()) .with_territory("US".to_string()) .with_code_set("UTF-8".to_string()); assert_eq!(locale.to_string(), "en_US.UTF-8"); } // -------------------------------------------------------------------------------------------- #[test] fn test_posix_from_string() { match Locale::from_str("POSIX") { Ok(Locale::POSIX) => (), _ => panic!("expecting Locale::POSIX"), } match Locale::from_str("C") { Ok(Locale::POSIX) => (), _ => panic!("expecting Locale::POSIX (C)"), } } #[test] fn test_path_from_string() { match Locale::from_str("/usr/share/locale/en_US") { Ok(Locale::Path(p)) => assert_eq!(p.to_str(), Some("/usr/share/locale/en_US")), _ => panic!("expecting Locale::Path"), } } #[test] fn test_string_from_string() { println!("{:#?}", Locale::from_str("en_US.UTF-8")); match Locale::from_str("en_US.UTF-8") { Ok(Locale::String(ls)) => { assert_eq!(ls.get_language_code(), "en"); assert_eq!(ls.get_territory(), Some("US".to_string())); assert_eq!(ls.get_code_set(), Some("UTF-8".to_string())); } _ => panic!("expecting Locale::String"), } } }