keyboard-codes 0.2.0

Cross-platform keyboard key code mapping and conversion
Documentation
use crate::error::KeyParseError;

/// Platform type enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Platform {
    /// Windows platform
    Windows,
    /// Linux platform
    Linux,
    /// macOS platform
    MacOS,
}

impl Platform {
    /// Get the string representation of the platform
    pub fn as_str(&self) -> &'static str {
        match self {
            Platform::Windows => "Windows",
            Platform::Linux => "Linux",
            Platform::MacOS => "MacOS",
        }
    }
}

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())),
        }
    }
}