Engine-p style in compact (#3325)

* Engine-p style in compact

* Remove unused import

* Use filter in compact, thanks to clippy for spotting it :]
This commit is contained in:
Leonhard Kipp 2021-04-18 20:40:29 +02:00 committed by GitHub
parent d8e105fe34
commit 033df9457b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,14 +2,13 @@ use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Compact; pub struct Compact;
#[derive(Deserialize)]
pub struct CompactArgs { pub struct CompactArgs {
rest: Vec<Tagged<String>>, columns: Vec<Tagged<String>>,
} }
impl WholeStreamCommand for Compact { impl WholeStreamCommand for Compact {
@ -25,7 +24,7 @@ impl WholeStreamCommand for Compact {
"Creates a table with non-empty rows." "Creates a table with non-empty rows."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
compact(args) compact(args)
} }
@ -38,36 +37,30 @@ impl WholeStreamCommand for Compact {
} }
} }
pub fn compact(args: CommandArgs) -> Result<ActionStream, ShellError> { pub fn compact(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (CompactArgs { rest: columns }, input) = args.process()?; let (args, input) = args.extract(|params| {
Ok(CompactArgs {
columns: params.rest(0)?,
})
})?;
Ok(input Ok(input
.filter_map(move |item| { .filter(move |item| {
if columns.is_empty() { if args.columns.is_empty() {
if !item.is_empty() { !item.is_empty()
Some(ReturnSuccess::value(item)) } else if let Value {
} else { value: UntaggedValue::Row(ref r),
None ..
} } = item
{
args.columns
.iter()
.all(|field| r.get_data(field).borrow().is_some())
} else { } else {
match item { false
Value {
value: UntaggedValue::Row(ref r),
..
} => {
if columns
.iter()
.all(|field| r.get_data(field).borrow().is_some())
{
Some(ReturnSuccess::value(item))
} else {
None
}
}
_ => None,
}
} }
}) })
.to_action_stream()) .to_output_stream())
} }
#[cfg(test)] #[cfg(test)]