keyboard-codes 0.3.0

Cross-platform keyboard key code mapping and conversion
Documentation
use keyboard_codes::{KeyParseError, Platform};

fn main() -> Result<(), KeyParseError> {
    // 从字符串解析平台类型
    let windows = "windows".parse::<Platform>()?;
    let linux = "linux".parse::<Platform>()?;
    let macos = "macos".parse::<Platform>()?;

    // 使用别名解析
    let _win = "win".parse::<Platform>()?; // Windows
    let _unix = "unix".parse::<Platform>()?; // Linux
    let _mac = "mac".parse::<Platform>()?; // MacOS

    // 获取平台字符串表示
    println!("Windows: {}", windows.as_str()); // 输出: Windows
    println!("Linux: {}", linux.as_str()); // 输出: Linux
    println!("MacOS: {}", macos.as_str()); // 输出: MacOS

    // 使用 Display trait
    println!("Platform: {}", windows); // 输出: Windows

    // 错误处理示例
    match "unknown".parse::<Platform>() {
        Ok(platform) => println!("Parsed platform: {}", platform),
        Err(e) => println!("Error: {}", e), // 输出: Invalid platform: unknown
    }

    // 平台比较
    assert_eq!(windows, Platform::Windows);
    assert_ne!(windows, linux);

    Ok(())
}