slash-lib 0.1.0

Executor types and high-level API for the slash-command language
Documentation
use slash_lang::parser::ast::Arg;

use crate::executor::{CommandOutput, ExecutionError, PipeValue};

/// Describes a single builder-chain method that a command accepts.
///
/// Used by the registry to validate `.method()` calls before dispatch.
#[derive(Debug, Clone, PartialEq)]
pub struct MethodDef {
    /// The method name (e.g. `"text"`, `"level"`, `"append"`).
    pub name: &'static str,
    /// Whether this method requires a value: `.method(value)` vs `.flag`.
    pub takes_value: bool,
}

impl MethodDef {
    pub const fn flag(name: &'static str) -> Self {
        Self {
            name,
            takes_value: false,
        }
    }

    pub const fn with_value(name: &'static str) -> Self {
        Self {
            name,
            takes_value: true,
        }
    }
}

/// Port: a self-contained tool that knows its own methods.
///
/// Each implementor is a builtin command (e.g. `/read`, `/exec`, `/find`).
/// The registry holds these as `Box<dyn SlashCommand>` and dispatches by name.
pub trait SlashCommand: Send + Sync {
    /// The command's registered name (e.g. `"read"`, `"exec"`).
    fn name(&self) -> &str;

    /// The methods this command accepts.
    ///
    /// The registry validates all `.method()` args against this list before
    /// calling [`execute`](Self::execute). Unknown methods are rejected with
    /// a clear error message.
    fn methods(&self) -> &[MethodDef];

    /// Execute the command.
    ///
    /// - `primary` — the value from `/cmd(value)` syntax, if present.
    /// - `args` — builder-chain methods, already validated against [`methods`](Self::methods).
    /// - `input` — piped data from the previous command, if any.
    fn execute(
        &self,
        primary: Option<&str>,
        args: &[Arg],
        input: Option<&PipeValue>,
    ) -> Result<CommandOutput, ExecutionError>;
}