#![warn(
anonymous_parameters,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
nonstandard_style,
rust_2018_idioms,
single_use_lifetimes,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unused_extern_crates,
unused_qualifications,
variant_size_differences,
unsafe_code
)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/ardaku/whoami/stable/res/icon.svg",
html_favicon_url = "https://raw.githubusercontent.com/ardaku/whoami/stable/res/icon.svg"
)]
#[allow(unsafe_code)]
#[cfg_attr(
not(any(target_os = "windows", target_arch = "wasm32")),
path = "unix.rs"
)]
#[cfg_attr(all(target_arch = "wasm32", target_os = "daku"), path = "fake.rs")]
#[cfg_attr(all(target_arch = "wasm32", target_os = "wasi"), path = "fake.rs")]
#[cfg_attr(
all(
target_arch = "wasm32",
not(target_os = "wasi"),
not(target_os = "daku"),
feature = "web",
),
path = "web.rs"
)]
#[cfg_attr(
all(
target_arch = "wasm32",
not(target_os = "wasi"),
not(target_os = "daku"),
not(feature = "web"),
),
path = "fake.rs"
)]
#[cfg_attr(
all(target_os = "windows", not(target_arch = "wasm32")),
path = "windows.rs"
)]
mod platform;
use std::{
ffi::OsString,
fmt::{self, Display, Formatter},
io::{Error, ErrorKind},
};
pub type Result<T = (), E = Error> = std::result::Result<T, E>;
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum DesktopEnv {
Gnome,
Windows,
Lxde,
Openbox,
Mate,
Xfce,
Kde,
Cinnamon,
I3,
Aqua,
Ios,
Android,
WebBrowser,
Console,
Ubuntu,
Ermine,
Orbital,
Unknown(String),
}
impl Display for DesktopEnv {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let desktop_env = match self {
DesktopEnv::Gnome => "Gnome",
DesktopEnv::Windows => "Windows",
DesktopEnv::Lxde => "LXDE",
DesktopEnv::Openbox => "Openbox",
DesktopEnv::Mate => "Mate",
DesktopEnv::Xfce => "XFCE",
DesktopEnv::Kde => "KDE",
DesktopEnv::Cinnamon => "Cinnamon",
DesktopEnv::I3 => "I3",
DesktopEnv::Aqua => "Aqua",
DesktopEnv::Ios => "IOS",
DesktopEnv::Android => "Android",
DesktopEnv::WebBrowser => "Web Browser",
DesktopEnv::Console => "Console",
DesktopEnv::Ubuntu => "Ubuntu",
DesktopEnv::Ermine => "Ermine",
DesktopEnv::Orbital => "Orbital",
DesktopEnv::Unknown(a) => {
f.write_str("Unknown: ")?;
a
}
};
f.write_str(desktop_env)
}
}
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum Platform {
Linux,
Bsd,
Windows,
MacOS,
Illumos,
Ios,
Android,
Nintendo,
Xbox,
PlayStation,
Fuchsia,
Redox,
Unknown(String),
}
impl Display for Platform {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let platform = match self {
Platform::Linux => "Linux",
Platform::Bsd => "BSD",
Platform::Windows => "Windows",
Platform::MacOS => "Mac OS",
Platform::Illumos => "Illumos",
Platform::Ios => "iOS",
Platform::Android => "Android",
Platform::Nintendo => "Nintendo",
Platform::Xbox => "XBox",
Platform::PlayStation => "PlayStation",
Platform::Fuchsia => "Fuchsia",
Platform::Redox => "Redox",
Platform::Unknown(a) => {
f.write_str("Unknown: ")?;
a
}
};
f.write_str(platform)
}
}
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Arch {
ArmV5,
ArmV6,
ArmV7,
Arm64,
I386,
I586,
I686,
X64,
Mips,
MipsEl,
Mips64,
Mips64El,
PowerPc,
PowerPc64,
PowerPc64Le,
Riscv32,
Riscv64,
S390x,
Sparc,
Sparc64,
Wasm32,
Wasm64,
Unknown(String),
}
impl Display for Arch {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let arch = match self {
Arch::ArmV5 => "armv5",
Arch::ArmV6 => "armv6",
Arch::ArmV7 => "armv7",
Arch::Arm64 => "arm64",
Arch::I386 => "i386",
Arch::I586 => "i586",
Arch::I686 => "i686",
Arch::Mips => "mips",
Arch::MipsEl => "mipsel",
Arch::Mips64 => "mips64",
Arch::Mips64El => "mips64el",
Arch::PowerPc => "powerpc",
Arch::PowerPc64 => "powerpc64",
Arch::PowerPc64Le => "powerpc64le",
Arch::Riscv32 => "riscv32",
Arch::Riscv64 => "riscv64",
Arch::S390x => "s390x",
Arch::Sparc => "sparc",
Arch::Sparc64 => "sparc64",
Arch::Wasm32 => "wasm32",
Arch::Wasm64 => "wasm64",
Arch::X64 => "x86_64",
Arch::Unknown(arch) => {
f.write_str("Unknown: ")?;
arch
}
};
f.write_str(arch)
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[non_exhaustive]
pub enum Width {
Bits32,
Bits64,
}
impl Display for Width {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let bits = match self {
Width::Bits32 => "32 bits",
Width::Bits64 => "64 bits",
};
f.write_str(bits)
}
}
impl Arch {
pub fn width(&self) -> Result<Width> {
match self {
Arch::ArmV5
| Arch::ArmV6
| Arch::ArmV7
| Arch::I386
| Arch::I586
| Arch::I686
| Arch::Mips
| Arch::MipsEl
| Arch::PowerPc
| Arch::Riscv32
| Arch::Sparc
| Arch::Wasm32 => Ok(Width::Bits32),
Arch::Arm64
| Arch::Mips64
| Arch::Mips64El
| Arch::PowerPc64
| Arch::PowerPc64Le
| Arch::Riscv64
| Arch::S390x
| Arch::Sparc64
| Arch::Wasm64
| Arch::X64 => Ok(Width::Bits64),
Arch::Unknown(unknown_arch) => Err(Error::new(
ErrorKind::InvalidData,
format!(
"Tried getting width of unknown arch ({})",
unknown_arch
),
)),
}
}
}
#[inline(always)]
pub fn arch() -> Arch {
platform::arch()
}
#[inline(always)]
pub fn username() -> String {
platform::username()
}
#[inline(always)]
pub fn username_os() -> OsString {
platform::username_os()
}
#[inline(always)]
pub fn realname() -> String {
platform::realname()
}
#[inline(always)]
pub fn realname_os() -> OsString {
platform::realname_os()
}
#[inline(always)]
pub fn devicename() -> String {
platform::devicename()
}
#[inline(always)]
pub fn devicename_os() -> OsString {
platform::devicename_os()
}
#[inline(always)]
pub fn hostname() -> String {
let mut hostname = platform::hostname();
hostname.make_ascii_lowercase();
hostname
}
#[inline(always)]
pub fn hostname_os() -> OsString {
hostname().into()
}
#[inline(always)]
pub fn distro() -> String {
platform::distro().unwrap_or_else(|| format!("Unknown {}", platform()))
}
#[inline(always)]
pub fn distro_os() -> OsString {
platform::distro_os()
.unwrap_or_else(|| format!("Unknown {}", platform()).into())
}
#[inline(always)]
pub fn desktop_env() -> DesktopEnv {
platform::desktop_env()
}
#[inline(always)]
pub fn platform() -> Platform {
platform::platform()
}
#[inline(always)]
pub fn lang() -> impl Iterator<Item = String> {
platform::lang()
}