| 1 |
#![allow(unknown_lints)] |
| 2 |
use std::{env, ffi::OsStr}; |
| 3 |
|
| 4 |
const LC_ALL: &str = "LC_ALL"; |
| 5 |
const LC_CTYPE: &str = "LC_CTYPE"; |
| 6 |
const LANG: &str = "LANG"; |
| 7 |
|
| 8 |
/// Environment variable access abstraction to allow testing without |
| 9 |
/// mutating env variables. |
| 10 |
/// |
| 11 |
/// Use [StdEnv] to query [std::env] |
| 12 |
trait EnvAccess { |
| 13 |
/// See also [std::env::var] |
| 14 |
fn get(&self, key: impl AsRef<OsStr>) -> Option<String>; |
| 15 |
} |
| 16 |
|
| 17 |
/// Proxy to [std::env] |
| 18 |
struct StdEnv; |
| 19 |
impl EnvAccess for StdEnv { |
| 20 |
fn get(&self, key: impl AsRef<OsStr>) -> Option<String> { |
| 21 |
env::var(key).ok() |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
pub(crate) fn get() -> impl Iterator<Item = String> { |
| 26 |
_get(&StdEnv).into_iter() |
| 27 |
} |
| 28 |
|
| 29 |
fn _get(env: &impl EnvAccess) -> Option<String> { |
| 30 |
let code = env |
| 31 |
.get(LC_ALL) |
| 32 |
.or_else(|| env.get(LC_CTYPE)) |
| 33 |
.or_else(|| env.get(LANG))?; |
| 34 |
|
| 35 |
parse_locale_code(&code) |
| 36 |
} |
| 37 |
|
| 38 |
fn parse_locale_code(code: &str) -> Option<String> { |
| 39 |
// Some locales are returned with the char encoding too: `en_US.UTF-8` |
| 40 |
// TODO: Once we bump MSRV >= 1.52, remove this allow and clean up |
| 41 |
#[allow(clippy::manual_split_once)] |
| 42 |
#[allow(clippy::needless_splitn)] |
| 43 |
code.splitn(2, '.').next().map(|s| s.replace('_', "-")) |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(test)] |
| 47 |
mod tests { |
| 48 |
use super::{parse_locale_code, EnvAccess, _get, LANG, LC_ALL, LC_CTYPE}; |
| 49 |
use std::{ |
| 50 |
collections::HashMap, |
| 51 |
ffi::{OsStr, OsString}, |
| 52 |
}; |
| 53 |
|
| 54 |
type MockEnv = HashMap<OsString, String>; |
| 55 |
impl EnvAccess for MockEnv { |
| 56 |
fn get(&self, key: impl AsRef<OsStr>) -> Option<String> { |
| 57 |
self.get(key.as_ref()).cloned() |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
const PARSE_LOCALE: &str = "fr-FR"; |
| 62 |
const LANG_PARSE_LOCALE: &str = "fr_FR"; |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn parse_identifier() { |
| 66 |
let identifier = "fr_FR.UTF-8"; |
| 67 |
assert_eq!(parse_locale_code(identifier).as_deref(), Some(PARSE_LOCALE)); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn parse_non_suffixed_identifier() { |
| 72 |
assert_eq!( |
| 73 |
parse_locale_code(PARSE_LOCALE).as_deref(), |
| 74 |
Some(PARSE_LOCALE) |
| 75 |
); |
| 76 |
|
| 77 |
assert_eq!( |
| 78 |
parse_locale_code(LANG_PARSE_LOCALE).as_deref(), |
| 79 |
Some(PARSE_LOCALE) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn env_priority() { |
| 85 |
let mut env = MockEnv::new(); |
| 86 |
assert_eq!(_get(&env), None); |
| 87 |
|
| 88 |
// These locale names are technically allowed and some systems may still |
| 89 |
// defined aliases such as these but the glibc sources mention that this |
| 90 |
// should be considered deprecated |
| 91 |
|
| 92 |
env.insert(LANG.into(), "invalid".to_owned()); |
| 93 |
assert_eq!(_get(&env).as_deref(), Some("invalid")); |
| 94 |
|
| 95 |
env.insert(LC_CTYPE.into(), "invalid-also".to_owned()); |
| 96 |
assert_eq!(_get(&env).as_deref(), Some("invalid-also")); |
| 97 |
|
| 98 |
env.insert(LC_ALL.into(), "invalid-again".to_owned()); |
| 99 |
assert_eq!(_get(&env).as_deref(), Some("invalid-again")); |
| 100 |
} |
| 101 |
} |