diff --git a/src/commands/pick.rs b/src/commands/pick.rs index 3c7cf99be..a456aa372 100644 --- a/src/commands/pick.rs +++ b/src/commands/pick.rs @@ -4,6 +4,11 @@ use crate::errors::ShellError; use crate::object::base::select_fields; use crate::prelude::*; +#[derive(Deserialize)] +struct PickArgs { + rest: Vec>, +} + pub struct Pick; impl WholeStreamCommand for Pick { @@ -12,7 +17,7 @@ impl WholeStreamCommand for Pick { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - pick(args, registry) + args.process(registry, pick)?.run() } fn name(&self) -> &str { @@ -20,22 +25,15 @@ impl WholeStreamCommand for Pick { } fn signature(&self) -> Signature { - Signature::build("pick").required("fields", SyntaxType::Any) + Signature::build("pick").rest() } } -fn pick(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; - let (input, args) = args.parts(); - - let fields: Result, _> = args - .positional - .iter() - .flatten() - .map(|a| a.as_string()) - .collect(); - - let fields = fields?; +fn pick( + PickArgs { rest: fields }: PickArgs, + RunnableContext { input, .. }: RunnableContext, +) -> Result { + let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect(); let objects = input .values diff --git a/src/commands/reject.rs b/src/commands/reject.rs index dbe305b14..67d8b1d82 100644 --- a/src/commands/reject.rs +++ b/src/commands/reject.rs @@ -3,6 +3,11 @@ use crate::errors::ShellError; use crate::object::base::reject_fields; use crate::prelude::*; +#[derive(Deserialize)] +pub struct RejectArgs { + rest: Vec>, +} + pub struct Reject; impl WholeStreamCommand for Reject { @@ -11,7 +16,7 @@ impl WholeStreamCommand for Reject { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - reject(args, registry) + args.process(registry, reject)?.run() } fn name(&self) -> &str { @@ -19,22 +24,15 @@ impl WholeStreamCommand for Reject { } fn signature(&self) -> Signature { - Signature::build("reject").required("fields", SyntaxType::Any) + Signature::build("reject").rest() } } -fn reject(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; - let (input, args) = args.parts(); - - let fields: Result, _> = args - .positional - .iter() - .flatten() - .map(|a| a.as_string()) - .collect(); - - let fields = fields?; +fn reject( + RejectArgs { rest: fields }: RejectArgs, + RunnableContext { input, .. }: RunnableContext, +) -> Result { + let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect(); let stream = input .values diff --git a/src/commands/split_column.rs b/src/commands/split_column.rs index bcc717b37..d0351bf1c 100644 --- a/src/commands/split_column.rs +++ b/src/commands/split_column.rs @@ -4,6 +4,11 @@ use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; use log::trace; +#[derive(Deserialize)] +struct SplitColumnArgs { + rest: Vec>, +} + pub struct SplitColumn; impl WholeStreamCommand for SplitColumn { @@ -12,7 +17,7 @@ impl WholeStreamCommand for SplitColumn { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - split_column(args, registry) + args.process(registry, split_column)?.run() } fn name(&self) -> &str { @@ -20,26 +25,25 @@ impl WholeStreamCommand for SplitColumn { } fn signature(&self) -> Signature { - // TODO: Signature? // TODO: Improve error. Old error had extra info: // // needs parameter (e.g. split-column ",") - Signature::build("split-column").required("delimeter", SyntaxType::Any) + Signature::build("split-column").rest() } } -fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; - let span = args.name_span(); - let (input, args) = args.parts(); - - let positional: Vec<_> = args.positional.iter().flatten().cloned().collect(); - +fn split_column( + SplitColumnArgs { rest: positional }: SplitColumnArgs, + RunnableContext { input, name, .. }: RunnableContext, +) -> Result { Ok(input .values .map(move |v| match v.item { Value::Primitive(Primitive::String(ref s)) => { - let splitter = positional[0].as_string().unwrap().replace("\\n", "\n"); + let positional: Vec<_> = positional.iter().map(|f| f.item.clone()).collect(); + + // TODO: Require at least 1 positional argument. + let splitter = positional[0].replace("\\n", "\n"); trace!("splitting with {:?}", splitter); let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect(); @@ -61,16 +65,13 @@ fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result Result Err(ShellError::labeled_error_with_secondary( "Expected a string from pipeline", "requires string input", - span, + name, "value originates from here", v.span(), )), diff --git a/src/commands/split_row.rs b/src/commands/split_row.rs index 4781bd35e..71e342b28 100644 --- a/src/commands/split_row.rs +++ b/src/commands/split_row.rs @@ -4,6 +4,11 @@ use crate::object::{Primitive, Value}; use crate::prelude::*; use log::trace; +#[derive(Deserialize)] +struct SplitRowArgs { + rest: Vec>, +} + pub struct SplitRow; impl WholeStreamCommand for SplitRow { @@ -12,7 +17,7 @@ impl WholeStreamCommand for SplitRow { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - split_row(args, registry) + args.process(registry, split_row)?.run() } fn name(&self) -> &str { @@ -20,26 +25,22 @@ impl WholeStreamCommand for SplitRow { } fn signature(&self) -> Signature { - // TODO: Signature? // TODO: Improve error. Old error had extra info: // // needs parameter (e.g. split-row ",") - Signature::build("split-row").required("delimeter", SyntaxType::Any) + Signature::build("split-row").rest() } } -fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result { - let args = args.evaluate_once(registry)?; - let span = args.name_span(); - let (input, args) = args.parts(); - - let positional: Vec> = args.positional.iter().flatten().cloned().collect(); - +fn split_row( + SplitRowArgs { rest: positional }: SplitRowArgs, + RunnableContext { input, name, .. }: RunnableContext, +) -> Result { let stream = input .values .map(move |v| match v.item { Value::Primitive(Primitive::String(ref s)) => { - let splitter = positional[0].as_string().unwrap().replace("\\n", "\n"); + let splitter = positional[0].item.replace("\\n", "\n"); trace!("splitting with {:?}", splitter); let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect(); @@ -58,7 +59,7 @@ fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result