2019-09-08 00:31:16 +02:00
|
|
|
use crate::data::Value;
|
2019-05-10 18:59:12 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-07-24 00:22:11 +02:00
|
|
|
use crate::evaluate::Scope;
|
|
|
|
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::ops::Deref;
|
2019-07-08 18:44:53 +02:00
|
|
|
use std::path::PathBuf;
|
2019-10-13 06:12:43 +02:00
|
|
|
use std::sync::atomic::AtomicBool;
|
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,
|
2019-09-14 18:30:24 +02:00
|
|
|
pub name_tag: Tag,
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnevaluatedCallInfo {
|
2019-08-14 19:02:39 +02:00
|
|
|
pub fn evaluate(
|
2019-07-24 00:22:11 +02:00
|
|
|
self,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
scope: &Scope,
|
|
|
|
) -> Result<CallInfo, ShellError> {
|
|
|
|
let args = self.args.evaluate(registry, scope, &self.source)?;
|
|
|
|
|
|
|
|
Ok(CallInfo {
|
|
|
|
args,
|
2019-09-14 18:30:24 +02:00
|
|
|
name_tag: self.name_tag,
|
2019-07-24 00:22:11 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-14 18:30:24 +02:00
|
|
|
pub name_tag: Tag,
|
2019-07-20 04:27:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-17 05:53:39 +02:00
|
|
|
impl CallInfo {
|
|
|
|
pub fn process<'de, T: Deserialize<'de>>(
|
|
|
|
&self,
|
|
|
|
shell_manager: &ShellManager,
|
2019-08-24 21:36:19 +02:00
|
|
|
callback: fn(T, &RunnablePerItemContext) -> Result<OutputStream, ShellError>,
|
2019-08-17 05:53:39 +02:00
|
|
|
) -> Result<RunnablePerItemArgs<T>, ShellError> {
|
|
|
|
let mut deserializer = ConfigDeserializer::from_call_info(self.clone());
|
|
|
|
|
|
|
|
Ok(RunnablePerItemArgs {
|
|
|
|
args: T::deserialize(&mut deserializer)?,
|
|
|
|
context: RunnablePerItemContext {
|
|
|
|
shell_manager: shell_manager.clone(),
|
2019-10-13 06:12:43 +02:00
|
|
|
name: self.name_tag.clone(),
|
2019-08-17 05:53:39 +02:00
|
|
|
},
|
|
|
|
callback,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
#[derive(Getters)]
|
2019-09-01 21:56:17 +02:00
|
|
|
#[get = "pub(crate)"]
|
2019-05-23 09:23:06 +02:00
|
|
|
pub struct CommandArgs {
|
2019-11-04 16:47:03 +01:00
|
|
|
pub host: Arc<Mutex<Box<dyn Host>>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
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-05-23 09:23:06 +02:00
|
|
|
pub input: InputStream,
|
2019-05-16 02:21:46 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 19:02:39 +02:00
|
|
|
#[derive(Getters, Clone)]
|
2019-09-01 21:56:17 +02:00
|
|
|
#[get = "pub(crate)"]
|
2019-08-03 04:17:28 +02:00
|
|
|
pub struct RawCommandArgs {
|
2019-11-04 16:47:03 +01:00
|
|
|
pub host: Arc<Mutex<Box<dyn Host>>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
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-11-21 15:33:14 +01:00
|
|
|
pub fn with_input(self, input: Vec<Value>) -> CommandArgs {
|
2019-08-03 04:17:28 +02:00
|
|
|
CommandArgs {
|
|
|
|
host: self.host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: self.ctrl_c,
|
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-11-04 16:47:03 +01:00
|
|
|
|
|
|
|
pub fn source(&self) -> Text {
|
|
|
|
self.call_info.source.clone()
|
|
|
|
}
|
2019-08-03 04:17:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:46:50 +01:00
|
|
|
impl std::fmt::Debug for CommandArgs {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
self.call_info.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2019-08-15 07:02:02 +02:00
|
|
|
) -> Result<EvaluatedWholeStreamCommandArgs, ShellError> {
|
2019-07-24 00:22:11 +02:00
|
|
|
let host = self.host.clone();
|
2019-10-13 06:12:43 +02:00
|
|
|
let ctrl_c = self.ctrl_c.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-15 07:02:02 +02:00
|
|
|
Ok(EvaluatedWholeStreamCommandArgs::new(
|
2019-08-09 06:51:21 +02:00
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
|
|
|
call_info,
|
|
|
|
input,
|
|
|
|
))
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
pub fn source(&self) -> Text {
|
|
|
|
self.call_info.source.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process<'de, T: Deserialize<'de>, O: ToOutputStream>(
|
2019-08-02 21:15:07 +02:00
|
|
|
self,
|
|
|
|
registry: &CommandRegistry,
|
2019-11-04 16:47:03 +01:00
|
|
|
callback: fn(T, RunnableContext) -> Result<O, ShellError>,
|
|
|
|
) -> Result<RunnableArgs<T, O>, ShellError> {
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = self.shell_manager.clone();
|
2019-08-03 04:17:28 +02:00
|
|
|
let host = self.host.clone();
|
2019-11-04 16:47:03 +01:00
|
|
|
let source = self.source();
|
2019-10-13 06:12:43 +02:00
|
|
|
let ctrl_c = self.ctrl_c.clone();
|
2019-08-02 21:15:07 +02:00
|
|
|
let args = self.evaluate_once(registry)?;
|
2019-10-13 06:12:43 +02:00
|
|
|
let call_info = args.call_info.clone();
|
2019-08-02 21:15:07 +02:00
|
|
|
let (input, args) = args.split();
|
2019-09-14 18:30:24 +02:00
|
|
|
let name_tag = args.call_info.name_tag;
|
2019-10-13 06:12:43 +02:00
|
|
|
let mut deserializer = ConfigDeserializer::from_call_info(call_info);
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
Ok(RunnableArgs {
|
|
|
|
args: T::deserialize(&mut deserializer)?,
|
|
|
|
context: RunnableContext {
|
2019-09-01 23:39:59 +02:00
|
|
|
input,
|
2019-08-03 04:17:28 +02:00
|
|
|
commands: registry.clone(),
|
2019-11-04 16:47:03 +01:00
|
|
|
source,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-09-14 18:30:24 +02:00
|
|
|
name: name_tag,
|
2019-08-03 04:17:28 +02:00
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-03 04:17:28 +02:00
|
|
|
},
|
|
|
|
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-10-13 06:12:43 +02:00
|
|
|
ctrl_c: self.ctrl_c.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-03 04:17:28 +02:00
|
|
|
let host = self.host.clone();
|
2019-11-04 16:47:03 +01:00
|
|
|
let source = self.source();
|
2019-10-13 06:12:43 +02:00
|
|
|
let ctrl_c = self.ctrl_c.clone();
|
2019-08-03 04:17:28 +02:00
|
|
|
let args = self.evaluate_once(registry)?;
|
2019-10-13 06:12:43 +02:00
|
|
|
let call_info = args.call_info.clone();
|
|
|
|
|
2019-08-03 04:17:28 +02:00
|
|
|
let (input, args) = args.split();
|
2019-09-14 18:30:24 +02:00
|
|
|
let name_tag = args.call_info.name_tag;
|
2019-10-13 06:12:43 +02:00
|
|
|
let mut deserializer = ConfigDeserializer::from_call_info(call_info.clone());
|
2019-08-03 04:17:28 +02:00
|
|
|
|
|
|
|
Ok(RunnableRawArgs {
|
|
|
|
args: T::deserialize(&mut deserializer)?,
|
|
|
|
context: RunnableContext {
|
2019-09-01 23:39:59 +02:00
|
|
|
input,
|
2019-08-03 04:17:28 +02:00
|
|
|
commands: registry.clone(),
|
2019-11-04 16:47:03 +01:00
|
|
|
source,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-09-14 18:30:24 +02:00
|
|
|
name: name_tag,
|
2019-08-03 04:17:28 +02:00
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 05:53:39 +02:00
|
|
|
pub struct RunnablePerItemContext {
|
|
|
|
pub shell_manager: ShellManager,
|
2019-09-14 18:30:24 +02:00
|
|
|
pub name: Tag,
|
2019-08-17 05:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RunnablePerItemContext {
|
|
|
|
pub fn cwd(&self) -> PathBuf {
|
|
|
|
PathBuf::from(self.shell_manager.path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub struct RunnableContext {
|
|
|
|
pub input: InputStream,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-11-04 16:47:03 +01:00
|
|
|
pub host: Arc<Mutex<Box<dyn Host>>>,
|
|
|
|
pub source: Text,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
2019-08-03 04:17:28 +02:00
|
|
|
pub commands: CommandRegistry,
|
2019-09-14 18:30:24 +02:00
|
|
|
pub name: Tag,
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RunnableContext {
|
2019-09-07 09:32:07 +02:00
|
|
|
pub fn get_command(&self, name: &str) -> Option<Arc<Command>> {
|
|
|
|
self.commands.get_command(name)
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-17 05:53:39 +02:00
|
|
|
pub struct RunnablePerItemArgs<T> {
|
|
|
|
args: T,
|
|
|
|
context: RunnablePerItemContext,
|
2019-08-24 21:36:19 +02:00
|
|
|
callback: fn(T, &RunnablePerItemContext) -> Result<OutputStream, ShellError>,
|
2019-08-17 05:53:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RunnablePerItemArgs<T> {
|
2019-08-24 21:36:19 +02:00
|
|
|
pub fn run(self) -> Result<OutputStream, ShellError> {
|
2019-08-17 05:53:39 +02:00
|
|
|
(self.callback)(self.args, &self.context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
pub struct RunnableArgs<T, O: ToOutputStream> {
|
2019-08-02 21:15:07 +02:00
|
|
|
args: T,
|
|
|
|
context: RunnableContext,
|
2019-11-04 16:47:03 +01:00
|
|
|
callback: fn(T, RunnableContext) -> Result<O, ShellError>,
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
impl<T, O: ToOutputStream> RunnableArgs<T, O> {
|
2019-08-02 21:15:07 +02:00
|
|
|
pub fn run(self) -> Result<OutputStream, ShellError> {
|
2019-11-04 16:47:03 +01:00
|
|
|
(self.callback)(self.args, self.context).map(|v| v.to_output_stream())
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
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> {
|
2019-08-17 05:53:39 +02:00
|
|
|
pub fn run(self) -> OutputStream {
|
|
|
|
match (self.callback)(self.args, self.context, self.raw_args) {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(err) => OutputStream::one(Err(err)),
|
|
|
|
}
|
2019-08-03 04:17:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
pub struct EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub args: EvaluatedCommandArgs,
|
|
|
|
pub input: InputStream,
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl Deref for EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
type Target = EvaluatedCommandArgs;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub fn new(
|
|
|
|
host: Arc<Mutex<dyn Host>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: Arc<AtomicBool>,
|
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>,
|
2019-08-15 07:02:02 +02:00
|
|
|
) -> EvaluatedWholeStreamCommandArgs {
|
|
|
|
EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
args: EvaluatedCommandArgs {
|
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
},
|
|
|
|
input: input.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:30:24 +02:00
|
|
|
pub fn name_tag(&self) -> Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
self.args.call_info.name_tag.clone()
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parts(self) -> (InputStream, registry::EvaluatedArgs) {
|
2019-08-15 07:02:02 +02:00
|
|
|
let EvaluatedWholeStreamCommandArgs { args, input } = self;
|
2019-07-24 00:22:11 +02:00
|
|
|
|
|
|
|
(input, args.call_info.args)
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
pub fn split(self) -> (InputStream, EvaluatedCommandArgs) {
|
2019-08-15 07:02:02 +02:00
|
|
|
let EvaluatedWholeStreamCommandArgs { args, input } = self;
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
(input, args)
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Getters)]
|
|
|
|
#[get = "pub"]
|
|
|
|
pub struct EvaluatedFilterCommandArgs {
|
|
|
|
args: EvaluatedCommandArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-13 06:12:43 +02:00
|
|
|
ctrl_c: Arc<AtomicBool>,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info: CallInfo,
|
|
|
|
) -> EvaluatedFilterCommandArgs {
|
|
|
|
EvaluatedFilterCommandArgs {
|
|
|
|
args: EvaluatedCommandArgs {
|
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
#[derive(Getters, new)]
|
2019-09-01 21:56:17 +02:00
|
|
|
#[get = "pub(crate)"]
|
2019-07-24 00:22:11 +02:00
|
|
|
pub struct EvaluatedCommandArgs {
|
|
|
|
pub host: Arc<Mutex<dyn Host>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
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-11-21 15:33:14 +01:00
|
|
|
pub fn nth(&self, pos: usize) -> Option<&Value> {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.nth(pos)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn expect_nth(&self, pos: usize) -> Result<&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-11-21 15:33:14 +01:00
|
|
|
pub fn get(&self, name: &str) -> Option<&Value> {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.get(name)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn slice_from(&self, from: usize) -> Vec<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-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-10-28 15:46:50 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2019-05-13 19:30:51 +02:00
|
|
|
pub enum CommandAction {
|
2019-08-07 19:49:11 +02:00
|
|
|
ChangePath(String),
|
2019-06-13 23:47:25 +02:00
|
|
|
Exit,
|
2019-11-04 16:47:03 +01:00
|
|
|
Error(ShellError),
|
2019-08-07 19:49:11 +02:00
|
|
|
EnterShell(String),
|
2019-11-21 15:33:14 +01:00
|
|
|
EnterValueShell(Value),
|
|
|
|
EnterHelpShell(Value),
|
2019-08-07 19:49:11 +02:00
|
|
|
PreviousShell,
|
|
|
|
NextShell,
|
|
|
|
LeaveShell,
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebug for CommandAction {
|
|
|
|
fn pretty(&self) -> DebugDocBuilder {
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
match self {
|
2019-11-21 15:33:14 +01:00
|
|
|
CommandAction::ChangePath(path) => b::typed("change path", b::description(path)),
|
|
|
|
CommandAction::Exit => b::description("exit"),
|
|
|
|
CommandAction::Error(_) => b::error("error"),
|
|
|
|
CommandAction::EnterShell(s) => b::typed("enter shell", b::description(s)),
|
|
|
|
CommandAction::EnterValueShell(v) => b::typed("enter value shell", v.pretty()),
|
|
|
|
CommandAction::EnterHelpShell(v) => b::typed("enter help shell", v.pretty()),
|
|
|
|
CommandAction::PreviousShell => b::description("previous shell"),
|
|
|
|
CommandAction::NextShell => b::description("next shell"),
|
|
|
|
CommandAction::LeaveShell => b::description("leave shell"),
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:46:50 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2019-07-03 22:31:15 +02:00
|
|
|
pub enum ReturnSuccess {
|
2019-11-21 15:33:14 +01:00
|
|
|
Value(Value),
|
|
|
|
DebugValue(Value),
|
2019-05-13 19:30:51 +02:00
|
|
|
Action(CommandAction),
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebug for ReturnSuccess {
|
|
|
|
fn pretty(&self) -> DebugDocBuilder {
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
match self {
|
2019-11-21 15:33:14 +01:00
|
|
|
ReturnSuccess::Value(value) => b::typed("value", value.pretty()),
|
|
|
|
ReturnSuccess::DebugValue(value) => b::typed("debug value", value.pretty()),
|
|
|
|
ReturnSuccess::Action(action) => b::typed("action", action.pretty()),
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub type ReturnValue = Result<ReturnSuccess, ShellError>;
|
|
|
|
|
|
|
|
impl From<Value> for ReturnValue {
|
|
|
|
fn from(input: 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-11-21 15:33:14 +01:00
|
|
|
pub fn value(input: impl Into<Value>) -> ReturnValue {
|
2019-07-08 18:44:53 +02:00
|
|
|
Ok(ReturnSuccess::Value(input.into()))
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn debug_value(input: impl Into<Value>) -> ReturnValue {
|
2019-11-04 16:47:03 +01:00
|
|
|
Ok(ReturnSuccess::DebugValue(input.into()))
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:48:14 +02:00
|
|
|
pub fn action(input: CommandAction) -> ReturnValue {
|
|
|
|
Ok(ReturnSuccess::Action(input))
|
|
|
|
}
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
2019-05-13 19:30:51 +02:00
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
pub trait WholeStreamCommand: Send + Sync {
|
2019-08-02 21:15:07 +02:00
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature {
|
2019-05-28 08:45:18 +02:00
|
|
|
name: self.name().to_string(),
|
2019-08-30 00:52:32 +02:00
|
|
|
usage: self.usage().to_string(),
|
2019-07-03 22:31:15 +02:00
|
|
|
positional: vec![],
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
rest_positional: None,
|
2019-05-28 08:45:18 +02:00
|
|
|
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-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str;
|
2019-08-14 19:02:39 +02:00
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2019-08-30 00:52:32 +02:00
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
2019-08-24 21:36:19 +02:00
|
|
|
) -> Result<OutputStream, ShellError>;
|
2019-09-04 03:50:23 +02:00
|
|
|
|
|
|
|
fn is_binary(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PerItemCommand: Send + Sync {
|
|
|
|
fn name(&self) -> &str;
|
2019-08-14 19:02:39 +02:00
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature {
|
|
|
|
name: self.name().to_string(),
|
2019-08-30 00:52:32 +02:00
|
|
|
usage: self.usage().to_string(),
|
2019-08-14 19:02:39 +02:00
|
|
|
positional: vec![],
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
rest_positional: None,
|
2019-08-14 19:02:39 +02:00
|
|
|
named: indexmap::IndexMap::new(),
|
|
|
|
is_filter: true,
|
|
|
|
}
|
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str;
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
call_info: &CallInfo,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
2019-11-21 15:33:14 +01:00
|
|
|
input: Value,
|
2019-08-30 00:52:32 +02:00
|
|
|
) -> Result<OutputStream, ShellError>;
|
2019-09-04 03:50:23 +02:00
|
|
|
|
|
|
|
fn is_binary(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2019-08-14 19:02:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
pub enum Command {
|
2019-08-15 07:02:02 +02:00
|
|
|
WholeStream(Arc<dyn WholeStreamCommand>),
|
2019-08-14 19:02:39 +02:00
|
|
|
PerItem(Arc<dyn PerItemCommand>),
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-06-07 08:34:42 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebugWithSource for Command {
|
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
|
|
|
match self {
|
|
|
|
Command::WholeStream(command) => b::typed(
|
|
|
|
"whole stream command",
|
|
|
|
b::description(command.name())
|
|
|
|
+ b::space()
|
|
|
|
+ b::equals()
|
|
|
|
+ b::space()
|
|
|
|
+ command.signature().pretty_debug(source),
|
|
|
|
),
|
|
|
|
Command::PerItem(command) => b::typed(
|
|
|
|
"per item command",
|
|
|
|
b::description(command.name())
|
|
|
|
+ b::space()
|
|
|
|
+ b::equals()
|
|
|
|
+ b::space()
|
|
|
|
+ command.signature().pretty_debug(source),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
|
|
|
impl std::fmt::Debug for Command {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Command::WholeStream(command) => write!(f, "WholeStream({})", command.name()),
|
|
|
|
Command::PerItem(command) => write!(f, "PerItem({})", command.name()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl Command {
|
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
match self {
|
2019-08-15 07:02:02 +02:00
|
|
|
Command::WholeStream(command) => command.name(),
|
2019-08-14 19:02:39 +02:00
|
|
|
Command::PerItem(command) => command.name(),
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn signature(&self) -> Signature {
|
|
|
|
match self {
|
2019-08-15 07:02:02 +02:00
|
|
|
Command::WholeStream(command) => command.signature(),
|
2019-08-14 19:02:39 +02:00
|
|
|
Command::PerItem(command) => command.signature(),
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
pub fn usage(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Command::WholeStream(command) => command.usage(),
|
|
|
|
Command::PerItem(command) => command.usage(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 02:04:01 +01:00
|
|
|
pub fn run(&self, args: CommandArgs, registry: ®istry::CommandRegistry) -> OutputStream {
|
2019-08-02 21:15:07 +02:00
|
|
|
match self {
|
2019-08-17 05:53:39 +02:00
|
|
|
Command::WholeStream(command) => match command.run(args, registry) {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(err) => OutputStream::one(Err(err)),
|
|
|
|
},
|
2019-11-04 02:04:01 +01:00
|
|
|
Command::PerItem(command) => self.run_helper(command.clone(), args, registry.clone()),
|
2019-06-07 08:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 19:02:39 +02:00
|
|
|
|
|
|
|
fn run_helper(
|
|
|
|
&self,
|
|
|
|
command: Arc<dyn PerItemCommand>,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: CommandRegistry,
|
2019-08-17 05:53:39 +02:00
|
|
|
) -> OutputStream {
|
2019-08-14 19:02:39 +02:00
|
|
|
let raw_args = RawCommandArgs {
|
|
|
|
host: args.host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: args.ctrl_c,
|
2019-08-14 19:02:39 +02:00
|
|
|
shell_manager: args.shell_manager,
|
|
|
|
call_info: args.call_info,
|
|
|
|
};
|
2019-08-15 07:02:02 +02:00
|
|
|
|
2019-11-04 02:04:01 +01:00
|
|
|
let out = args
|
|
|
|
.input
|
|
|
|
.values
|
|
|
|
.map(move |x| {
|
|
|
|
let call_info = raw_args
|
|
|
|
.clone()
|
|
|
|
.call_info
|
|
|
|
.evaluate(®istry, &Scope::it_value(x.clone()))
|
|
|
|
.unwrap();
|
|
|
|
match command.run(&call_info, ®istry, &raw_args, x) {
|
|
|
|
Ok(o) => o,
|
|
|
|
Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]).to_output_stream(),
|
2019-10-28 19:40:34 +01:00
|
|
|
}
|
2019-11-04 02:04:01 +01:00
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
out.to_output_stream()
|
2019-08-14 19:02:39 +02:00
|
|
|
}
|
2019-09-04 03:50:23 +02:00
|
|
|
|
|
|
|
pub fn is_binary(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Command::WholeStream(command) => command.is_binary(),
|
|
|
|
Command::PerItem(command) => command.is_binary(),
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 08:34:42 +02:00
|
|
|
}
|
|
|
|
|
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-15 07:02:02 +02:00
|
|
|
impl WholeStreamCommand for FnFilterCommand {
|
2019-07-24 00:22:11 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"usage"
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: ®istry::CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let CommandArgs {
|
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
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();
|
2019-09-02 08:11:05 +02:00
|
|
|
let call_info = match call_info.clone().evaluate(®istry, &Scope::it_value(it)) {
|
2019-07-24 00:22:11 +02:00
|
|
|
Err(err) => return OutputStream::from(vec![Err(err)]).values,
|
|
|
|
Ok(args) => args,
|
|
|
|
};
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
let args = EvaluatedFilterCommandArgs::new(
|
|
|
|
host.clone(),
|
|
|
|
ctrl_c.clone(),
|
|
|
|
shell_manager.clone(),
|
|
|
|
call_info,
|
|
|
|
);
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
pub fn whole_stream_command(command: impl WholeStreamCommand + 'static) -> Arc<Command> {
|
|
|
|
Arc::new(Command::WholeStream(Arc::new(command)))
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 19:02:39 +02:00
|
|
|
pub fn per_item_command(command: impl PerItemCommand + 'static) -> Arc<Command> {
|
|
|
|
Arc::new(Command::PerItem(Arc::new(command)))
|
|
|
|
}
|