#![allow(unsafe_code)]
#[cfg_attr(all(target_arch = "wasm32", daku), path = "os/daku.rs")]
#[cfg_attr(
all(target_os = "redox", not(target_arch = "wasm32")),
path = "os/redox.rs"
)]
#[cfg_attr(
all(
any(
target_os = "linux",
target_os = "macos",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
),
not(target_arch = "wasm32")
),
path = "os/unix.rs"
)]
#[cfg_attr(
all(target_arch = "wasm32", target_os = "wasi"),
path = "os/wasi.rs"
)]
#[cfg_attr(
all(
target_arch = "wasm32",
not(target_os = "wasi"),
not(daku),
feature = "web",
),
path = "os/web.rs"
)]
#[cfg_attr(
all(target_os = "windows", not(target_arch = "wasm32")),
path = "os/windows.rs"
)]
mod target;
use std::{
env::{self, VarError},
ffi::OsString,
io::{Error, ErrorKind},
};
use crate::{Arch, DesktopEnv, Platform, Result};
pub(crate) struct Os;
pub(crate) trait Target: Sized {
fn langs(self) -> Result<String>;
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) -> DesktopEnv;
fn platform(self) -> Platform;
fn arch(self) -> Result<Arch>;
fn account(self) -> Result<OsString> {
self.username()
}
}
#[allow(dead_code)]
fn err_missing_record() -> Error {
Error::new(ErrorKind::NotFound, "Missing record")
}
#[allow(dead_code)]
fn err_null_record() -> Error {
Error::new(ErrorKind::NotFound, "Null record")
}
#[allow(dead_code)]
fn err_empty_record() -> Error {
Error::new(ErrorKind::NotFound, "Empty record")
}
#[allow(dead_code)]
fn unix_lang() -> Result<String> {
let check_var = |var| {
env::var(var).map_err(|e| {
let kind = match e {
VarError::NotPresent => ErrorKind::NotFound,
VarError::NotUnicode(_) => ErrorKind::InvalidData,
};
Error::new(kind, e)
})
};
let langs = check_var("LANGS").or_else(|_| check_var("LANG"))?;
if langs.is_empty() {
return Err(err_empty_record());
}
Ok(langs)
}