forked from extern/nushell
Use args structs to better extract multiple arguments
This commit is contained in:
parent
9951691023
commit
31790a9906
@ -4,6 +4,11 @@ use crate::errors::ShellError;
|
||||
use crate::object::base::select_fields;
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PickArgs {
|
||||
rest: Vec<Tagged<String>>,
|
||||
}
|
||||
|
||||
pub struct Pick;
|
||||
|
||||
impl WholeStreamCommand for Pick {
|
||||
@ -12,7 +17,7 @@ impl WholeStreamCommand for Pick {
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
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<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once(registry)?;
|
||||
let (input, args) = args.parts();
|
||||
|
||||
let fields: Result<Vec<String>, _> = args
|
||||
.positional
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|a| a.as_string())
|
||||
.collect();
|
||||
|
||||
let fields = fields?;
|
||||
fn pick(
|
||||
PickArgs { rest: fields }: PickArgs,
|
||||
RunnableContext { input, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
let objects = input
|
||||
.values
|
||||
|
@ -3,6 +3,11 @@ use crate::errors::ShellError;
|
||||
use crate::object::base::reject_fields;
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct RejectArgs {
|
||||
rest: Vec<Tagged<String>>,
|
||||
}
|
||||
|
||||
pub struct Reject;
|
||||
|
||||
impl WholeStreamCommand for Reject {
|
||||
@ -11,7 +16,7 @@ impl WholeStreamCommand for Reject {
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
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<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once(registry)?;
|
||||
let (input, args) = args.parts();
|
||||
|
||||
let fields: Result<Vec<String>, _> = args
|
||||
.positional
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|a| a.as_string())
|
||||
.collect();
|
||||
|
||||
let fields = fields?;
|
||||
fn reject(
|
||||
RejectArgs { rest: fields }: RejectArgs,
|
||||
RunnableContext { input, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
let stream = input
|
||||
.values
|
||||
|
@ -4,6 +4,11 @@ use crate::object::{Primitive, TaggedDictBuilder, Value};
|
||||
use crate::prelude::*;
|
||||
use log::trace;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SplitColumnArgs {
|
||||
rest: Vec<Tagged<String>>,
|
||||
}
|
||||
|
||||
pub struct SplitColumn;
|
||||
|
||||
impl WholeStreamCommand for SplitColumn {
|
||||
@ -12,7 +17,7 @@ impl WholeStreamCommand for SplitColumn {
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
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<OutputStream, ShellError> {
|
||||
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<OutputStream, ShellError> {
|
||||
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<OutputS
|
||||
} else if split_result.len() == (positional.len() - 1) {
|
||||
let mut dict = TaggedDictBuilder::new(v.tag());
|
||||
for (&k, v) in split_result.iter().zip(positional.iter().skip(1)) {
|
||||
dict.insert(
|
||||
v.as_string().unwrap(),
|
||||
Value::Primitive(Primitive::String(k.into())),
|
||||
);
|
||||
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
|
||||
}
|
||||
ReturnSuccess::value(dict.into_tagged_value())
|
||||
} else {
|
||||
let mut dict = TaggedDictBuilder::new(v.tag());
|
||||
for k in positional.iter().skip(1) {
|
||||
dict.insert(k.as_string().unwrap().trim(), Primitive::String("".into()));
|
||||
dict.insert(k.trim(), Primitive::String("".into()));
|
||||
}
|
||||
ReturnSuccess::value(dict.into_tagged_value())
|
||||
}
|
||||
@ -78,7 +79,7 @@ fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputS
|
||||
_ => Err(ShellError::labeled_error_with_secondary(
|
||||
"Expected a string from pipeline",
|
||||
"requires string input",
|
||||
span,
|
||||
name,
|
||||
"value originates from here",
|
||||
v.span(),
|
||||
)),
|
||||
|
@ -4,6 +4,11 @@ use crate::object::{Primitive, Value};
|
||||
use crate::prelude::*;
|
||||
use log::trace;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SplitRowArgs {
|
||||
rest: Vec<Tagged<String>>,
|
||||
}
|
||||
|
||||
pub struct SplitRow;
|
||||
|
||||
impl WholeStreamCommand for SplitRow {
|
||||
@ -12,7 +17,7 @@ impl WholeStreamCommand for SplitRow {
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
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<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once(registry)?;
|
||||
let span = args.name_span();
|
||||
let (input, args) = args.parts();
|
||||
|
||||
let positional: Vec<Tagged<Value>> = args.positional.iter().flatten().cloned().collect();
|
||||
|
||||
fn split_row(
|
||||
SplitRowArgs { rest: positional }: SplitRowArgs,
|
||||
RunnableContext { input, name, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
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<OutputStre
|
||||
result.push_back(Err(ShellError::labeled_error_with_secondary(
|
||||
"Expected a string from pipeline",
|
||||
"requires string input",
|
||||
span,
|
||||
name,
|
||||
"value originates from here",
|
||||
v.span(),
|
||||
)));
|
||||
|
Loading…
Reference in New Issue
Block a user