forked from extern/nushell
Finish updating the last cases
This commit is contained in:
parent
421aacee76
commit
64c129d65f
@ -5,7 +5,7 @@ use crate::prelude::*;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct NthArgs {
|
||||
position: Tagged<i64>,
|
||||
amount: Tagged<i64>,
|
||||
}
|
||||
|
||||
pub struct Nth;
|
||||
@ -24,12 +24,12 @@ impl WholeStreamCommand for Nth {
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("nth").optional("amount", SyntaxType::Any)
|
||||
Signature::build("nth").required("amount", SyntaxType::Any)
|
||||
}
|
||||
}
|
||||
|
||||
fn nth(
|
||||
NthArgs { position: amount }: NthArgs,
|
||||
NthArgs { amount }: NthArgs,
|
||||
RunnableContext { input, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
Ok(OutputStream::from_input(
|
||||
|
@ -31,8 +31,16 @@ impl WholeStreamCommand for Pick {
|
||||
|
||||
fn pick(
|
||||
PickArgs { rest: fields }: PickArgs,
|
||||
RunnableContext { input, .. }: RunnableContext,
|
||||
RunnableContext { input, name, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
if fields.len() == 0 {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Pick requires fields",
|
||||
"needs parameter",
|
||||
name,
|
||||
));
|
||||
}
|
||||
|
||||
let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
let objects = input
|
||||
|
@ -30,8 +30,16 @@ impl WholeStreamCommand for Reject {
|
||||
|
||||
fn reject(
|
||||
RejectArgs { rest: fields }: RejectArgs,
|
||||
RunnableContext { input, .. }: RunnableContext,
|
||||
RunnableContext { input, name, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
if fields.len() == 0 {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Reject requires fields",
|
||||
"needs parameter",
|
||||
name,
|
||||
));
|
||||
}
|
||||
|
||||
let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
let stream = input
|
||||
|
@ -6,6 +6,7 @@ use log::trace;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SplitColumnArgs {
|
||||
separator: Tagged<String>,
|
||||
rest: Vec<Tagged<String>>,
|
||||
}
|
||||
|
||||
@ -25,32 +26,30 @@ impl WholeStreamCommand for SplitColumn {
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
// TODO: Improve error. Old error had extra info:
|
||||
//
|
||||
// needs parameter (e.g. split-column ",")
|
||||
Signature::build("split-column").rest()
|
||||
Signature::build("split-column")
|
||||
.required("separator", SyntaxType::Any)
|
||||
.rest()
|
||||
}
|
||||
}
|
||||
|
||||
fn split_column(
|
||||
SplitColumnArgs { rest: positional }: SplitColumnArgs,
|
||||
SplitColumnArgs { separator, rest }: SplitColumnArgs,
|
||||
RunnableContext { input, name, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
Ok(input
|
||||
.values
|
||||
.map(move |v| match v.item {
|
||||
Value::Primitive(Primitive::String(ref s)) => {
|
||||
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");
|
||||
let splitter = separator.replace("\\n", "\n");
|
||||
trace!("splitting with {:?}", splitter);
|
||||
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
||||
|
||||
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
||||
trace!("split result = {:?}", split_result);
|
||||
|
||||
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
// If they didn't provide column names, make up our own
|
||||
if (positional.len() - 1) == 0 {
|
||||
if positional.len() == 0 {
|
||||
let mut gen_columns = vec![];
|
||||
for i in 0..split_result.len() {
|
||||
gen_columns.push(format!("Column{}", i + 1));
|
||||
@ -62,16 +61,16 @@ fn split_column(
|
||||
}
|
||||
|
||||
ReturnSuccess::value(dict.into_tagged_value())
|
||||
} else if split_result.len() == (positional.len() - 1) {
|
||||
} else if split_result.len() == positional.len() {
|
||||
let mut dict = TaggedDictBuilder::new(v.tag());
|
||||
for (&k, v) in split_result.iter().zip(positional.iter().skip(1)) {
|
||||
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
||||
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.trim(), Primitive::String("".into()));
|
||||
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
||||
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
|
||||
}
|
||||
ReturnSuccess::value(dict.into_tagged_value())
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use log::trace;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SplitRowArgs {
|
||||
rest: Vec<Tagged<String>>,
|
||||
separator: Tagged<String>,
|
||||
}
|
||||
|
||||
pub struct SplitRow;
|
||||
@ -25,22 +25,19 @@ impl WholeStreamCommand for SplitRow {
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
// TODO: Improve error. Old error had extra info:
|
||||
//
|
||||
// needs parameter (e.g. split-row ",")
|
||||
Signature::build("split-row").rest()
|
||||
Signature::build("split-row").required("separator", SyntaxType::Any)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_row(
|
||||
SplitRowArgs { rest: positional }: SplitRowArgs,
|
||||
SplitRowArgs { separator }: 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].item.replace("\\n", "\n");
|
||||
let splitter = separator.item.replace("\\n", "\n");
|
||||
trace!("splitting with {:?}", splitter);
|
||||
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
||||
|
||||
|
@ -22,11 +22,13 @@ impl WholeStreamCommand for ToArray {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_array(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
fn to_array(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once(registry)?;
|
||||
let span = args.call_info.name_span;
|
||||
let out = args.input.values.collect();
|
||||
|
||||
Ok(out
|
||||
.map(|vec: Vec<_>| stream![Value::List(vec).tagged_unknown()]) // TODO: args.input should have a span
|
||||
.map(move |vec: Vec<_>| stream![Value::List(vec).simple_spanned(span)])
|
||||
.flatten_stream()
|
||||
.from_input_stream())
|
||||
}
|
||||
|
@ -186,13 +186,14 @@ impl ShellError {
|
||||
ArgumentError::MissingMandatoryPositional(name) => Diagnostic::new(
|
||||
Severity::Error,
|
||||
format!(
|
||||
"{} requires {}",
|
||||
"{} requires {} parameter",
|
||||
Color::Cyan.paint(command),
|
||||
Color::Green.bold().paint(name)
|
||||
Color::Green.bold().paint(name.clone())
|
||||
),
|
||||
)
|
||||
.with_label(Label::new_primary(span)),
|
||||
|
||||
.with_label(
|
||||
Label::new_primary(span).with_message(format!("requires {} parameter", name)),
|
||||
),
|
||||
ArgumentError::MissingValueForName(name) => Diagnostic::new(
|
||||
Severity::Error,
|
||||
format!(
|
||||
|
Loading…
Reference in New Issue
Block a user