use crate::types::Platform;
pub fn is_valid_key_code(code: usize, platform: Platform) -> bool {
match platform {
Platform::Windows => code <= 0xFF, Platform::Linux => code <= 0x2FF, Platform::MacOS => code <= 0x7F, }
}
pub fn normalize_key_code(code: usize, platform: Platform) -> usize {
match platform {
Platform::Windows => code & 0xFF, Platform::Linux => code & 0x3FF, Platform::MacOS => code & 0x7F, }
}
pub fn is_valid_key_name(name: &str) -> bool {
if name.is_empty() {
return false;
}
if !name.chars().next().unwrap().is_ascii_alphabetic() {
return false;
}
name.chars().all(|c| c.is_ascii_alphanumeric())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_valid_key_code() {
assert!(is_valid_key_code(0x41, Platform::Windows));
assert!(is_valid_key_code(0x100, Platform::Linux));
assert!(!is_valid_key_code(0x100, Platform::Windows));
}
#[test]
fn test_normalize_key_code() {
assert_eq!(normalize_key_code(0x141, Platform::Windows), 0x41);
assert_eq!(normalize_key_code(0x1FF, Platform::Windows), 0xFF);
assert_eq!(normalize_key_code(0x341, Platform::Linux), 0x341); assert_eq!(normalize_key_code(0x7FF, Platform::Linux), 0x3FF);
assert_eq!(normalize_key_code(0x81, Platform::MacOS), 0x01);
assert_eq!(normalize_key_code(0xFF, Platform::MacOS), 0x7F);
}
#[test]
fn test_is_valid_key_name() {
assert!(is_valid_key_name("A"));
assert!(is_valid_key_name("Enter"));
assert!(is_valid_key_name("F12"));
assert!(!is_valid_key_name(""));
assert!(!is_valid_key_name("123"));
assert!(!is_valid_key_name("Key-Name"));
}
}