2019-07-24 00:22:11 +02:00
|
|
|
use crate::context::{SourceMap, SpanSource};
|
2019-05-10 18:59:12 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-07-24 00:22:11 +02:00
|
|
|
use crate::evaluate::Scope;
|
2019-05-10 18:59:12 +02:00
|
|
|
use crate::object::Value;
|
2019-07-24 00:22:11 +02:00
|
|
|
use crate::parser::hir;
|
2019-08-09 06:51:21 +02:00
|
|
|
use crate::parser::{registry, ConfigDeserializer};
|
2019-05-13 19:30:51 +02:00
|
|
|
use crate::prelude::*;
|
2019-08-02 21:15:07 +02:00
|
|
|
use derive_new::new;
|
2019-06-22 05:43:37 +02:00
|
|
|
use getset::Getters;
|
2019-06-27 06:56:48 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-07-24 00:22:11 +02:00
|
|
|
use std::fmt;
|
|
|
|
use std::ops::Deref;
|
2019-07-08 18:44:53 +02:00
|
|
|
use std::path::PathBuf;
|
2019-07-19 21:48:14 +02:00
|
|
|
use uuid::Uuid;
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
|
|
pub struct UnevaluatedCallInfo {
|
|
|
|
pub args: hir::Call,
|
|
|
|
pub source: Text,
|
|
|
|
pub source_map: SourceMap,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub name_span: Span,
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToDebug for UnevaluatedCallInfo {
|
|
|
|
fn fmt_debug(&self, f: &mut fmt::Formatter, source: &str) -> fmt::Result {
|
|
|
|
self.args.fmt_debug(f, source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnevaluatedCallInfo {
|
|
|
|
fn evaluate(
|
|
|
|
self,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
scope: &Scope,
|
|
|
|
) -> Result<CallInfo, ShellError> {
|
|
|
|
let args = self.args.evaluate(registry, scope, &self.source)?;
|
|
|
|
|
|
|
|
Ok(CallInfo {
|
|
|
|
args,
|
|
|
|
source_map: self.source_map,
|
|
|
|
name_span: self.name_span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 09:45:00 +02:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
2019-07-20 04:27:10 +02:00
|
|
|
pub struct CallInfo {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub args: registry::EvaluatedArgs,
|
2019-07-20 04:27:10 +02:00
|
|
|
pub source_map: SourceMap,
|
2019-08-05 10:54:29 +02:00
|
|
|
pub name_span: Span,
|
2019-07-20 04:27:10 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
#[derive(Getters)]
|
|
|
|
#[get = "crate"]
|
2019-05-23 09:23:06 +02:00
|
|
|
pub struct CommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub host: Arc<Mutex<dyn Host>>,
|
2019-08-07 19:49:11 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
pub call_info: UnevaluatedCallInfo,
|
2019-08-09 06:51:21 +02:00
|
|
|
// pub host: Arc<Mutex<dyn Host + Send>>,
|
|
|
|
// pub shell_manager: ShellManager,
|
|
|
|
// pub call_info: CallInfo,
|
2019-05-23 09:23:06 +02:00
|
|
|
pub input: InputStream,
|
2019-05-16 02:21:46 +02:00
|
|
|
}
|
|
|
|
|
2019-08-03 04:17:28 +02:00
|
|
|
#[derive(Getters)]
|
|
|
|
#[get = "crate"]
|
|
|
|
pub struct RawCommandArgs {
|
|
|
|
pub host: Arc<Mutex<dyn Host>>,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-08-03 04:17:28 +02:00
|
|
|
pub call_info: UnevaluatedCallInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawCommandArgs {
|
2019-08-09 06:51:21 +02:00
|
|
|
pub fn with_input(self, input: Vec<Tagged<Value>>) -> CommandArgs {
|
2019-08-03 04:17:28 +02:00
|
|
|
CommandArgs {
|
|
|
|
host: self.host,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: self.shell_manager,
|
2019-08-03 04:17:28 +02:00
|
|
|
call_info: self.call_info,
|
|
|
|
input: input.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
impl ToDebug for CommandArgs {
|
|
|
|
fn fmt_debug(&self, f: &mut fmt::Formatter, source: &str) -> fmt::Result {
|
|
|
|
self.call_info.fmt_debug(f, source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
impl CommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub fn evaluate_once(
|
|
|
|
self,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<EvaluatedStaticCommandArgs, ShellError> {
|
|
|
|
let host = self.host.clone();
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = self.shell_manager.clone();
|
2019-07-24 00:22:11 +02:00
|
|
|
let input = self.input;
|
|
|
|
let call_info = self.call_info.evaluate(registry, &Scope::empty())?;
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
Ok(EvaluatedStaticCommandArgs::new(
|
|
|
|
host,
|
|
|
|
shell_manager,
|
|
|
|
call_info,
|
|
|
|
input,
|
|
|
|
))
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
pub fn name_span(&self) -> Span {
|
2019-07-24 00:22:11 +02:00
|
|
|
self.call_info.name_span
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
pub fn process<'de, T: Deserialize<'de>>(
|
|
|
|
self,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
callback: fn(T, RunnableContext) -> Result<OutputStream, ShellError>,
|
|
|
|
) -> Result<RunnableArgs<T>, ShellError> {
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = self.shell_manager.clone();
|
2019-08-09 07:36:43 +02:00
|
|
|
let source_map = self.call_info.source_map.clone();
|
2019-08-03 04:17:28 +02:00
|
|
|
let host = self.host.clone();
|
2019-08-02 21:15:07 +02:00
|
|
|
let args = self.evaluate_once(registry)?;
|
|
|
|
let (input, args) = args.split();
|
|
|
|
let name_span = args.call_info.name_span;
|
|
|
|
let mut deserializer = ConfigDeserializer::from_call_node(args);
|
|
|
|
|
|
|
|
Ok(RunnableArgs {
|
|
|
|
args: T::deserialize(&mut deserializer)?,
|
|
|
|
context: RunnableContext {
|
|
|
|
input: input,
|
2019-08-03 04:17:28 +02:00
|
|
|
commands: registry.clone(),
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-08-03 04:17:28 +02:00
|
|
|
name: name_span,
|
2019-08-09 07:36:43 +02:00
|
|
|
source_map,
|
2019-08-03 04:17:28 +02:00
|
|
|
host,
|
|
|
|
},
|
|
|
|
callback,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_raw<'de, T: Deserialize<'de>>(
|
|
|
|
self,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
callback: fn(T, RunnableContext, RawCommandArgs) -> Result<OutputStream, ShellError>,
|
|
|
|
) -> Result<RunnableRawArgs<T>, ShellError> {
|
|
|
|
let raw_args = RawCommandArgs {
|
|
|
|
host: self.host.clone(),
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: self.shell_manager.clone(),
|
2019-08-03 04:17:28 +02:00
|
|
|
call_info: self.call_info.clone(),
|
|
|
|
};
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = self.shell_manager.clone();
|
2019-08-09 07:36:43 +02:00
|
|
|
let source_map = self.call_info.source_map.clone();
|
2019-08-03 04:17:28 +02:00
|
|
|
let host = self.host.clone();
|
|
|
|
let args = self.evaluate_once(registry)?;
|
|
|
|
let (input, args) = args.split();
|
|
|
|
let name_span = args.call_info.name_span;
|
|
|
|
let mut deserializer = ConfigDeserializer::from_call_node(args);
|
|
|
|
|
|
|
|
Ok(RunnableRawArgs {
|
|
|
|
args: T::deserialize(&mut deserializer)?,
|
|
|
|
context: RunnableContext {
|
|
|
|
input: input,
|
|
|
|
commands: registry.clone(),
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-08-02 21:15:07 +02:00
|
|
|
name: name_span,
|
2019-08-09 07:36:43 +02:00
|
|
|
source_map,
|
2019-08-03 04:17:28 +02:00
|
|
|
host,
|
2019-08-02 21:15:07 +02:00
|
|
|
},
|
2019-08-03 04:17:28 +02:00
|
|
|
raw_args,
|
2019-08-02 21:15:07 +02:00
|
|
|
callback,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RunnableContext {
|
|
|
|
pub input: InputStream,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-08-03 04:17:28 +02:00
|
|
|
pub host: Arc<Mutex<dyn Host>>,
|
|
|
|
pub commands: CommandRegistry,
|
2019-08-09 07:36:43 +02:00
|
|
|
pub source_map: SourceMap,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub name: Span,
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RunnableContext {
|
|
|
|
pub fn cwd(&self) -> PathBuf {
|
2019-08-09 06:51:21 +02:00
|
|
|
PathBuf::from(self.shell_manager.path())
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-08-03 04:17:28 +02:00
|
|
|
|
|
|
|
pub fn expect_command(&self, name: &str) -> Arc<Command> {
|
|
|
|
self.commands
|
|
|
|
.get_command(name)
|
|
|
|
.expect(&format!("Expected command {}", name))
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RunnableArgs<T> {
|
|
|
|
args: T,
|
|
|
|
context: RunnableContext,
|
|
|
|
callback: fn(T, RunnableContext) -> Result<OutputStream, ShellError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RunnableArgs<T> {
|
|
|
|
pub fn run(self) -> Result<OutputStream, ShellError> {
|
|
|
|
(self.callback)(self.args, self.context)
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-03 04:17:28 +02:00
|
|
|
pub struct RunnableRawArgs<T> {
|
|
|
|
args: T,
|
|
|
|
raw_args: RawCommandArgs,
|
|
|
|
context: RunnableContext,
|
|
|
|
callback: fn(T, RunnableContext, RawCommandArgs) -> Result<OutputStream, ShellError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RunnableRawArgs<T> {
|
|
|
|
pub fn run(self) -> Result<OutputStream, ShellError> {
|
|
|
|
(self.callback)(self.args, self.context, self.raw_args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
pub struct EvaluatedStaticCommandArgs {
|
|
|
|
pub args: EvaluatedCommandArgs,
|
|
|
|
pub input: InputStream,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for EvaluatedStaticCommandArgs {
|
|
|
|
type Target = EvaluatedCommandArgs;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EvaluatedStaticCommandArgs {
|
|
|
|
pub fn new(
|
|
|
|
host: Arc<Mutex<dyn Host>>,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info: CallInfo,
|
|
|
|
input: impl Into<InputStream>,
|
|
|
|
) -> EvaluatedStaticCommandArgs {
|
|
|
|
EvaluatedStaticCommandArgs {
|
|
|
|
args: EvaluatedCommandArgs {
|
|
|
|
host,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
},
|
|
|
|
input: input.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
pub fn name_span(&self) -> Span {
|
2019-07-24 00:22:11 +02:00
|
|
|
self.args.call_info.name_span
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parts(self) -> (InputStream, registry::EvaluatedArgs) {
|
|
|
|
let EvaluatedStaticCommandArgs { args, input } = self;
|
|
|
|
|
|
|
|
(input, args.call_info.args)
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
pub fn split(self) -> (InputStream, EvaluatedCommandArgs) {
|
|
|
|
let EvaluatedStaticCommandArgs { args, input } = self;
|
|
|
|
|
|
|
|
(input, args)
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Getters)]
|
|
|
|
#[get = "pub"]
|
|
|
|
pub struct EvaluatedFilterCommandArgs {
|
|
|
|
args: EvaluatedCommandArgs,
|
2019-07-24 06:10:48 +02:00
|
|
|
#[allow(unused)]
|
2019-08-09 06:51:21 +02:00
|
|
|
input: Tagged<Value>,
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for EvaluatedFilterCommandArgs {
|
|
|
|
type Target = EvaluatedCommandArgs;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EvaluatedFilterCommandArgs {
|
|
|
|
pub fn new(
|
|
|
|
host: Arc<Mutex<dyn Host>>,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info: CallInfo,
|
2019-08-09 06:51:21 +02:00
|
|
|
input: Tagged<Value>,
|
2019-07-24 00:22:11 +02:00
|
|
|
) -> EvaluatedFilterCommandArgs {
|
|
|
|
EvaluatedFilterCommandArgs {
|
|
|
|
args: EvaluatedCommandArgs {
|
|
|
|
host,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
},
|
|
|
|
input,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
#[derive(Getters, new)]
|
2019-07-24 00:22:11 +02:00
|
|
|
#[get = "crate"]
|
|
|
|
pub struct EvaluatedCommandArgs {
|
|
|
|
pub host: Arc<Mutex<dyn Host>>,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
pub call_info: CallInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EvaluatedCommandArgs {
|
|
|
|
pub fn call_args(&self) -> ®istry::EvaluatedArgs {
|
|
|
|
&self.call_info.args
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
pub fn nth(&self, pos: usize) -> Option<&Tagged<Value>> {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.nth(pos)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
pub fn expect_nth(&self, pos: usize) -> Result<&Tagged<Value>, ShellError> {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.expect_nth(pos)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.len()
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
pub fn get(&self, name: &str) -> Option<&Tagged<Value>> {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.get(name)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
pub fn slice_from(&self, from: usize) -> Vec<Tagged<Value>> {
|
2019-08-02 21:15:07 +02:00
|
|
|
let positional = &self.call_info.args.positional;
|
|
|
|
|
|
|
|
match positional {
|
|
|
|
None => vec![],
|
|
|
|
Some(list) => list[from..].to_vec(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 21:10:25 +02:00
|
|
|
#[allow(unused)]
|
2019-06-22 05:43:37 +02:00
|
|
|
pub fn has(&self, name: &str) -> bool {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.has(name)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:56:48 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-05-13 19:30:51 +02:00
|
|
|
pub enum CommandAction {
|
2019-08-07 19:49:11 +02:00
|
|
|
ChangePath(String),
|
2019-07-19 21:48:14 +02:00
|
|
|
AddSpanSource(Uuid, SpanSource),
|
2019-06-13 23:47:25 +02:00
|
|
|
Exit,
|
2019-08-07 19:49:11 +02:00
|
|
|
EnterShell(String),
|
|
|
|
PreviousShell,
|
|
|
|
NextShell,
|
|
|
|
LeaveShell,
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 06:56:48 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-07-03 22:31:15 +02:00
|
|
|
pub enum ReturnSuccess {
|
2019-08-01 03:58:42 +02:00
|
|
|
Value(Tagged<Value>),
|
2019-05-13 19:30:51 +02:00
|
|
|
Action(CommandAction),
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
pub type ReturnValue = Result<ReturnSuccess, ShellError>;
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
impl From<Tagged<Value>> for ReturnValue {
|
|
|
|
fn from(input: Tagged<Value>) -> ReturnValue {
|
2019-07-03 22:31:15 +02:00
|
|
|
Ok(ReturnSuccess::Value(input))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReturnSuccess {
|
2019-08-07 19:49:11 +02:00
|
|
|
pub fn change_cwd(path: String) -> ReturnValue {
|
2019-07-03 22:31:15 +02:00
|
|
|
Ok(ReturnSuccess::Action(CommandAction::ChangePath(path)))
|
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
pub fn value(input: impl Into<Tagged<Value>>) -> ReturnValue {
|
2019-07-08 18:44:53 +02:00
|
|
|
Ok(ReturnSuccess::Value(input.into()))
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:48:14 +02:00
|
|
|
pub fn action(input: CommandAction) -> ReturnValue {
|
|
|
|
Ok(ReturnSuccess::Action(input))
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:44:53 +02:00
|
|
|
pub fn spanned_value(input: Value, span: Span) -> ReturnValue {
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(ReturnSuccess::Value(Tagged::from_simple_spanned_item(
|
|
|
|
input, span,
|
|
|
|
)))
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
2019-05-13 19:30:51 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub trait StaticCommand: Send + Sync {
|
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError>;
|
2019-05-28 08:45:18 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature {
|
2019-05-28 08:45:18 +02:00
|
|
|
name: self.name().to_string(),
|
2019-07-03 22:31:15 +02:00
|
|
|
positional: vec![],
|
2019-05-28 08:45:18 +02:00
|
|
|
rest_positional: true,
|
|
|
|
named: indexmap::IndexMap::new(),
|
2019-07-02 09:56:20 +02:00
|
|
|
is_filter: true,
|
2019-05-28 08:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub enum Command {
|
|
|
|
Static(Arc<dyn StaticCommand>),
|
|
|
|
}
|
2019-06-07 08:34:42 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl Command {
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Command::Static(command) => command.name(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn signature(&self) -> Signature {
|
|
|
|
match self {
|
|
|
|
Command::Static(command) => command.signature(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
match self {
|
|
|
|
Command::Static(command) => command.run(args, registry),
|
2019-06-07 08:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 06:10:48 +02:00
|
|
|
#[allow(unused)]
|
2019-07-24 00:22:11 +02:00
|
|
|
pub struct FnFilterCommand {
|
2019-05-28 08:45:18 +02:00
|
|
|
name: String,
|
2019-07-24 00:22:11 +02:00
|
|
|
func: fn(EvaluatedFilterCommandArgs) -> Result<OutputStream, ShellError>,
|
2019-05-13 19:30:51 +02:00
|
|
|
}
|
2019-05-22 09:12:03 +02:00
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl StaticCommand for FnFilterCommand {
|
2019-07-24 00:22:11 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let CommandArgs {
|
|
|
|
host,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
input,
|
|
|
|
} = args;
|
|
|
|
|
|
|
|
let host: Arc<Mutex<dyn Host>> = host.clone();
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = shell_manager.clone();
|
2019-07-24 00:22:11 +02:00
|
|
|
let registry: registry::CommandRegistry = registry.clone();
|
|
|
|
let func = self.func;
|
|
|
|
|
|
|
|
let result = input.values.map(move |it| {
|
|
|
|
let registry = registry.clone();
|
|
|
|
let call_info = match call_info
|
|
|
|
.clone()
|
|
|
|
.evaluate(®istry, &Scope::it_value(it.clone()))
|
|
|
|
{
|
|
|
|
Err(err) => return OutputStream::from(vec![Err(err)]).values,
|
|
|
|
Ok(args) => args,
|
|
|
|
};
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
let args =
|
|
|
|
EvaluatedFilterCommandArgs::new(host.clone(), shell_manager.clone(), call_info, it);
|
2019-07-24 00:22:11 +02:00
|
|
|
|
|
|
|
match func(args) {
|
|
|
|
Err(err) => return OutputStream::from(vec![Err(err)]).values,
|
|
|
|
Ok(stream) => stream.values,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let result = result.flatten();
|
|
|
|
let result: BoxStream<ReturnValue> = result.boxed();
|
|
|
|
|
|
|
|
Ok(result.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FnRawCommand {
|
|
|
|
name: String,
|
|
|
|
func: Box<
|
|
|
|
dyn Fn(CommandArgs, ®istry::CommandRegistry) -> Result<OutputStream, ShellError>
|
|
|
|
+ Send
|
|
|
|
+ Sync,
|
|
|
|
>,
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl StaticCommand for FnRawCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
(self.func)(args, registry)
|
2019-05-22 09:12:03 +02:00
|
|
|
}
|
2019-05-28 08:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn command(
|
|
|
|
name: &str,
|
2019-07-24 00:22:11 +02:00
|
|
|
func: Box<
|
|
|
|
dyn Fn(CommandArgs, ®istry::CommandRegistry) -> Result<OutputStream, ShellError>
|
|
|
|
+ Send
|
|
|
|
+ Sync,
|
|
|
|
>,
|
2019-08-02 21:15:07 +02:00
|
|
|
) -> Arc<Command> {
|
|
|
|
Arc::new(Command::Static(Arc::new(FnRawCommand {
|
2019-07-24 00:22:11 +02:00
|
|
|
name: name.to_string(),
|
|
|
|
func,
|
2019-08-02 21:15:07 +02:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn static_command(command: impl StaticCommand + 'static) -> Arc<Command> {
|
|
|
|
Arc::new(Command::Static(Arc::new(command)))
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 06:10:48 +02:00
|
|
|
#[allow(unused)]
|
2019-07-24 00:22:11 +02:00
|
|
|
pub fn filter(
|
|
|
|
name: &str,
|
|
|
|
func: fn(EvaluatedFilterCommandArgs) -> Result<OutputStream, ShellError>,
|
2019-08-02 21:15:07 +02:00
|
|
|
) -> Arc<Command> {
|
|
|
|
Arc::new(Command::Static(Arc::new(FnFilterCommand {
|
2019-06-07 08:34:42 +02:00
|
|
|
name: name.to_string(),
|
|
|
|
func,
|
2019-08-02 21:15:07 +02:00
|
|
|
})))
|
2019-06-07 08:34:42 +02:00
|
|
|
}
|