keyboard_codes/types/
platform.rs1use crate::error::KeyParseError;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum Platform {
7 Windows,
9 Linux,
11 MacOS,
13}
14
15impl Platform {
16 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}