nushell/src/commands/command.rs

63 lines
1.4 KiB
Rust
Raw Normal View History

2019-05-10 16:59:12 +00:00
use crate::errors::ShellError;
use crate::object::Value;
use crate::prelude::*;
2019-05-11 08:08:21 +00:00
use std::path::PathBuf;
2019-05-10 16:59:12 +00:00
2019-05-16 00:21:46 +00:00
pub struct CommandArgs<'caller> {
2019-05-22 07:12:03 +00:00
pub host: &'caller mut dyn Host,
2019-05-23 04:30:43 +00:00
pub env: &'caller Environment,
2019-05-16 00:21:46 +00:00
pub args: Vec<Value>,
pub input: VecDeque<Value>,
}
2019-05-22 07:12:03 +00:00
impl CommandArgs<'caller> {
crate fn from_context(
ctx: &'caller mut Context,
args: Vec<Value>,
input: VecDeque<Value>,
) -> CommandArgs<'caller> {
CommandArgs {
host: &mut ctx.host,
env: &ctx.env,
args,
input,
}
}
2019-05-11 08:08:21 +00:00
}
2019-05-15 16:12:38 +00:00
#[derive(Debug)]
pub enum CommandAction {
2019-05-11 08:08:21 +00:00
ChangeCwd(PathBuf),
}
2019-05-15 16:12:38 +00:00
#[derive(Debug)]
pub enum ReturnValue {
Value(Value),
Action(CommandAction),
2019-05-11 08:08:21 +00: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 08:08:21 +00:00
}
crate fn change_cwd(path: PathBuf) -> ReturnValue {
ReturnValue::Action(CommandAction::ChangeCwd(path))
2019-05-11 08:08:21 +00:00
}
2019-05-10 16:59:12 +00:00
}
pub trait Command {
2019-05-16 00:21:46 +00:00
fn run(&self, args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError>;
}
2019-05-22 07:12:03 +00:00
impl<F> Command for F
where
F: Fn(CommandArgs<'_>) -> Result<VecDeque<ReturnValue>, ShellError>,
{
fn run(&self, args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError> {
self(args)
}
}