use core::str::FromStr;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use error::Error;
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum OS {
Android,
Bitrig,
CloudABI,
Dragonfly,
Emscripten,
FreeBSD,
Fuchsia,
Haiku,
#[allow(non_camel_case_types)]
iOS,
Linux,
MacOS,
NetBSD,
OpenBSD,
Redox,
Solaris,
Windows,
Unknown,
}
impl OS {
pub fn as_str(self) -> &'static str {
match self {
OS::Android => "android",
OS::Bitrig => "bitrig",
OS::CloudABI => "cloudabi",
OS::Dragonfly => "dragonfly",
OS::Emscripten => "emscripten",
OS::FreeBSD => "freebsd",
OS::Fuchsia => "fuchsia",
OS::Haiku => "haiku",
OS::iOS => "ios",
OS::Linux => "linux",
OS::MacOS => "macos",
OS::NetBSD => "netbsd",
OS::OpenBSD => "openbsd",
OS::Redox => "redox",
OS::Solaris => "solaris",
OS::Windows => "windows",
OS::Unknown => "unknown",
}
}
}
impl FromStr for OS {
type Err = Error;
fn from_str(os_name: &str) -> Result<Self, Self::Err> {
let os = match os_name {
"android" => OS::Android,
"bitrig" => OS::Bitrig,
"cloudabi" => OS::CloudABI,
"dragonfly" => OS::Dragonfly,
"emscripten" => OS::Emscripten,
"freebsd" => OS::FreeBSD,
"fuchsia" => OS::Fuchsia,
"haiku" => OS::Haiku,
"ios" => OS::iOS,
"linux" => OS::Linux,
"macos" => OS::MacOS,
"netbsd" => OS::NetBSD,
"openbsd" => OS::OpenBSD,
"redox" => OS::Redox,
"solaris" => OS::Solaris,
"windows" => OS::Windows,
_ => return Err(Error),
};
Ok(os)
}
}
#[cfg(feature = "serde")]
impl Serialize for OS {
fn serialize<S: 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: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use std::string::String;
Ok(Self::from_str(&String::deserialize(deserializer)?).unwrap_or(OS::Unknown))
}
}
#[cfg(target_os = "android")]
pub const TARGET_OS: OS = OS::Android;
#[cfg(target_os = "bitrig")]
pub const TARGET_OS: OS = OS::Bitrig;
#[cfg(target_os = "cloudabi")]
pub const TARGET_OS: OS = OS::CloudABI;
#[cfg(target_os = "dragonfly")]
pub const TARGET_OS: OS = OS::Dragonfly;
#[cfg(target_os = "emscripten")]
pub const TARGET_OS: OS = OS::Emscripten;
#[cfg(target_os = "freebsd")]
pub const TARGET_OS: OS = OS::FreeBSD;
#[cfg(target_os = "fuchsia")]
pub const TARGET_OS: OS = OS::Fuchsia;
#[cfg(target_os = "haiku")]
pub const TARGET_OS: OS = OS::Haiku;
#[cfg(target_os = "ios")]
pub const TARGET_OS: OS = OS::iOS;
#[cfg(target_os = "linux")]
pub const TARGET_OS: OS = OS::Linux;
#[cfg(target_os = "macos")]
pub const TARGET_OS: OS = OS::MacOS;
#[cfg(target_os = "netbsd")]
pub const TARGET_OS: OS = OS::NetBSD;
#[cfg(target_os = "openbsd")]
pub const TARGET_OS: OS = OS::OpenBSD;
#[cfg(target_os = "redox")]
pub const TARGET_OS: OS = OS::Redox;
#[cfg(target_os = "solaris")]
pub const TARGET_OS: OS = OS::Solaris;
#[cfg(target_os = "windows")]
pub const TARGET_OS: OS = OS::Windows;
#[cfg(
not(
any(
target_os = "android",
target_os = "bitrig",
target_os = "cloudabi",
target_os = "dragonfly",
target_os = "emscripten",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "windows",
)
)
)]
pub const TARGET_ENV: OS = OS::Unknown;