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_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
pub struct Compact;
#[derive(Deserialize)]
pub struct CompactArgs {
rest: Vec<Tagged<String>>,
columns: Vec<Tagged<String>>,
}
impl WholeStreamCommand for Compact {
@ -25,7 +24,7 @@ impl WholeStreamCommand for Compact {
"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)
}
@ -38,36 +37,30 @@ impl WholeStreamCommand for Compact {
}
}
pub fn compact(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (CompactArgs { rest: columns }, input) = args.process()?;
pub fn compact(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (args, input) = args.extract(|params| {
Ok(CompactArgs {
columns: params.rest(0)?,
})
})?;
Ok(input
.filter_map(move |item| {
if columns.is_empty() {
if !item.is_empty() {
Some(ReturnSuccess::value(item))
} else {
None
}
.filter(move |item| {
if args.columns.is_empty() {
!item.is_empty()
} else if let Value {
value: UntaggedValue::Row(ref r),
..
} = item
{
args.columns
.iter()
.all(|field| r.get_data(field).borrow().is_some())
} else {
match item {
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,
}
false
}
})
.to_action_stream())
.to_output_stream())
}
#[cfg(test)]