keyboard_codes/
lib.rs

1//! keyboard-codes: Cross-platform keyboard key code mapping and conversion
2//!
3//! This crate provides comprehensive keyboard key definitions and cross-platform
4//! code mapping for Windows, Linux, and macOS. It supports standard keys,
5//! modifiers, custom key mapping, and bidirectional conversion between key names
6//! and platform-specific codes.
7//!
8//! # Examples
9//!
10//! ```rust
11//! use keyboard_codes::{Key, Modifier, Platform, KeyCodeMapper};
12//!
13//! // Parse key from string
14//! let key: Key = "Enter".parse().unwrap();
15//! assert_eq!(key, Key::Enter);
16//!
17//! // Convert key to platform-specific code
18//! let windows_code = key.to_code(Platform::Windows);
19//! let linux_code = key.to_code(Platform::Linux);
20//!
21//! // Parse key from code
22//! let key_from_code = Key::from_code(0x0D, Platform::Windows).unwrap();
23//! assert_eq!(key_from_code, Key::Enter);
24//! ```
25//!
26//! # Features
27//!
28//! - `serde`: Enables serialization/deserialization support
29//! - `phf`: Uses perfect hash functions for faster lookups
30
31#![deny(missing_docs)]
32#![warn(clippy::all)]
33
34/// Error types for key parsing and mapping operations
35pub mod error;
36
37/// Key code mapping implementations
38pub mod mapping;
39
40/// Core type definitions for keyboard keys and platforms
41pub mod types;
42
43/// Utility functions and helpers
44pub mod utils;
45
46// Re-export main types for convenient access
47pub use error::KeyParseError;
48pub use mapping::custom::{CustomKey, CustomKeyMap};
49pub use types::{Key, KeyCodeMapper, Modifier, Platform};
50
51use std::str::FromStr;
52
53// Implement FromStr for Key using the standard mappings
54impl 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
62// Implement FromStr for Modifier using the standard mappings
63impl 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
71/// Get the current platform based on compilation target
72pub 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}