crisol-core 1.1.0

Crisol core: an egui-free data model, themes, metrics, localization, commands, state, and storage.
Documentation
//! Logical keys and modifiers that do not depend on egui.
//!
//! [`Key`] mirrors every variant of `egui::Key` 0.37 and [`Modifiers`] mirrors the
//! pressed set. The UI adapter is exhaustive: adding a key in egui without adding it
//! here fails to compile (CORE-KEY-01).
//!
//! ```
//! use crisol_core::commands::key;
//! ```

use std::fmt;
use std::str::FromStr;

mod modifiers;
mod names;

pub use modifiers::Modifiers;

/// A logical key, independent of the physical keyboard layout.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// # Example
///
/// ```
/// use crisol_core::commands::Key;
/// ```
pub enum Key {
    /// The `Down` key.
    ArrowDown,
    /// The `Left` key.
    ArrowLeft,
    /// The `Right` key.
    ArrowRight,
    /// The `Up` key.
    ArrowUp,
    /// The `Escape` key.
    Escape,
    /// The `Tab` key.
    Tab,
    /// The `Backspace` key.
    Backspace,
    /// The `Enter` key.
    Enter,
    /// The `Space` key.
    Space,
    /// The `Insert` key.
    Insert,
    /// The `Delete` key.
    Delete,
    /// The `Home` key.
    Home,
    /// The `End` key.
    End,
    /// The `PageUp` key.
    PageUp,
    /// The `PageDown` key.
    PageDown,
    /// The `Copy` key.
    Copy,
    /// The `Cut` key.
    Cut,
    /// The `Paste` key.
    Paste,
    /// The `Colon` key.
    Colon,
    /// The `Comma` key.
    Comma,
    /// The `Backslash` key.
    Backslash,
    /// The `Slash` key.
    Slash,
    /// The `Pipe` key.
    Pipe,
    /// The `Questionmark` key.
    Questionmark,
    /// The `Exclamationmark` key.
    Exclamationmark,
    /// The `OpenBracket` key.
    OpenBracket,
    /// The `CloseBracket` key.
    CloseBracket,
    /// The `OpenCurlyBracket` key.
    OpenCurlyBracket,
    /// The `CloseCurlyBracket` key.
    CloseCurlyBracket,
    /// The `Backtick` key.
    Backtick,
    /// The `Minus` key.
    Minus,
    /// The `Period` key.
    Period,
    /// The `Plus` key.
    Plus,
    /// The `Equals` key.
    Equals,
    /// The `Semicolon` key.
    Semicolon,
    /// The `Quote` key.
    Quote,
    /// The `0` key.
    Num0,
    /// The `1` key.
    Num1,
    /// The `2` key.
    Num2,
    /// The `3` key.
    Num3,
    /// The `4` key.
    Num4,
    /// The `5` key.
    Num5,
    /// The `6` key.
    Num6,
    /// The `7` key.
    Num7,
    /// The `8` key.
    Num8,
    /// The `9` key.
    Num9,
    /// The `A` key.
    A,
    /// The `B` key.
    B,
    /// The `C` key.
    C,
    /// The `D` key.
    D,
    /// The `E` key.
    E,
    /// The `F` key.
    F,
    /// The `G` key.
    G,
    /// The `H` key.
    H,
    /// The `I` key.
    I,
    /// The `J` key.
    J,
    /// The `K` key.
    K,
    /// The `L` key.
    L,
    /// The `M` key.
    M,
    /// The `N` key.
    N,
    /// The `O` key.
    O,
    /// The `P` key.
    P,
    /// The `Q` key.
    Q,
    /// The `R` key.
    R,
    /// The `S` key.
    S,
    /// The `T` key.
    T,
    /// The `U` key.
    U,
    /// The `V` key.
    V,
    /// The `W` key.
    W,
    /// The `X` key.
    X,
    /// The `Y` key.
    Y,
    /// The `Z` key.
    Z,
    /// The `F1` key.
    F1,
    /// The `F2` key.
    F2,
    /// The `F3` key.
    F3,
    /// The `F4` key.
    F4,
    /// The `F5` key.
    F5,
    /// The `F6` key.
    F6,
    /// The `F7` key.
    F7,
    /// The `F8` key.
    F8,
    /// The `F9` key.
    F9,
    /// The `F10` key.
    F10,
    /// The `F11` key.
    F11,
    /// The `F12` key.
    F12,
    /// The `F13` key.
    F13,
    /// The `F14` key.
    F14,
    /// The `F15` key.
    F15,
    /// The `F16` key.
    F16,
    /// The `F17` key.
    F17,
    /// The `F18` key.
    F18,
    /// The `F19` key.
    F19,
    /// The `F20` key.
    F20,
    /// The `F21` key.
    F21,
    /// The `F22` key.
    F22,
    /// The `F23` key.
    F23,
    /// The `F24` key.
    F24,
    /// The `F25` key.
    F25,
    /// The `F26` key.
    F26,
    /// The `F27` key.
    F27,
    /// The `F28` key.
    F28,
    /// The `F29` key.
    F29,
    /// The `F30` key.
    F30,
    /// The `F31` key.
    F31,
    /// The `F32` key.
    F32,
    /// The `F33` key.
    F33,
    /// The `F34` key.
    F34,
    /// The `F35` key.
    F35,
    /// The `BrowserBack` key.
    BrowserBack,
    /// The `BrowserForward` key.
    BrowserForward,
    /// The `BrowserRefresh` key.
    BrowserRefresh,
    /// The `BrowserSearch` key.
    BrowserSearch,
    /// The `BrowserHome` key.
    BrowserHome,
    /// The `BrowserFavorites` key.
    BrowserFavorites,
    /// The `BrowserStop` key.
    BrowserStop,
    /// The `MediaPlayPause` key.
    MediaPlayPause,
    /// The `MediaTrackNext` key.
    MediaTrackNext,
    /// The `MediaTrackPrevious` key.
    MediaTrackPrevious,
    /// The `MediaStop` key.
    MediaStop,
    /// The `AudioVolumeMute` key.
    AudioVolumeMute,
    /// The `AudioVolumeDown` key.
    AudioVolumeDown,
    /// The `AudioVolumeUp` key.
    AudioVolumeUp,
    /// The `LaunchMail` key.
    LaunchMail,
    /// The `LaunchApp1` key.
    LaunchApp1,
    /// The `LaunchApp2` key.
    LaunchApp2,
    /// The `ShiftLeft` key.
    ShiftLeft,
    /// The `ShiftRight` key.
    ShiftRight,
    /// The `ControlLeft` key.
    ControlLeft,
    /// The `ControlRight` key.
    ControlRight,
    /// The `AltLeft` key.
    AltLeft,
    /// The `AltRight` key.
    AltRight,
    /// The `SuperLeft` key.
    SuperLeft,
    /// The `SuperRight` key.
    SuperRight,
    /// The `IntlBackslash` key.
    IntlBackslash,
    /// The `CapsLock` key.
    CapsLock,
    /// The `NumLock` key.
    NumLock,
    /// The `ScrollLock` key.
    ScrollLock,
    /// The `PrintScreen` key.
    PrintScreen,
    /// The `Pause` key.
    Pause,
    /// The `Menu` key.
    Menu,
    /// The `Fn` key.
    Fn,
    /// The `Eject` key.
    Eject,
    /// The `Help` key.
    Help,
    /// The `Power` key.
    Power,
    /// The `Sleep` key.
    Sleep,
    /// The `Clear` key.
    Clear,
}

impl Key {
    /// Every variant, in declaration order.
    pub const ALL: [Self; 145] = names::ALL;

    /// The stable display name used by `Display` and the keybindings file.
    pub const fn name(self) -> &'static str {
        names::name(self)
    }

    /// Parse a key by its display name or its Rust variant name, case-insensitively.
    pub fn from_name(text: &str) -> Option<Self> {
        names::from_name(text)
    }
}

impl fmt::Display for Key {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.name())
    }
}

impl FromStr for Key {
    type Err = ParseKeyError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Self::from_name(text).ok_or_else(|| ParseKeyError(text.to_owned()))
    }
}

/// Why a key name could not be parsed.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("`{0}` is not a key name")]
#[non_exhaustive]
/// # Example
///
/// ```
/// use crisol_core::commands::ParseKeyError;
/// ```
pub struct ParseKeyError(pub String);