alma 0.1.0

A Bevy-native modal text editor with Vim-style navigation.
Documentation
//! Normalized Vim key tokens independent of Bevy input types.

use std::num::NonZeroUsize;

/// A normalized key token consumed by Vim grammar and keymaps.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KeyToken {
    /// A printable character after keyboard layout and modifiers are applied.
    Char(char),
    /// A control-modified character.
    Ctrl(char),
    /// Escape.
    Escape,
    /// Enter/Return.
    Enter,
    /// Backspace.
    Backspace,
}

impl KeyToken {
    /// Returns this token as a count digit when it can extend a Vim count.
    #[must_use]
    pub fn count_digit(self) -> Option<usize> {
        let Self::Char(character) = self else {
            return None;
        };

        character.to_digit(10).map(|digit| digit as usize)
    }

    /// Returns this token as a non-zero first count digit.
    #[must_use]
    pub fn non_zero_count_digit(self) -> Option<NonZeroUsize> {
        NonZeroUsize::new(self.count_digit()?)
    }
}