From 033df9457b32f2e52447e5068cc8e13f577eabd5 Mon Sep 17 00:00:00 2001 From: Leonhard Kipp Date: Sun, 18 Apr 2021 20:40:29 +0200 Subject: [PATCH] Engine-p style in compact (#3325) * Engine-p style in compact * Remove unused import * Use filter in compact, thanks to clippy for spotting it :] --- crates/nu-command/src/commands/compact.rs | 53 ++++++++++------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/crates/nu-command/src/commands/compact.rs b/crates/nu-command/src/commands/compact.rs index 5988cdf29..0b17683a0 100644 --- a/crates/nu-command/src/commands/compact.rs +++ b/crates/nu-command/src/commands/compact.rs @@ -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>, + columns: Vec>, } 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 { + fn run(&self, args: CommandArgs) -> Result { compact(args) } @@ -38,36 +37,30 @@ impl WholeStreamCommand for Compact { } } -pub fn compact(args: CommandArgs) -> Result { - let (CompactArgs { rest: columns }, input) = args.process()?; +pub fn compact(args: CommandArgs) -> Result { + 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)]