use crate::parser::Args; use crate::prelude::*; use indexmap::IndexMap; use std::error::Error; use std::sync::Arc; pub struct Context { commands: IndexMap>, crate host: Arc>, crate env: Arc>, } impl Context { crate fn basic() -> Result> { Ok(Context { commands: indexmap::IndexMap::new(), host: Arc::new(Mutex::new(crate::env::host::BasicHost)), env: Arc::new(Mutex::new(Environment::basic()?)), }) } pub fn add_commands(&mut self, commands: Vec>) { for command in commands { self.commands.insert(command.name().to_string(), command); } } pub fn clone_commands(&self) -> indexmap::IndexMap> { self.commands.clone() } crate fn has_command(&self, name: &str) -> bool { self.commands.contains_key(name) } crate fn get_command(&self, name: &str) -> Arc { self.commands.get(name).unwrap().clone() } crate fn run_command( &mut self, command: Arc, args: Args, input: InputStream, ) -> Result { let command_args = CommandArgs { host: self.host.clone(), env: self.env.clone(), positional: args.positional, named: args.named, input, }; command.run(command_args) } }