#![allow(unsafe_code)]
#[cfg_attr(
all(
not(any(
feature = "force-stub",
all(target_os = "wasi", feature = "wasi-wasite")
)),
target_arch = "wasm32",
daku,
),
path = "os/daku.rs"
)]
#[cfg_attr(
all(
not(any(feature = "force-stub", target_arch = "wasm32")),
feature = "std",
target_os = "redox",
),
path = "os/redox.rs"
)]
#[cfg_attr(
all(
not(any(feature = "force-stub", target_arch = "wasm32")),
feature = "std",
any(
target_vendor = "apple",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
target_os = "hurd",
),
),
path = "os/unix.rs"
)]
#[cfg_attr(
all(
not(feature = "force-stub"),
target_arch = "wasm32",
target_os = "wasi",
feature = "wasi-wasite",
),
path = "os/wasite.rs"
)]
#[cfg_attr(
all(
not(any(
feature = "force-stub",
daku,
all(target_os = "wasi", feature = "wasi-wasite")
)),
target_arch = "wasm32",
feature = "wasm-web",
),
path = "os/web.rs"
)]
#[cfg_attr(
all(
not(any(feature = "force-stub", target_arch = "wasm32")),
feature = "std",
target_os = "windows",
),
path = "os/windows.rs"
)]
mod stub;
use alloc::string::String;
use crate::{
CpuArchitecture, DesktopEnvironment, LanguagePreferences, OsString,
Platform, Result,
};
pub(crate) struct Os;
pub(crate) trait Target: Sized {
fn lang_prefs(self) -> Result<LanguagePreferences>;
fn realname(self) -> Result<OsString>;
fn username(self) -> Result<OsString>;
fn devicename(self) -> Result<OsString>;
fn hostname(self) -> Result<String>;
fn distro(self) -> Result<String>;
fn desktop_env(self) -> Option<DesktopEnvironment>;
fn platform(self) -> Platform;
fn arch(self) -> Result<CpuArchitecture>;
fn account(self) -> Result<OsString> {
self.username()
}
}
#[cfg(feature = "std")]
#[allow(dead_code)]
fn unix_lang() -> Result<LanguagePreferences> {
use std::{
env::{self, VarError},
str::FromStr,
vec::Vec,
};
use crate::{Error, Language};
let env_var = |var: &str| match env::var(var) {
Ok(value) => Ok(if value.is_empty() { None } else { Some(value) }),
Err(VarError::NotPresent) => Ok(None),
Err(VarError::NotUnicode(_)) => {
Err(Error::with_invalid_data("not unicode"))
}
};
let lc_all = env_var("LC_ALL")?;
let lang = env_var("LANG")?;
if lang.is_none() && lc_all.is_none() {
return Err(Error::empty_record());
}
if let Some(l) = &lang {
if l == "C" || l == "POSIX" {
return Ok(LanguagePreferences {
fallbacks: Vec::new(),
..Default::default()
});
}
}
if let Some(language) = env_var("LANGUAGE")? {
return Ok(LanguagePreferences {
fallbacks: language
.split(":")
.map(Language::from_str)
.collect::<Result<_>>()?,
..Default::default()
});
}
let lang_from_var = |var| -> Result<Option<Language>> {
env_var(var)?.as_deref().map(Language::from_str).transpose()
};
Ok(LanguagePreferences {
fallbacks: lang
.as_ref()
.map(|l| -> Result<_> { Ok([Language::from_str(l)?].to_vec()) })
.transpose()?
.unwrap_or(Vec::new()),
collation: lang_from_var("LC_COLLATE")?,
char_classes: lang_from_var("LC_CTYPE")?,
monetary: lang_from_var("LC_MONETARY")?,
messages: lang_from_var("LC_MESSAGES")?,
numeric: lang_from_var("LC_NUMERIC")?,
time: lang_from_var("LC_TIME")?,
})
}