keyboard_codes/types/
platform.rs

1use crate::error::KeyParseError;
2
3/// Platform type enumeration
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Platform {
7    /// Windows platform
8    Windows,
9    /// Linux platform
10    Linux,
11    /// macOS platform
12    MacOS,
13}
14
15impl Platform {
16    /// Get the string representation of the platform
17    pub fn as_str(&self) -> &'static str {
18        match self {
19            Platform::Windows => "Windows",
20            Platform::Linux => "Linux",
21            Platform::MacOS => "MacOS",
22        }
23    }
24}
25
26impl std::fmt::Display for Platform {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{}", self.as_str())
29    }
30}
31
32impl std::str::FromStr for Platform {
33    type Err = KeyParseError;
34
35    fn from_str(s: &str) -> Result<Self, Self::Err> {
36        match s.to_lowercase().as_str() {
37            "windows" | "win" => Ok(Platform::Windows),
38            "linux" | "unix" => Ok(Platform::Linux),
39            "macos" | "mac" | "osx" => Ok(Platform::MacOS),
40            _ => Err(KeyParseError::InvalidPlatform(s.to_string())),
41        }
42    }
43}