| 1 | //! Inspect Unix system for locale configuration |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 4 | use superrust-analyzer cargo locale_config 0.3.0 crate/::{LanguageRangerust-analyzer cargo locale_config 0.3.0 LanguageRange#,Localerust-analyzer cargo locale_config 0.3.0 Locale#}; |
| 5 | |
| 6 | fn tagrust-analyzer cargo locale_config 0.3.0 unix/tag().(s: &str) -> superrust-analyzer cargo locale_config 0.3.0 crate/::Resultrust-analyzer cargo locale_config 0.3.0 Result#<LanguageRangerust-analyzer cargo locale_config 0.3.0 LanguageRange#> { |
| 7 | LanguageRangerust-analyzer cargo locale_config 0.3.0 LanguageRange#::from_unixrust-analyzer cargo locale_config 0.3.0 impl#[`LanguageRange<'a>`]from_unix().(s).or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]or_else().(|_| LanguageRangerust-analyzer cargo locale_config 0.3.0 LanguageRange#::newrust-analyzer cargo locale_config 0.3.0 impl#[`LanguageRange<'a>`]new().(s)) |
| 8 | } |
| 9 | |
| 10 | // TODO: Read /etc/locale.alias |
| 11 | fn tag_invrust-analyzer cargo locale_config 0.3.0 unix/tag_inv().(s: &str) -> LanguageRangerust-analyzer cargo locale_config 0.3.0 LanguageRange# { |
| 12 | tagrust-analyzer cargo locale_config 0.3.0 unix/tag().(s).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(LanguageRangerust-analyzer cargo locale_config 0.3.0 LanguageRange#::invariantrust-analyzer cargo locale_config 0.3.0 impl#[`LanguageRange<'a>`]invariant().()) |
| 13 | } |
| 14 | |
| 15 | pub fn system_localerust-analyzer cargo locale_config 0.3.0 unix/system_locale().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Localerust-analyzer cargo locale_config 0.3.0 Locale#> { |
| 16 | // LC_ALL overrides everything |
| 17 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(all) = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("LC_ALL") { |
| 18 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(t) = tagrust-analyzer cargo locale_config 0.3.0 unix/tag().(all.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AsRef<str>`]as_ref().()) { |
| 19 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Localerust-analyzer cargo locale_config 0.3.0 Locale#::fromrust-analyzer cargo locale_config 0.3.0 impl#[Locale][`From<LanguageRange<'a>>`]from().(t)); |
| 20 | } |
| 21 | } |
| 22 | // LANG is default |
| 23 | let mut loc = |
| 24 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(lang) = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("LANG") { |
| 25 | Localerust-analyzer cargo locale_config 0.3.0 Locale#::fromrust-analyzer cargo locale_config 0.3.0 impl#[Locale][`From<LanguageRange<'a>>`]from().(tag_invrust-analyzer cargo locale_config 0.3.0 unix/tag_inv().(lang.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AsRef<str>`]as_ref().())) |
| 26 | } else { |
| 27 | Localerust-analyzer cargo locale_config 0.3.0 Locale#::invariantrust-analyzer cargo locale_config 0.3.0 impl#[Locale]invariant().() |
| 28 | }; |
| 29 | // category overrides |
| 30 | for &(cat, var) in [ |
| 31 | ("ctype", "LC_CTYPE"), |
| 32 | ("numeric", "LC_NUMERIC"), |
| 33 | ("time", "LC_TIME"), |
| 34 | ("collate", "LC_COLLATE"), |
| 35 | ("monetary", "LC_MONETARY"), |
| 36 | ("messages", "LC_MESSAGES"), |
| 37 | ("paper", "LC_PAPER"), |
| 38 | ("name", "LC_NAME"), |
| 39 | ("address", "LC_ADDRESS"), |
| 40 | ("telephone", "LC_TELEPHONE"), |
| 41 | ("measurement", "LC_MEASUREMENT"), |
| 42 | ].iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 43 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().(var) { |
| 44 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(tag) = tagrust-analyzer cargo locale_config 0.3.0 unix/tag().(val.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`AsRef<str>`]as_ref().()) |
| 45 | { |
| 46 | loc.add_categoryrust-analyzer cargo locale_config 0.3.0 impl#[Locale]add_category().(cat, &tag); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | // LANGUAGE defines fallbacks |
| 51 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(langs) = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("LANGUAGE") { |
| 52 | for i in langs.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(':') { |
| 53 | if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne()."" { |
| 54 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(tag) = tagrust-analyzer cargo locale_config 0.3.0 unix/tag().(i) { |
| 55 | loc.addrust-analyzer cargo locale_config 0.3.0 impl#[Locale]add().(&tag); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | if loc.as_refrust-analyzer cargo locale_config 0.3.0 impl#[Locale][`AsRef<str>`]as_ref().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]ne()."" { |
| 61 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(loc); |
| 62 | } else { |
| 63 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 64 | } |
| 65 | } |