2019-05-10 18:59:12 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::Value;
|
2019-05-13 19:30:51 +02:00
|
|
|
use crate::prelude::*;
|
2019-05-11 10:08:21 +02:00
|
|
|
use std::path::PathBuf;
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
pub struct CommandArgs {
|
|
|
|
pub host: Arc<Mutex<dyn Host>>,
|
|
|
|
pub env: Arc<Mutex<Environment>>,
|
2019-05-16 02:21:46 +02:00
|
|
|
pub args: Vec<Value>,
|
2019-05-23 09:23:06 +02:00
|
|
|
pub input: InputStream,
|
2019-05-16 02:21:46 +02:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
impl CommandArgs {
|
2019-05-22 09:12:03 +02:00
|
|
|
crate fn from_context(
|
|
|
|
ctx: &'caller mut Context,
|
|
|
|
args: Vec<Value>,
|
2019-05-23 09:23:06 +02:00
|
|
|
input: InputStream,
|
|
|
|
) -> CommandArgs {
|
2019-05-22 09:12:03 +02:00
|
|
|
CommandArgs {
|
2019-05-23 09:23:06 +02:00
|
|
|
host: ctx.host.clone(),
|
|
|
|
env: ctx.env.clone(),
|
2019-05-22 09:12:03 +02:00
|
|
|
args,
|
|
|
|
input,
|
|
|
|
}
|
|
|
|
}
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 18:12:38 +02:00
|
|
|
#[derive(Debug)]
|
2019-05-13 19:30:51 +02:00
|
|
|
pub enum CommandAction {
|
2019-05-11 10:08:21 +02:00
|
|
|
ChangeCwd(PathBuf),
|
|
|
|
}
|
|
|
|
|
2019-05-15 18:12:38 +02:00
|
|
|
#[derive(Debug)]
|
2019-05-13 19:30:51 +02:00
|
|
|
pub enum ReturnValue {
|
|
|
|
Value(Value),
|
|
|
|
Action(CommandAction),
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:30:51 +02:00
|
|
|
impl ReturnValue {
|
|
|
|
crate fn single(value: Value) -> VecDeque<ReturnValue> {
|
|
|
|
let mut v = VecDeque::new();
|
|
|
|
v.push_back(ReturnValue::Value(value));
|
|
|
|
v
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
2019-05-13 19:30:51 +02:00
|
|
|
|
|
|
|
crate fn change_cwd(path: PathBuf) -> ReturnValue {
|
|
|
|
ReturnValue::Action(CommandAction::ChangeCwd(path))
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
2019-05-13 19:30:51 +02:00
|
|
|
|
|
|
|
pub trait Command {
|
2019-05-23 09:23:06 +02:00
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError>;
|
2019-05-13 19:30:51 +02:00
|
|
|
}
|
2019-05-22 09:12:03 +02:00
|
|
|
|
|
|
|
impl<F> Command for F
|
|
|
|
where
|
2019-05-23 09:23:06 +02:00
|
|
|
F: Fn(CommandArgs) -> Result<OutputStream, ShellError>,
|
2019-05-22 09:12:03 +02:00
|
|
|
{
|
2019-05-23 09:23:06 +02:00
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-05-22 09:12:03 +02:00
|
|
|
self(args)
|
|
|
|
}
|
|
|
|
}
|