mouse_codes/types/
platform.rs1use std::fmt;
4
5use crate::types::Button;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub enum Platform {
11 Windows,
13 Linux,
15 MacOS,
17}
18
19impl Platform {
20 pub fn current() -> Self {
22 #[cfg(target_os = "windows")]
23 return Platform::Windows;
24
25 #[cfg(target_os = "linux")]
26 return Platform::Linux;
27
28 #[cfg(target_os = "macos")]
29 return Platform::MacOS;
30
31 #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
32 panic!("Unsupported platform");
33 }
34
35 pub fn button_code(&self, button: Button) -> usize {
37 button.to_code(*self)
38 }
39
40 pub fn button_from_code(&self, code: usize) -> Option<Button> {
42 Button::from_code(code, *self)
43 }
44
45 pub fn code_matches_button(&self, code: usize, button: Button) -> bool {
47 button.matches_code(code, *self)
48 }
49}
50
51impl fmt::Display for Platform {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {
54 Platform::Windows => write!(f, "Windows"),
55 Platform::Linux => write!(f, "Linux"),
56 Platform::MacOS => write!(f, "macOS"),
57 }
58 }
59}
60
61impl std::str::FromStr for Platform {
62 type Err = crate::error::MouseParseError;
63
64 fn from_str(s: &str) -> Result<Self, Self::Err> {
65 match s.to_lowercase().as_str() {
66 "windows" => Ok(Platform::Windows),
67 "linux" => Ok(Platform::Linux),
68 "macos" | "osx" => Ok(Platform::MacOS),
69 _ => Err(crate::error::MouseParseError::UnknownPlatform),
70 }
71 }
72}