use crate::error::KeyParseError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Platform {
Windows,
Linux,
MacOS,
}
impl Platform {
pub fn as_str(&self) -> &'static str {
match self {
Platform::Windows => "Windows",
Platform::Linux => "Linux",
Platform::MacOS => "MacOS",
}
}
pub fn current() -> Platform {
#[cfg(target_os = "windows")]
return Platform::Windows;
#[cfg(target_os = "linux")]
return Platform::Linux;
#[cfg(target_os = "macos")]
return Platform::MacOS;
#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
panic!("Unsupported platform");
}
}
impl std::fmt::Display for Platform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl std::str::FromStr for Platform {
type Err = KeyParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"windows" | "win" => Ok(Platform::Windows),
"linux" | "unix" => Ok(Platform::Linux),
"macos" | "mac" | "osx" => Ok(Platform::MacOS),
_ => Err(KeyParseError::InvalidPlatform(s.to_string())),
}
}
}