forked from extern/nushell
commands to engine p (#3417)
* commands to engine p * Clippy suggestion * Clippy suggestion
This commit is contained in:
parent
3aa00b78f9
commit
0905a2c3a2
@ -2,16 +2,11 @@ use crate::prelude::*;
|
|||||||
use nu_data::base::select_fields;
|
use nu_data::base::select_fields;
|
||||||
use nu_engine::WholeStreamCommand;
|
use nu_engine::WholeStreamCommand;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape};
|
use nu_protocol::{Signature, SyntaxShape};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
|
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Arguments {
|
|
||||||
columns: Option<Tagged<u64>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WholeStreamCommand for SubCommand {
|
impl WholeStreamCommand for SubCommand {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"drop column"
|
"drop column"
|
||||||
@ -29,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
|
|||||||
"Remove the last number of columns. If you want to remove columns by name, try 'reject'."
|
"Remove the last number of columns. If you want to remove columns by name, try 'reject'."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
drop(args)
|
drop(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,8 +42,9 @@ impl WholeStreamCommand for SubCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn drop(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let (Arguments { columns }, input) = args.process()?;
|
let args = args.evaluate_once()?;
|
||||||
|
let columns: Option<Tagged<u64>> = args.opt(0)?;
|
||||||
|
|
||||||
let to_drop = if let Some(quantity) = columns {
|
let to_drop = if let Some(quantity) = columns {
|
||||||
*quantity as usize
|
*quantity as usize
|
||||||
@ -56,7 +52,8 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||||||
1
|
1
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(input
|
Ok(args
|
||||||
|
.input
|
||||||
.map(move |item| {
|
.map(move |item| {
|
||||||
let headers = item.data_descriptors();
|
let headers = item.data_descriptors();
|
||||||
|
|
||||||
@ -66,10 +63,9 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||||||
n => &headers[..n - to_drop],
|
n => &headers[..n - to_drop],
|
||||||
};
|
};
|
||||||
|
|
||||||
select_fields(&item, descs, item.tag())
|
Ok(select_fields(&item, descs, item.tag()))
|
||||||
})
|
})
|
||||||
.map(ReturnSuccess::value)
|
.to_input_stream())
|
||||||
.to_action_stream())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -6,11 +6,6 @@ use nu_source::Tagged;
|
|||||||
|
|
||||||
pub struct Command;
|
pub struct Command;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Arguments {
|
|
||||||
rows: Option<Tagged<u64>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WholeStreamCommand for Command {
|
impl WholeStreamCommand for Command {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"drop"
|
"drop"
|
||||||
@ -28,7 +23,7 @@ impl WholeStreamCommand for Command {
|
|||||||
"Remove the last number of rows or columns."
|
"Remove the last number of rows or columns."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
drop(args)
|
drop(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,9 +46,10 @@ impl WholeStreamCommand for Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn drop(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let (Arguments { rows }, input) = args.process()?;
|
let args = args.evaluate_once()?;
|
||||||
let v: Vec<_> = input.into_vec();
|
let rows: Option<Tagged<u64>> = args.opt(0)?;
|
||||||
|
let v: Vec<_> = args.input.into_vec();
|
||||||
|
|
||||||
let rows_to_drop = if let Some(quantity) = rows {
|
let rows_to_drop = if let Some(quantity) = rows {
|
||||||
*quantity as usize
|
*quantity as usize
|
||||||
@ -62,7 +58,7 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Ok(if rows_to_drop == 0 {
|
Ok(if rows_to_drop == 0 {
|
||||||
v.into_iter().to_action_stream()
|
v.into_iter().map(Ok).to_input_stream()
|
||||||
} else {
|
} else {
|
||||||
let k = if v.len() < rows_to_drop {
|
let k = if v.len() < rows_to_drop {
|
||||||
0
|
0
|
||||||
@ -70,8 +66,8 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||||||
v.len() - rows_to_drop
|
v.len() - rows_to_drop
|
||||||
};
|
};
|
||||||
|
|
||||||
let iter = v.into_iter().take(k);
|
let iter = v.into_iter().map(Ok).take(k);
|
||||||
|
|
||||||
iter.to_action_stream()
|
iter.to_input_stream()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -7,17 +7,9 @@ use nu_protocol::{
|
|||||||
Signature, SyntaxShape, UntaggedValue, Value,
|
Signature, SyntaxShape, UntaggedValue, Value,
|
||||||
};
|
};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
pub struct EachGroup;
|
pub struct EachGroup;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct EachGroupArgs {
|
|
||||||
group_size: Tagged<usize>,
|
|
||||||
block: CapturedBlock,
|
|
||||||
//numbered: Tagged<bool>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WholeStreamCommand for EachGroup {
|
impl WholeStreamCommand for EachGroup {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"each group"
|
"each group"
|
||||||
@ -45,21 +37,24 @@ impl WholeStreamCommand for EachGroup {
|
|||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_with_actions(&self, raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn run(&self, raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let context = Arc::new(EvaluationContext::from_args(&raw_args));
|
let context = Arc::new(EvaluationContext::from_args(&raw_args));
|
||||||
let external_redirection = raw_args.call_info.args.external_redirection;
|
let external_redirection = raw_args.call_info.args.external_redirection;
|
||||||
let (each_args, input): (EachGroupArgs, _) = raw_args.process()?;
|
let args = raw_args.evaluate_once()?;
|
||||||
let block = Arc::new(Box::new(each_args.block));
|
|
||||||
|
let group_size: Tagged<usize> = args.req(0)?;
|
||||||
|
let block: CapturedBlock = args.req(1)?;
|
||||||
|
let block = Arc::new(Box::new(block));
|
||||||
|
|
||||||
let each_group_iterator = EachGroupIterator {
|
let each_group_iterator = EachGroupIterator {
|
||||||
block,
|
block,
|
||||||
context,
|
context,
|
||||||
group_size: each_args.group_size.item,
|
group_size: group_size.item,
|
||||||
input,
|
input: args.input,
|
||||||
external_redirection,
|
external_redirection,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(each_group_iterator.flatten().to_action_stream())
|
Ok(each_group_iterator.flatten().map(Ok).to_input_stream())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,17 +5,9 @@ use nu_engine::WholeStreamCommand;
|
|||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{hir::CapturedBlock, Primitive, Signature, SyntaxShape, UntaggedValue};
|
use nu_protocol::{hir::CapturedBlock, Primitive, Signature, SyntaxShape, UntaggedValue};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
pub struct EachWindow;
|
pub struct EachWindow;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct EachWindowArgs {
|
|
||||||
window_size: Tagged<usize>,
|
|
||||||
block: CapturedBlock,
|
|
||||||
stride: Option<Tagged<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WholeStreamCommand for EachWindow {
|
impl WholeStreamCommand for EachWindow {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"each window"
|
"each window"
|
||||||
@ -49,22 +41,29 @@ impl WholeStreamCommand for EachWindow {
|
|||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_with_actions(&self, raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn run(&self, raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let context = Arc::new(EvaluationContext::from_args(&raw_args));
|
let context = Arc::new(EvaluationContext::from_args(&raw_args));
|
||||||
let external_redirection = raw_args.call_info.args.external_redirection;
|
let external_redirection = raw_args.call_info.args.external_redirection;
|
||||||
let (each_args, mut input): (EachWindowArgs, _) = raw_args.process()?;
|
|
||||||
let block = Arc::new(Box::new(each_args.block));
|
|
||||||
|
|
||||||
let mut window: Vec<_> = input
|
let mut args = raw_args.evaluate_once()?;
|
||||||
|
let window_size: Tagged<usize> = args.req(0)?;
|
||||||
|
let block: CapturedBlock = args.req(1)?;
|
||||||
|
let stride: Option<Tagged<usize>> = args.get_flag("stride")?;
|
||||||
|
|
||||||
|
let block = Arc::new(Box::new(block));
|
||||||
|
|
||||||
|
let mut window: Vec<_> = args
|
||||||
|
.input
|
||||||
.by_ref()
|
.by_ref()
|
||||||
.take(*each_args.window_size - 1)
|
.take(*window_size - 1)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
// `window` must start with dummy values, which will be removed on the first iteration
|
// `window` must start with dummy values, which will be removed on the first iteration
|
||||||
let stride = each_args.stride.map(|x| *x).unwrap_or(1);
|
let stride = stride.map(|x| *x).unwrap_or(1);
|
||||||
window.insert(0, UntaggedValue::Primitive(Primitive::Nothing).into());
|
window.insert(0, UntaggedValue::Primitive(Primitive::Nothing).into());
|
||||||
|
|
||||||
Ok(input
|
Ok(args
|
||||||
|
.input
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(move |(i, input)| {
|
.map(move |(i, input)| {
|
||||||
// This would probably be more efficient if `last` was a VecDeque
|
// This would probably be more efficient if `last` was a VecDeque
|
||||||
@ -89,7 +88,8 @@ impl WholeStreamCommand for EachWindow {
|
|||||||
})
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
.flatten()
|
.flatten()
|
||||||
.to_action_stream())
|
.map(Ok)
|
||||||
|
.to_input_stream())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,17 +2,12 @@ use crate::prelude::*;
|
|||||||
use nu_engine::evaluate_baseline_expr;
|
use nu_engine::evaluate_baseline_expr;
|
||||||
use nu_engine::WholeStreamCommand;
|
use nu_engine::WholeStreamCommand;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
pub struct Format;
|
pub struct Format;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct FormatArgs {
|
|
||||||
pattern: Tagged<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WholeStreamCommand for Format {
|
impl WholeStreamCommand for Format {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"format"
|
"format"
|
||||||
@ -30,7 +25,7 @@ impl WholeStreamCommand for Format {
|
|||||||
"Format columns into a string using a simple pattern."
|
"Format columns into a string using a simple pattern."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
format_command(args)
|
format_command(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,14 +38,16 @@ impl WholeStreamCommand for Format {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_command(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn format_command(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let ctx = Arc::new(EvaluationContext::from_args(&args));
|
let ctx = Arc::new(EvaluationContext::from_args(&args));
|
||||||
let (FormatArgs { pattern }, input) = args.process()?;
|
let args = args.evaluate_once()?;
|
||||||
|
let pattern: Tagged<String> = args.req(0)?;
|
||||||
|
|
||||||
let format_pattern = format(&pattern);
|
let format_pattern = format(&pattern);
|
||||||
let commands = Arc::new(format_pattern);
|
let commands = Arc::new(format_pattern);
|
||||||
|
|
||||||
Ok(input
|
Ok(args
|
||||||
|
.input
|
||||||
.map(move |value| {
|
.map(move |value| {
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
let commands = commands.clone();
|
let commands = commands.clone();
|
||||||
@ -82,9 +79,9 @@ fn format_command(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnSuccess::value(UntaggedValue::string(output).into_untagged_value())
|
Ok(UntaggedValue::string(output).into_untagged_value())
|
||||||
})
|
})
|
||||||
.to_action_stream())
|
.to_input_stream())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -2,20 +2,12 @@ use crate::prelude::*;
|
|||||||
use nu_data::base::shape::InlineShape;
|
use nu_data::base::shape::InlineShape;
|
||||||
use nu_engine::WholeStreamCommand;
|
use nu_engine::WholeStreamCommand;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{
|
use nu_protocol::{ColumnPath, Primitive::Filesize, Signature, SyntaxShape, UntaggedValue, Value};
|
||||||
ColumnPath, Primitive::Filesize, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
|
||||||
};
|
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use nu_value_ext::get_data_by_column_path;
|
use nu_value_ext::get_data_by_column_path;
|
||||||
|
|
||||||
pub struct FileSize;
|
pub struct FileSize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Arguments {
|
|
||||||
field: ColumnPath,
|
|
||||||
format: Tagged<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WholeStreamCommand for FileSize {
|
impl WholeStreamCommand for FileSize {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"format filesize"
|
"format filesize"
|
||||||
@ -39,7 +31,7 @@ impl WholeStreamCommand for FileSize {
|
|||||||
"Converts a column of filesizes to some specified format"
|
"Converts a column of filesizes to some specified format"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
filesize(args)
|
filesize(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,50 +55,55 @@ fn process_row(
|
|||||||
input: Value,
|
input: Value,
|
||||||
format: Tagged<String>,
|
format: Tagged<String>,
|
||||||
field: Arc<ColumnPath>,
|
field: Arc<ColumnPath>,
|
||||||
) -> Result<ActionStream, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
Ok({
|
let replace_for = get_data_by_column_path(&input, &field, move |_, _, error| error);
|
||||||
let replace_for = get_data_by_column_path(&input, &field, move |_, _, error| error);
|
match replace_for {
|
||||||
match replace_for {
|
Ok(s) => {
|
||||||
Ok(s) => {
|
if let Value {
|
||||||
if let Value {
|
value: UntaggedValue::Primitive(Filesize(fs)),
|
||||||
value: UntaggedValue::Primitive(Filesize(fs)),
|
..
|
||||||
..
|
} = s
|
||||||
} = s
|
{
|
||||||
{
|
let byte_format = InlineShape::format_bytes(&fs, Some(&format.item));
|
||||||
let byte_format = InlineShape::format_bytes(&fs, Some(&format.item));
|
let byte_value = Value::from(byte_format.1);
|
||||||
let byte_value = Value::from(byte_format.1);
|
Ok(input
|
||||||
ActionStream::one(ReturnSuccess::value(
|
.replace_data_at_column_path(&field, byte_value)
|
||||||
input.replace_data_at_column_path(&field, byte_value).expect("Given that the existence check was already done, this shouldn't trigger never"),
|
.expect(
|
||||||
))
|
"Given that the existence check was already done, this shouldn't trigger never",
|
||||||
} else {
|
))
|
||||||
return Err(ShellError::labeled_error(
|
} else {
|
||||||
"the data in this row is not of the type filesize",
|
Err(ShellError::labeled_error(
|
||||||
"invalid datatype in row",
|
"the data in this row is not of the type filesize",
|
||||||
input.tag(),
|
"invalid datatype in row",
|
||||||
));
|
input.tag(),
|
||||||
}
|
))
|
||||||
}
|
}
|
||||||
Err(e) => ActionStream::one(Err(e)),
|
|
||||||
}
|
}
|
||||||
})
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filesize(raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn filesize(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
let (Arguments { field, format }, input) = raw_args.process()?;
|
let args = raw_args.evaluate_once()?;
|
||||||
|
|
||||||
|
let field: ColumnPath = args.req(0)?;
|
||||||
|
let format: Tagged<String> = args.req(1)?;
|
||||||
let field = Arc::new(field);
|
let field = Arc::new(field);
|
||||||
|
|
||||||
Ok(input
|
Ok(args
|
||||||
|
.input
|
||||||
.map(move |input| {
|
.map(move |input| {
|
||||||
let format = format.clone();
|
let format = format.clone();
|
||||||
let field = field.clone();
|
let field = field.clone();
|
||||||
|
|
||||||
match process_row(input, format, field) {
|
match process_row(input, format, field) {
|
||||||
Ok(s) => s,
|
Ok(s) => Ok(s),
|
||||||
Err(e) => ActionStream::one(Err(e)),
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
.to_action_stream())
|
.map(Ok)
|
||||||
|
.to_input_stream())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user