use core::{fmt, str::FromStr};
#[cfg(feature = "serde")]
use serde::{de, de::Error as DeError, ser, Deserialize, Serialize};
use crate::error::Error;
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Os {
Aix,
Amdhsa,
Android,
Cuda,
Cygwin,
Dragonfly,
Emscripten,
Espidf,
FreeBSD,
Fuchsia,
Haiku,
HelenOS,
Hermit,
Horizon,
Hurd,
IllumOS,
#[allow(non_camel_case_types)]
iOS,
L4re,
Linux,
Lynxos178,
MacOS,
Managarm,
Motor,
NetBSD,
None,
Nto,
Nuttx,
OpenBSD,
Psp,
Psx,
Qnx,
Qurt,
Redox,
Rtems,
Solaris,
SolidAsp3,
TeeOS,
Trusty,
TvOS,
Uefi,
Unknown,
VexOS,
VisionOS,
Vita,
VxWorks,
Wasi,
WatchOS,
Windows,
Xous,
Zkvm,
}
impl Os {
pub fn as_str(self) -> &'static str {
match self {
Self::Aix => "aix",
Self::Amdhsa => "amdhsa",
Self::Android => "android",
Self::Cuda => "cuda",
Self::Cygwin => "cygwin",
Self::Dragonfly => "dragonfly",
Self::Emscripten => "emscripten",
Self::Espidf => "espidf",
Self::FreeBSD => "freebsd",
Self::Fuchsia => "fuchsia",
Self::Haiku => "haiku",
Self::HelenOS => "helenos",
Self::Hermit => "hermit",
Self::Horizon => "horizon",
Self::Hurd => "hurd",
Self::IllumOS => "illumos",
Self::iOS => "ios",
Self::L4re => "l4re",
Self::Linux => "linux",
Self::Lynxos178 => "lynxos178",
Self::MacOS => "macos",
Self::Managarm => "managarm",
Self::Motor => "motor",
Self::NetBSD => "netbsd",
Self::None => "none",
Self::Nto => "nto",
Self::Nuttx => "nuttx",
Self::OpenBSD => "openbsd",
Self::Psp => "psp",
Self::Psx => "psx",
Self::Qnx => "qnx",
Self::Qurt => "qurt",
Self::Redox => "redox",
Self::Rtems => "rtems",
Self::Solaris => "solaris",
Self::SolidAsp3 => "solid_asp3",
Self::TeeOS => "teeos",
Self::Trusty => "trusty",
Self::TvOS => "tvos",
Self::Uefi => "uefi",
Self::Unknown => "unknown",
Self::VexOS => "vexos",
Self::VisionOS => "visionos",
Self::Vita => "vita",
Self::VxWorks => "vxworks",
Self::Wasi => "wasi",
Self::WatchOS => "watchos",
Self::Windows => "windows",
Self::Xous => "xous",
Self::Zkvm => "zkvm",
}
}
}
impl FromStr for Os {
type Err = Error;
fn from_str(name: &str) -> Result<Self, Self::Err> {
let result = match name {
"aix" => Self::Aix,
"amdhsa" => Self::Amdhsa,
"android" => Self::Android,
"cuda" => Self::Cuda,
"cygwin" => Self::Cygwin,
"dragonfly" => Self::Dragonfly,
"emscripten" => Self::Emscripten,
"espidf" => Self::Espidf,
"freebsd" => Self::FreeBSD,
"fuchsia" => Self::Fuchsia,
"haiku" => Self::Haiku,
"helenos" => Self::HelenOS,
"hermit" => Self::Hermit,
"horizon" => Self::Horizon,
"hurd" => Self::Hurd,
"illumos" => Self::IllumOS,
"ios" => Self::iOS,
"l4re" => Self::L4re,
"linux" => Self::Linux,
"lynxos178" => Self::Lynxos178,
"macos" => Self::MacOS,
"managarm" => Self::Managarm,
"motor" => Self::Motor,
"netbsd" => Self::NetBSD,
"none" => Self::None,
"nto" => Self::Nto,
"nuttx" => Self::Nuttx,
"openbsd" => Self::OpenBSD,
"psp" => Self::Psp,
"psx" => Self::Psx,
"qnx" => Self::Qnx,
"qurt" => Self::Qurt,
"redox" => Self::Redox,
"rtems" => Self::Rtems,
"solaris" => Self::Solaris,
"solid_asp3" => Self::SolidAsp3,
"teeos" => Self::TeeOS,
"trusty" => Self::Trusty,
"tvos" => Self::TvOS,
"uefi" => Self::Uefi,
"unknown" => Self::Unknown,
"vexos" => Self::VexOS,
"visionos" => Self::VisionOS,
"vita" => Self::Vita,
"vxworks" => Self::VxWorks,
"wasi" => Self::Wasi,
"watchos" => Self::WatchOS,
"windows" => Self::Windows,
"xous" => Self::Xous,
"zkvm" => Self::Zkvm,
_ => return Err(Error),
};
Ok(result)
}
}
impl fmt::Display for Os {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(feature = "serde")]
impl Serialize for Os {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
#[cfg(all(feature = "serde", feature = "std"))]
impl<'de> Deserialize<'de> for Os {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let string = std::string::String::deserialize(deserializer)?;
string.parse().map_err(|_| {
D::Error::custom(std::format!(
"Unrecognized value '{}' for target_os",
string
))
})
}
}