1use crate::types::Platform;
4
5pub fn is_valid_key_code(code: usize, platform: Platform) -> bool {
10 match platform {
11 Platform::Windows => code <= 0xFF, Platform::Linux => code <= 0x2FF, Platform::MacOS => code <= 0x7F, }
15}
16
17pub fn normalize_key_code(code: usize, platform: Platform) -> usize {
22 match platform {
23 Platform::Windows => code & 0xFF, Platform::Linux => code & 0x3FF, Platform::MacOS => code & 0x7F, }
27}
28
29pub fn is_valid_key_name(name: &str) -> bool {
33 if name.is_empty() {
34 return false;
35 }
36
37 if !name.chars().next().unwrap().is_ascii_alphabetic() {
39 return false;
40 }
41
42 name.chars().all(|c| c.is_ascii_alphanumeric())
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_is_valid_key_code() {
52 assert!(is_valid_key_code(0x41, Platform::Windows));
53 assert!(is_valid_key_code(0x100, Platform::Linux));
54 assert!(!is_valid_key_code(0x100, Platform::Windows));
55 }
56
57 #[test]
58 fn test_normalize_key_code() {
59 assert_eq!(normalize_key_code(0x141, Platform::Windows), 0x41);
61 assert_eq!(normalize_key_code(0x1FF, Platform::Windows), 0xFF);
62
63 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);
69 assert_eq!(normalize_key_code(0xFF, Platform::MacOS), 0x7F);
70 }
71
72 #[test]
73 fn test_is_valid_key_name() {
74 assert!(is_valid_key_name("A"));
75 assert!(is_valid_key_name("Enter"));
76 assert!(is_valid_key_name("F12"));
77 assert!(!is_valid_key_name(""));
78 assert!(!is_valid_key_name("123"));
79 assert!(!is_valid_key_name("Key-Name"));
80 }
81}