weavetui_core 0.1.3

Core traits and utilities for weavetui TUI framework.
Documentation
//! This module defines the `Event` and `Action` enums, which are central to the application's event-driven architecture.
//! `Event` represents raw input from the terminal or internal application occurrences, while `Action` represents a processed command that can be dispatched and handled by components.

use {
    crossterm::event::{KeyEvent, MouseEvent},
    std::fmt::{Display, Formatter, Result},
    strum::EnumString,
};

/// Represents an action that can be triggered within the application.
///
/// Actions are used to communicate between components and the main application loop.
#[derive(Debug, PartialEq, Eq, Clone, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum Action {
    /// A tick event, used for periodic updates.
    Tick,
    /// A render event, used to trigger a redraw of the UI.
    Render,
    /// A resize event, indicating the terminal has been resized.
    Resize(u16, u16),
    /// A quit event, signaling the application to exit.
    Quit,
    /// A custom application-specific action with a string payload.
    AppAction(String),
    /// A key press event with a string payload.
    Key(String),
}

impl Display for Action {
    /// Formats the `Action` into a debug string representation.
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let enum_str = write!(f, "{:?}", self);
        enum_str
    }
}

/// Represents an event that can occur within the application.
///
/// Events are generated by the TUI backend and the application loop.
#[derive(Clone, Debug)]
pub enum Event {
    /// Initialization event.
    Init,
    /// Quit event.
    Quit,
    /// Error event.
    Error,
    /// Tick event.
    Tick,
    /// Render event.
    Render,
    /// Focus gained event.
    FocusGained,
    /// Focus lost event.
    FocusLost,
    /// Paste event with a string payload.
    Paste(String),
    /// Key press event.
    Key(KeyEvent),
    /// Mouse event.
    Mouse(MouseEvent),
    /// Resize event.
    Resize(u16, u16),
}

/// Represents the kind of an action, which can be either a string or a full `Action`.
///
/// This is used for flexibility in defining keybindings.
pub enum ActionKind {
    /// A string representation of an action.
    Stringified(String),
    /// A full `Action` enum variant.
    Full(Action),
}

impl From<&str> for ActionKind {
    /// Converts a string slice into an `ActionKind::Stringified` variant.
    fn from(s: &str) -> Self {
        ActionKind::Stringified(s.to_string())
    }
}

impl From<String> for ActionKind {
    /// Converts a `String` into an `ActionKind::Stringified` variant.
    fn from(s: String) -> Self {
        ActionKind::Stringified(s)
    }
}

impl From<Action> for ActionKind {
    /// Converts an `Action` into an `ActionKind::Full` variant.
    fn from(a: Action) -> Self {
        ActionKind::Full(a)
    }
}