rusty_repl 0.3.0

REPL library with customisable prompts and clean terminal management.
Documentation
//! Defines styling metadata for keyword-based syntax highlighting in the REPL.
//!
//! The [`KeywordStyle`] struct provides a lightweight way to configure which
//! keywords should be visually emphasised and what foreground color they
//! should use when rendered in the terminal. It acts as the configuration
//! layer for components such as [`KeywordHighlighter`](crate::repl::highlighter::KeywordHighlighter).
//!
//! # Overview
//! A `KeywordStyle` contains:
//! - A list of `keywords` that will be highlighted when matched.
//! - A `foreground` color defining the ANSI terminal color used for those keywords.
//!
//! This type is designed to be cloned and reused across multiple REPL sessions
//! or components that require consistent visual behavior.
//!
//! # Example
//! ```no_run
//! use nu_ansi_term::Color;
//! use rusty_repl::KeywordStyle;
//!
//! // Define a keyword style highlighting "help" and "exit" in cyan
//! let style = KeywordStyle::new(vec!["help", "exit"], Color::Cyan);
//! ```
//!
//! This instance can then be passed into a [`KeywordHighlighter`](crate::repl::highlighter::KeywordHighlighter) or directly
//! into a REPL constructor that supports keyword highlighting.
//!
//! # Notes
//! - Keywords are matched exactly (case-sensitive).
//! - The `foreground` color uses `nu_ansi_term`’s color API for portability
//!   across ANSI-compatible terminals.

use nu_ansi_term::Color;
use std::borrow::Cow;

/// Represents a set of keywords and the style used to highlight them.
///
/// A [`KeywordStyle`] is typically used to configure components that perform
/// syntax highlighting or semantic colorisation within a REPL or command-line
/// environment.
///
/// # Fields
/// - `keywords`: A list of keywords to be highlighted.
/// - `foreground`: The foreground color used for those keywords.
///
/// # Cloning
/// `KeywordStyle` implements [`Clone`], allowing it to be easily shared
/// or reused across multiple REPL instances or components.
#[derive(Clone)]
pub struct KeywordStyle {
    /// The list of keywords that should be highlighted.
    pub keywords: Vec<String>,

    /// The ANSI foreground color used to render the keywords.
    pub foreground: Color,
}

impl KeywordStyle {
    /// Creates a new [`KeywordStyle`] from a list of keywords and a color.
    ///
    /// # Arguments
    /// * `keywords` - Any iterator of items convertible into `Cow<'static, str>`.
    /// * `foreground` - The [`Color`] to use for highlighting.
    ///
    /// # Example
    /// ```no_run
    /// use nu_ansi_term::Color;
    /// use rusty_repl::KeywordStyle;
    ///
    /// let kw_style = KeywordStyle::new(vec!["list", "quit"], Color::Yellow);
    /// ```
    ///
    /// # Notes
    /// Each keyword is converted into an owned `String` internally for lifetime safety.
    pub fn new<I>(keywords: Vec<I>, foreground: Color) -> Self
    where
        I: Into<Cow<'static, str>>,
    {
        Self {
            keywords: keywords
                .into_iter()
                .map(|kw| kw.into().into_owned())
                .collect(),
            foreground,
        }
    }
}