#![deny(missing_docs)]
#![warn(clippy::all)]
pub mod error;
pub mod mapping;
pub mod types;
pub mod utils;
pub use error::KeyParseError;
pub use mapping::custom::{CustomKey, CustomKeyMap};
pub use types::{Key, KeyCodeMapper, Modifier, Platform};
use std::str::FromStr;
impl FromStr for Key {
type Err = KeyParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
mapping::standard::parse_key_from_str(s)
}
}
impl FromStr for Modifier {
type Err = KeyParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
mapping::standard::parse_modifier_from_str(s)
}
}
pub fn current_platform() -> Platform {
#[cfg(target_os = "windows")]
return Platform::Windows;
#[cfg(target_os = "linux")]
return Platform::Linux;
#[cfg(target_os = "macos")]
return Platform::MacOS;
#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
panic!("Unsupported platform");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_key_from_str() {
assert_eq!("Escape".parse::<Key>().unwrap(), Key::Escape);
assert_eq!("A".parse::<Key>().unwrap(), Key::A);
assert!("UnknownKey".parse::<Key>().is_err());
}
#[test]
fn test_modifier_from_str() {
assert_eq!("Shift".parse::<Modifier>().unwrap(), Modifier::Shift);
assert_eq!("Control".parse::<Modifier>().unwrap(), Modifier::Control);
assert!("UnknownModifier".parse::<Modifier>().is_err());
}
#[test]
fn test_current_platform() {
let platform = current_platform();
assert!(matches!(
platform,
Platform::Windows | Platform::Linux | Platform::MacOS
));
}
}