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 {
Os::Aix => "aix",
Os::Amdhsa => "amdhsa",
Os::Android => "android",
Os::Cuda => "cuda",
Os::Cygwin => "cygwin",
Os::Dragonfly => "dragonfly",
Os::Emscripten => "emscripten",
Os::Espidf => "espidf",
Os::FreeBSD => "freebsd",
Os::Fuchsia => "fuchsia",
Os::Haiku => "haiku",
Os::HelenOS => "helenos",
Os::Hermit => "hermit",
Os::Horizon => "horizon",
Os::Hurd => "hurd",
Os::IllumOS => "illumos",
Os::iOS => "ios",
Os::L4re => "l4re",
Os::Linux => "linux",
Os::Lynxos178 => "lynxos178",
Os::MacOS => "macos",
Os::Managarm => "managarm",
Os::Motor => "motor",
Os::NetBSD => "netbsd",
Os::None => "none",
Os::Nto => "nto",
Os::Nuttx => "nuttx",
Os::OpenBSD => "openbsd",
Os::Psp => "psp",
Os::Psx => "psx",
Os::Qnx => "qnx",
Os::Qurt => "qurt",
Os::Redox => "redox",
Os::Rtems => "rtems",
Os::Solaris => "solaris",
Os::SolidAsp3 => "solid_asp3",
Os::TeeOS => "teeos",
Os::Trusty => "trusty",
Os::TvOS => "tvos",
Os::Uefi => "uefi",
Os::Unknown => "unknown",
Os::VexOS => "vexos",
Os::VisionOS => "visionos",
Os::Vita => "vita",
Os::VxWorks => "vxworks",
Os::Wasi => "wasi",
Os::WatchOS => "watchos",
Os::Windows => "windows",
Os::Xous => "xous",
Os::Zkvm => "zkvm",
}
}
}
impl FromStr for Os {
type Err = Error;
fn from_str(name: &str) -> Result<Self, Self::Err> {
let result = match name {
"aix" => Os::Aix,
"amdhsa" => Os::Amdhsa,
"android" => Os::Android,
"cuda" => Os::Cuda,
"cygwin" => Os::Cygwin,
"dragonfly" => Os::Dragonfly,
"emscripten" => Os::Emscripten,
"espidf" => Os::Espidf,
"freebsd" => Os::FreeBSD,
"fuchsia" => Os::Fuchsia,
"haiku" => Os::Haiku,
"helenos" => Os::HelenOS,
"hermit" => Os::Hermit,
"horizon" => Os::Horizon,
"hurd" => Os::Hurd,
"illumos" => Os::IllumOS,
"ios" => Os::iOS,
"l4re" => Os::L4re,
"linux" => Os::Linux,
"lynxos178" => Os::Lynxos178,
"macos" => Os::MacOS,
"managarm" => Os::Managarm,
"motor" => Os::Motor,
"netbsd" => Os::NetBSD,
"none" => Os::None,
"nto" => Os::Nto,
"nuttx" => Os::Nuttx,
"openbsd" => Os::OpenBSD,
"psp" => Os::Psp,
"psx" => Os::Psx,
"qnx" => Os::Qnx,
"qurt" => Os::Qurt,
"redox" => Os::Redox,
"rtems" => Os::Rtems,
"solaris" => Os::Solaris,
"solid_asp3" => Os::SolidAsp3,
"teeos" => Os::TeeOS,
"trusty" => Os::Trusty,
"tvos" => Os::TvOS,
"uefi" => Os::Uefi,
"unknown" => Os::Unknown,
"vexos" => Os::VexOS,
"visionos" => Os::VisionOS,
"vita" => Os::Vita,
"vxworks" => Os::VxWorks,
"wasi" => Os::Wasi,
"watchos" => Os::WatchOS,
"windows" => Os::Windows,
"xous" => Os::Xous,
"zkvm" => Os::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
))
})
}
}