From d8ed01400f109a28d54f926515e00848ba29dbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Thu, 14 Jan 2021 18:55:37 -0500 Subject: [PATCH] str set sub command removal. (#2940) --- crates/nu-command/src/commands.rs | 4 +- .../src/commands/default_context.rs | 1 - crates/nu-command/src/commands/str_/mod.rs | 2 - crates/nu-command/src/commands/str_/set.rs | 115 ------------------ crates/nu-command/tests/commands/str_/mod.rs | 24 ---- 5 files changed, 2 insertions(+), 144 deletions(-) delete mode 100644 crates/nu-command/src/commands/str_/set.rs diff --git a/crates/nu-command/src/commands.rs b/crates/nu-command/src/commands.rs index 60ec47f848..00ea99b355 100644 --- a/crates/nu-command/src/commands.rs +++ b/crates/nu-command/src/commands.rs @@ -260,8 +260,8 @@ pub(crate) use split_by::SplitBy; pub(crate) use str_::{ Str, StrCamelCase, StrCapitalize, StrCollect, StrContains, StrDowncase, StrEndsWith, StrFindReplace, StrFrom, StrIndexOf, StrKebabCase, StrLPad, StrLength, StrPascalCase, StrRPad, - StrReverse, StrScreamingSnakeCase, StrSet, StrSnakeCase, StrStartsWith, StrSubstring, - StrToDatetime, StrToDecimal, StrToInteger, StrTrim, StrTrimLeft, StrTrimRight, StrUpcase, + StrReverse, StrScreamingSnakeCase, StrSnakeCase, StrStartsWith, StrSubstring, StrToDatetime, + StrToDecimal, StrToInteger, StrTrim, StrTrimLeft, StrTrimRight, StrUpcase, }; pub(crate) use table::Table; pub(crate) use tags::Tags; diff --git a/crates/nu-command/src/commands/default_context.rs b/crates/nu-command/src/commands/default_context.rs index be14b6a3d7..de81993daa 100644 --- a/crates/nu-command/src/commands/default_context.rs +++ b/crates/nu-command/src/commands/default_context.rs @@ -91,7 +91,6 @@ pub fn create_default_context(interactive: bool) -> Result, - rest: Vec, -} - -pub struct SubCommand; - -#[async_trait] -impl WholeStreamCommand for SubCommand { - fn name(&self) -> &str { - "str set" - } - - fn signature(&self) -> Signature { - Signature::build("str set") - .required("set", SyntaxShape::String, "the new string to set") - .rest( - SyntaxShape::ColumnPath, - "optionally set text by column paths", - ) - } - - fn usage(&self) -> &str { - "sets text" - } - - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await - } - - fn examples(&self) -> Vec { - vec![ - Example { - description: "Set contents with preferred string", - example: "echo 'good day' | str set 'good bye'", - result: Some(vec![Value::from("good bye")]), - }, - Example { - description: "Set the contents on preferred column paths", - example: "open Cargo.toml | str set '255' package.version", - result: None, - }, - ] - } -} - -#[derive(Clone)] -struct Replace(String); - -async fn operate(args: CommandArgs) -> Result { - let (Arguments { replace, rest }, input) = args.process().await?; - let options = Replace(replace.item); - - let column_paths: Vec<_> = rest; - - Ok(input - .map(move |v| { - if column_paths.is_empty() { - ReturnSuccess::value(action(&v, &options, v.tag())?) - } else { - let mut ret = v; - - for path in &column_paths { - let options = options.clone(); - - ret = ret.swap_data_by_column_path( - path, - Box::new(move |old| action(old, &options, old.tag())), - )?; - } - - ReturnSuccess::value(ret) - } - }) - .to_output_stream()) -} - -fn action(_input: &Value, options: &Replace, tag: impl Into) -> Result { - let replacement = &options.0; - Ok(UntaggedValue::string(replacement.as_str()).into_value(tag)) -} - -#[cfg(test)] -mod tests { - use super::ShellError; - use super::{action, Replace, SubCommand}; - use nu_source::Tag; - use nu_test_support::value::string; - - #[test] - fn examples_work_as_expected() -> Result<(), ShellError> { - use crate::examples::test as test_examples; - - Ok(test_examples(SubCommand {})?) - } - - #[test] - fn sets() { - let word = string("andres"); - let expected = string("robalino"); - - let set_options = Replace(String::from("robalino")); - - let actual = action(&word, &set_options, Tag::unknown()).unwrap(); - assert_eq!(actual, expected); - } -} diff --git a/crates/nu-command/tests/commands/str_/mod.rs b/crates/nu-command/tests/commands/str_/mod.rs index 37c037bef3..1899fa283e 100644 --- a/crates/nu-command/tests/commands/str_/mod.rs +++ b/crates/nu-command/tests/commands/str_/mod.rs @@ -150,30 +150,6 @@ fn converts_to_decimal() { assert_eq!(actual.out, "3.1415"); } -#[test] -fn sets() { - Playground::setup("str_test_5", |dirs, sandbox| { - sandbox.with_files(vec![FileWithContent( - "sample.toml", - r#" - [package] - name = "nushell" - "#, - )]); - - let actual = nu!( - cwd: dirs.test(), pipeline( - r#" - open sample.toml - | str set wykittenshell package.name - | get package.name - "# - )); - - assert_eq!(actual.out, "wykittenshell"); - }) -} - #[test] fn find_and_replaces() { Playground::setup("str_test_6", |dirs, sandbox| {