nushell/src/context.rs

40 lines
1.1 KiB
Rust
Raw Normal View History

2019-05-15 18:12:38 +02:00
use crate::prelude::*;
use std::error::Error;
pub struct Context {
2019-05-16 00:58:44 +02:00
commands: indexmap::IndexMap<String, Box<dyn crate::CommandBlueprint>>,
2019-05-15 18:12:38 +02:00
crate host: Box<dyn crate::Host>,
crate env: Environment,
}
impl Context {
crate fn basic() -> Result<Context, Box<Error>> {
Ok(Context {
2019-05-16 00:58:44 +02:00
commands: indexmap::IndexMap::new(),
2019-05-15 18:12:38 +02:00
host: Box::new(crate::env::host::BasicHost),
env: crate::Environment::basic()?,
})
}
pub fn add_commands(&mut self, commands: Vec<(&str, Box<dyn crate::CommandBlueprint>)>) {
for (name, command) in commands {
self.commands.insert(name.to_string(), command);
}
}
crate fn has_command(&mut self, name: &str) -> bool {
self.commands.contains_key(name)
}
crate fn create_command(
&mut self,
name: &str,
arg_list: Vec<Value>,
) -> Result<Box<dyn Command>, ShellError> {
match self.commands.get(name) {
Some(command) => Ok(command.create(arg_list, &self.host, &mut self.env)?),
None => Err(ShellError::string(format!("Missing command {}", name))),
}
}
}