use crate::errors::ShellError; use crate::object::Value; use crate::prelude::*; use std::path::PathBuf; pub struct CommandArgs<'caller> { pub host: &'caller mut dyn Host, pub env: &'caller Environment, pub args: Vec, pub input: VecDeque, } impl CommandArgs<'caller> { crate fn from_context( ctx: &'caller mut Context, args: Vec, input: VecDeque, ) -> CommandArgs<'caller> { CommandArgs { host: &mut ctx.host, env: &ctx.env, args, input, } } } #[derive(Debug)] pub enum CommandAction { ChangeCwd(PathBuf), } #[derive(Debug)] pub enum ReturnValue { Value(Value), Action(CommandAction), } impl ReturnValue { crate fn single(value: Value) -> VecDeque { let mut v = VecDeque::new(); v.push_back(ReturnValue::Value(value)); v } crate fn change_cwd(path: PathBuf) -> ReturnValue { ReturnValue::Action(CommandAction::ChangeCwd(path)) } } pub trait Command { fn run(&self, args: CommandArgs<'caller>) -> Result, ShellError>; } impl Command for F where F: Fn(CommandArgs<'_>) -> Result, ShellError>, { fn run(&self, args: CommandArgs<'caller>) -> Result, ShellError> { self(args) } }