From a5b6bb6209cca950fc93585e59a83cd927bfc01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20V=C3=ADt?= Date: Sun, 20 Sep 2020 11:04:26 +0200 Subject: [PATCH] Add global mode to str trim (#2576) * Add global mode to str trim The global mode allows skipping non-string values, and processes rows and tables as well * Add tests to action with ActionMode::Global --- crates/nu-cli/src/commands/str_/trim/mod.rs | 65 ++++++++++++++++--- .../src/commands/str_/trim/trim_both_ends.rs | 47 ++++++++++++-- .../src/commands/str_/trim/trim_left.rs | 62 ++++++++++++++++-- .../src/commands/str_/trim/trim_right.rs | 61 +++++++++++++++-- crates/nu-plugin/src/test_helpers.rs | 4 +- 5 files changed, 215 insertions(+), 24 deletions(-) diff --git a/crates/nu-cli/src/commands/str_/trim/mod.rs b/crates/nu-cli/src/commands/str_/trim/mod.rs index 78da9f055e..b294cca9e0 100644 --- a/crates/nu-cli/src/commands/str_/trim/mod.rs +++ b/crates/nu-cli/src/commands/str_/trim/mod.rs @@ -8,6 +8,7 @@ use nu_protocol::ShellTypeName; use nu_protocol::{ColumnPath, Primitive, ReturnSuccess, UntaggedValue, Value}; use nu_source::{Tag, Tagged}; use nu_value_ext::ValueExt; +use std::iter::FromIterator; pub use trim_both_ends::SubCommand as Trim; pub use trim_left::SubCommand as TrimLeft; @@ -38,14 +39,22 @@ where Ok(input .map(move |v| { if column_paths.is_empty() { - ReturnSuccess::value(action(&v, v.tag(), to_trim, &trim_operation)?) + ReturnSuccess::value(action( + &v, + v.tag(), + to_trim, + &trim_operation, + ActionMode::Global, + )?) } else { let mut ret = v; for path in &column_paths { ret = ret.swap_data_by_column_path( path, - Box::new(move |old| action(old, old.tag(), to_trim, &trim_operation)), + Box::new(move |old| { + action(old, old.tag(), to_trim, &trim_operation, ActionMode::Local) + }), )?; } @@ -55,27 +64,63 @@ where .to_output_stream()) } +#[derive(Debug, Copy, Clone)] +pub enum ActionMode { + Local, + Global, +} + pub fn action( input: &Value, tag: impl Into, char_: Option, trim_operation: &F, + mode: ActionMode, ) -> Result where F: Fn(&str, Option) -> String + Send + Sync + 'static, { + let tag = tag.into(); match &input.value { UntaggedValue::Primitive(Primitive::Line(s)) | UntaggedValue::Primitive(Primitive::String(s)) => { Ok(UntaggedValue::string(trim_operation(s, char_)).into_value(tag)) } - other => { - let got = format!("got {}", other.type_name()); - Err(ShellError::labeled_error( - "value is not string", - got, - tag.into().span, - )) - } + other => match mode { + ActionMode::Global => match other { + UntaggedValue::Row(dictionary) => { + let results: Result, ShellError> = dictionary + .entries() + .iter() + .map(|(k, v)| -> Result<_, ShellError> { + Ok(( + k.clone(), + action(&v, tag.clone(), char_, trim_operation, mode)?, + )) + }) + .collect(); + let indexmap = IndexMap::from_iter(results?); + Ok(UntaggedValue::Row(indexmap.into()).into_value(tag)) + } + UntaggedValue::Table(values) => { + let values: Result, ShellError> = values + .iter() + .map(|v| -> Result<_, ShellError> { + Ok(action(v, tag.clone(), char_, trim_operation, mode)?) + }) + .collect(); + Ok(UntaggedValue::Table(values?).into_value(tag)) + } + _ => Ok(input.clone()), + }, + ActionMode::Local => { + let got = format!("got {}", other.type_name()); + Err(ShellError::labeled_error( + "value is not string", + got, + tag.span, + )) + } + }, } } diff --git a/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs b/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs index 8eaf200fc5..2c099f136d 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs @@ -63,8 +63,11 @@ fn trim(s: &str, char_: Option) -> String { #[cfg(test)] mod tests { use super::{trim, SubCommand}; - use crate::commands::str_::trim::action; - use nu_plugin::test_helpers::value::string; + use crate::commands::str_::trim::{action, ActionMode}; + use nu_plugin::{ + row, + test_helpers::value::{int, string, table}, + }; use nu_source::Tag; #[test] @@ -79,7 +82,43 @@ mod tests { let word = string("andres "); let expected = string("andres"); - let actual = action(&word, Tag::unknown(), None, &trim).unwrap(); + let actual = action(&word, Tag::unknown(), None, &trim, ActionMode::Local).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn trims_global() { + let word = string(" global "); + let expected = string("global"); + + let actual = action(&word, Tag::unknown(), None, &trim, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_ignores_numbers() { + let number = int(2020); + let expected = int(2020); + + let actual = action(&number, Tag::unknown(), None, &trim, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_row() { + let row = row!["a".to_string() => string(" c "), " b ".to_string() => string(" d ")]; + let expected = row!["a".to_string() => string("c"), " b ".to_string() => string("d")]; + + let actual = action(&row, Tag::unknown(), None, &trim, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_table() { + let row = table(&[string(" a "), int(65), string(" d")]); + let expected = table(&[string("a"), int(65), string("d")]); + + let actual = action(&row, Tag::unknown(), None, &trim, ActionMode::Global).unwrap(); assert_eq!(actual, expected); } @@ -88,7 +127,7 @@ mod tests { let word = string("!#andres#!"); let expected = string("#andres#"); - let actual = action(&word, Tag::unknown(), Some('!'), &trim).unwrap(); + let actual = action(&word, Tag::unknown(), Some('!'), &trim, ActionMode::Local).unwrap(); assert_eq!(actual, expected); } } diff --git a/crates/nu-cli/src/commands/str_/trim/trim_left.rs b/crates/nu-cli/src/commands/str_/trim/trim_left.rs index ae54b1e063..29400f58e8 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_left.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_left.rs @@ -64,8 +64,11 @@ fn trim_left(s: &str, char_: Option) -> String { #[cfg(test)] mod tests { use super::{trim_left, SubCommand}; - use crate::commands::str_::trim::action; - use nu_plugin::test_helpers::value::string; + use crate::commands::str_::trim::{action, ActionMode}; + use nu_plugin::{ + row, + test_helpers::value::{int, string, table}, + }; use nu_source::Tag; #[test] @@ -80,15 +83,66 @@ mod tests { let word = string(" andres "); let expected = string("andres "); - let actual = action(&word, Tag::unknown(), None, &trim_left).unwrap(); + let actual = action(&word, Tag::unknown(), None, &trim_left, ActionMode::Local).unwrap(); assert_eq!(actual, expected); } + + #[test] + fn trims_left_global() { + let word = string(" global "); + let expected = string("global "); + + let actual = action(&word, Tag::unknown(), None, &trim_left, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_left_ignores_numbers() { + let number = int(2020); + let expected = int(2020); + + let actual = action( + &number, + Tag::unknown(), + None, + &trim_left, + ActionMode::Global, + ) + .unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_left_row() { + let row = row!["a".to_string() => string(" c "), " b ".to_string() => string(" d ")]; + let expected = row!["a".to_string() => string("c "), " b ".to_string() => string("d ")]; + + let actual = action(&row, Tag::unknown(), None, &trim_left, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_left_table() { + let row = table(&[string(" a "), int(65), string(" d")]); + let expected = table(&[string("a "), int(65), string("d")]); + + let actual = action(&row, Tag::unknown(), None, &trim_left, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + #[test] fn trims_custom_chars_from_left() { let word = string("!!! andres !!!"); let expected = string(" andres !!!"); - let actual = action(&word, Tag::unknown(), Some('!'), &trim_left).unwrap(); + let actual = action( + &word, + Tag::unknown(), + Some('!'), + &trim_left, + ActionMode::Local, + ) + .unwrap(); assert_eq!(actual, expected); } } diff --git a/crates/nu-cli/src/commands/str_/trim/trim_right.rs b/crates/nu-cli/src/commands/str_/trim/trim_right.rs index bc874ec8c3..203562f02f 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_right.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_right.rs @@ -64,8 +64,11 @@ fn trim_right(s: &str, char_: Option) -> String { #[cfg(test)] mod tests { use super::{trim_right, SubCommand}; - use crate::commands::str_::trim::action; - use nu_plugin::test_helpers::value::string; + use crate::commands::str_::trim::{action, ActionMode}; + use nu_plugin::{ + row, + test_helpers::value::{int, string, table}, + }; use nu_source::Tag; #[test] @@ -80,7 +83,50 @@ mod tests { let word = string(" andres "); let expected = string(" andres"); - let actual = action(&word, Tag::unknown(), None, &trim_right).unwrap(); + let actual = action(&word, Tag::unknown(), None, &trim_right, ActionMode::Local).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn trims_right_global() { + let word = string(" global "); + let expected = string(" global"); + + let actual = action(&word, Tag::unknown(), None, &trim_right, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_right_ignores_numbers() { + let number = int(2020); + let expected = int(2020); + + let actual = action( + &number, + Tag::unknown(), + None, + &trim_right, + ActionMode::Global, + ) + .unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_row() { + let row = row!["a".to_string() => string(" c "), " b ".to_string() => string(" d ")]; + let expected = row!["a".to_string() => string(" c"), " b ".to_string() => string(" d")]; + + let actual = action(&row, Tag::unknown(), None, &trim_right, ActionMode::Global).unwrap(); + assert_eq!(actual, expected); + } + + #[test] + fn global_trim_table() { + let row = table(&[string(" a "), int(65), string(" d")]); + let expected = table(&[string(" a"), int(65), string(" d")]); + + let actual = action(&row, Tag::unknown(), None, &trim_right, ActionMode::Global).unwrap(); assert_eq!(actual, expected); } @@ -89,7 +135,14 @@ mod tests { let word = string("#@! andres !@#"); let expected = string("#@! andres !@"); - let actual = action(&word, Tag::unknown(), Some('#'), &trim_right).unwrap(); + let actual = action( + &word, + Tag::unknown(), + Some('#'), + &trim_right, + ActionMode::Local, + ) + .unwrap(); assert_eq!(actual, expected); } } diff --git a/crates/nu-plugin/src/test_helpers.rs b/crates/nu-plugin/src/test_helpers.rs index 42e6208f7c..d80b4cbdd1 100644 --- a/crates/nu-plugin/src/test_helpers.rs +++ b/crates/nu-plugin/src/test_helpers.rs @@ -206,9 +206,9 @@ pub mod value { #[macro_export] macro_rules! row { ($( $key: expr => $val: expr ),*) => {{ - let mut map = indexmap::IndexMap::new(); + let mut map = ::indexmap::IndexMap::new(); $( map.insert($key, $val); )* - UntaggedValue::row(map).into_untagged_value() + ::nu_protocol::UntaggedValue::row(map).into_untagged_value() }} } }