1#![deny(missing_docs)]
32#![warn(clippy::all)]
33
34pub mod error;
36
37pub mod mapping;
39
40pub mod types;
42
43pub mod utils;
45
46pub use error::KeyParseError;
48pub use mapping::custom::{CustomKey, CustomKeyMap};
49pub use types::{Key, KeyCodeMapper, Modifier, Platform};
50
51use std::str::FromStr;
52
53impl FromStr for Key {
55 type Err = KeyParseError;
56
57 fn from_str(s: &str) -> Result<Self, Self::Err> {
58 mapping::standard::parse_key_from_str(s)
59 }
60}
61
62impl FromStr for Modifier {
64 type Err = KeyParseError;
65
66 fn from_str(s: &str) -> Result<Self, Self::Err> {
67 mapping::standard::parse_modifier_from_str(s)
68 }
69}
70
71pub fn current_platform() -> Platform {
73 #[cfg(target_os = "windows")]
74 return Platform::Windows;
75
76 #[cfg(target_os = "linux")]
77 return Platform::Linux;
78
79 #[cfg(target_os = "macos")]
80 return Platform::MacOS;
81
82 #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
83 panic!("Unsupported platform");
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_key_from_str() {
92 assert_eq!("Escape".parse::<Key>().unwrap(), Key::Escape);
93 assert_eq!("A".parse::<Key>().unwrap(), Key::A);
94 assert!("UnknownKey".parse::<Key>().is_err());
95 }
96
97 #[test]
98 fn test_modifier_from_str() {
99 assert_eq!("Shift".parse::<Modifier>().unwrap(), Modifier::Shift);
100 assert_eq!("Control".parse::<Modifier>().unwrap(), Modifier::Control);
101 assert!("UnknownModifier".parse::<Modifier>().is_err());
102 }
103
104 #[test]
105 fn test_current_platform() {
106 let platform = current_platform();
107 assert!(matches!(
108 platform,
109 Platform::Windows | Platform::Linux | Platform::MacOS
110 ));
111 }
112}