Skip to main content

shannon_nu_cli/
mode_dispatcher.rs

1//! Mode dispatcher trait for external shell integration (e.g., Shannon).
2//!
3//! When a host binary provides a `ModeDispatcher`, the REPL checks
4//! `$env.SHANNON_MODE` each iteration. If the mode is not "nu", the
5//! dispatcher handles the command instead of nushell's parser/evaluator.
6
7use std::collections::HashMap;
8use std::path::PathBuf;
9
10/// Trait for dispatching commands to non-nushell modes (e.g., brush, AI).
11///
12/// The dispatcher receives string env vars (already converted from nushell's
13/// typed Values) and returns strings. The REPL handles conversion back to
14/// nushell Values.
15pub trait ModeDispatcher: Send {
16    /// Execute a command in the given mode.
17    ///
18    /// - `mode`: the active mode name (e.g., "brush", "ai")
19    /// - `command`: the raw command string from the user
20    /// - `env_vars`: all exported env vars as strings
21    /// - `cwd`: the current working directory
22    fn execute(
23        &mut self,
24        mode: &str,
25        command: &str,
26        env_vars: HashMap<String, String>,
27        cwd: PathBuf,
28    ) -> ModeResult;
29}
30
31/// Result of a mode dispatch execution.
32pub struct ModeResult {
33    /// Updated environment variables (as strings).
34    pub env: HashMap<String, String>,
35    /// Updated working directory.
36    pub cwd: PathBuf,
37    /// Exit code of the command.
38    pub exit_code: i32,
39}