Reduced LOC by replacing several instances of Value::Int {}, Value::Float{}, Value::Bool {}, and Value::String {} with Value::int(), Value::float(), Value::boolean() and Value::string() (#7412)

# Description

While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
  val: a,
  span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.

# User-Facing Changes

None.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Leon
2022-12-10 02:37:51 +10:00
committed by GitHub
parent b56ad92e25
commit 220b105efb
139 changed files with 540 additions and 2141 deletions

View File

@ -55,26 +55,11 @@ impl Command for Size {
"graphemes".into(),
],
vals: vec![
Value::Int {
val: 1,
span: Span::test_data(),
},
Value::Int {
val: 7,
span: Span::test_data(),
},
Value::Int {
val: 38,
span: Span::test_data(),
},
Value::Int {
val: 38,
span: Span::test_data(),
},
Value::Int {
val: 38,
span: Span::test_data(),
},
Value::int(1, Span::test_data()),
Value::int(7, Span::test_data()),
Value::int(38, Span::test_data()),
Value::int(38, Span::test_data()),
Value::int(38, Span::test_data()),
],
span: Span::test_data(),
}),
@ -91,26 +76,11 @@ impl Command for Size {
"graphemes".into(),
],
vals: vec![
Value::Int {
val: 1,
span: Span::test_data(),
},
Value::Int {
val: 6,
span: Span::test_data(),
},
Value::Int {
val: 18,
span: Span::test_data(),
},
Value::Int {
val: 6,
span: Span::test_data(),
},
Value::Int {
val: 6,
span: Span::test_data(),
},
Value::int(1, Span::test_data()),
Value::int(6, Span::test_data()),
Value::int(18, Span::test_data()),
Value::int(6, Span::test_data()),
Value::int(6, Span::test_data()),
],
span: Span::test_data(),
}),
@ -127,26 +97,11 @@ impl Command for Size {
"graphemes".into(),
],
vals: vec![
Value::Int {
val: 1,
span: Span::test_data(),
},
Value::Int {
val: 2,
span: Span::test_data(),
},
Value::Int {
val: 15,
span: Span::test_data(),
},
Value::Int {
val: 14,
span: Span::test_data(),
},
Value::Int {
val: 13,
span: Span::test_data(),
},
Value::int(1, Span::test_data()),
Value::int(2, Span::test_data()),
Value::int(15, Span::test_data()),
Value::int(14, Span::test_data()),
Value::int(13, Span::test_data()),
],
span: Span::test_data(),
}),

View File

@ -50,26 +50,17 @@ impl Command for SubCommand {
Example {
description: "convert a string to camelCase",
example: " 'NuShell' | str camel-case",
result: Some(Value::String {
val: "nuShell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nuShell", Span::test_data())),
},
Example {
description: "convert a string to camelCase",
example: "'this-is-the-first-case' | str camel-case",
result: Some(Value::String {
val: "thisIsTheFirstCase".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("thisIsTheFirstCase", Span::test_data())),
},
Example {
description: "convert a string to camelCase",
example: " 'this_is_the_second_case' | str camel-case",
result: Some(Value::String {
val: "thisIsTheSecondCase".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("thisIsTheSecondCase", Span::test_data())),
},
Example {
description: "convert a column from a table to camelCase",
@ -79,10 +70,7 @@ impl Command for SubCommand {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![
Value::String {
val: "nuTest".to_string(),
span: Span::test_data(),
},
Value::string("nuTest", Span::test_data()),
Value::test_int(100),
],
}],

View File

@ -48,18 +48,12 @@ impl Command for SubCommand {
Example {
description: "Capitalize contents",
example: "'good day' | str capitalize",
result: Some(Value::String {
val: "Good day".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("Good day", Span::test_data())),
},
Example {
description: "Capitalize contents",
example: "'anton' | str capitalize",
result: Some(Value::String {
val: "Anton".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("Anton", Span::test_data())),
},
Example {
description: "Capitalize a column in a table",
@ -69,10 +63,7 @@ impl Command for SubCommand {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![
Value::String {
val: "Nu_test".to_string(),
span: Span::test_data(),
},
Value::string("Nu_test", Span::test_data()),
Value::test_int(100),
],
}],

View File

@ -48,18 +48,12 @@ impl Command for SubCommand {
Example {
description: "Downcase contents",
example: "'NU' | str downcase",
result: Some(Value::String {
val: "nu".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nu", Span::test_data())),
},
Example {
description: "Downcase contents",
example: "'TESTa' | str downcase",
result: Some(Value::String {
val: "testa".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("testa", Span::test_data())),
},
Example {
description: "Downcase contents",
@ -68,14 +62,8 @@ impl Command for SubCommand {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::String {
val: "test".to_string(),
span: Span::test_data(),
},
Value::String {
val: "ABC".to_string(),
span: Span::test_data(),
},
Value::string("test", Span::test_data()),
Value::string("ABC", Span::test_data()),
],
span: Span::test_data(),
}],
@ -89,14 +77,8 @@ impl Command for SubCommand {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::String {
val: "test".to_string(),
span: Span::test_data(),
},
Value::String {
val: "abc".to_string(),
span: Span::test_data(),
},
Value::string("test", Span::test_data()),
Value::string("abc", Span::test_data()),
],
span: Span::test_data(),
}],

View File

@ -50,26 +50,17 @@ impl Command for SubCommand {
Example {
description: "convert a string to kebab-case",
example: "'NuShell' | str kebab-case",
result: Some(Value::String {
val: "nu-shell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nu-shell", Span::test_data())),
},
Example {
description: "convert a string to kebab-case",
example: "'thisIsTheFirstCase' | str kebab-case",
result: Some(Value::String {
val: "this-is-the-first-case".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("this-is-the-first-case", Span::test_data())),
},
Example {
description: "convert a string to kebab-case",
example: "'THIS_IS_THE_SECOND_CASE' | str kebab-case",
result: Some(Value::String {
val: "this-is-the-second-case".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("this-is-the-second-case", Span::test_data())),
},
Example {
description: "convert a column from a table to kebab-case",
@ -79,10 +70,7 @@ impl Command for SubCommand {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![
Value::String {
val: "nu-test".to_string(),
span: Span::test_data(),
},
Value::string("nu-test", Span::test_data()),
Value::test_int(100),
],
}],

View File

@ -50,26 +50,17 @@ impl Command for SubCommand {
Example {
description: "convert a string to PascalCase",
example: "'nu-shell' | str pascal-case",
result: Some(Value::String {
val: "NuShell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("NuShell", Span::test_data())),
},
Example {
description: "convert a string to PascalCase",
example: "'this-is-the-first-case' | str pascal-case",
result: Some(Value::String {
val: "ThisIsTheFirstCase".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("ThisIsTheFirstCase", Span::test_data())),
},
Example {
description: "convert a string to PascalCase",
example: "'this_is_the_second_case' | str pascal-case",
result: Some(Value::String {
val: "ThisIsTheSecondCase".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("ThisIsTheSecondCase", Span::test_data())),
},
Example {
description: "convert a column from a table to PascalCase",
@ -79,10 +70,7 @@ impl Command for SubCommand {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![
Value::String {
val: "NuTest".to_string(),
span: Span::test_data(),
},
Value::string("NuTest", Span::test_data()),
Value::test_int(100),
],
}],

View File

@ -49,26 +49,17 @@ impl Command for SubCommand {
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "NuShell" | str screaming-snake-case"#,
result: Some(Value::String {
val: "NU_SHELL".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("NU_SHELL", Span::test_data())),
},
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "this_is_the_second_case" | str screaming-snake-case"#,
result: Some(Value::String {
val: "THIS_IS_THE_SECOND_CASE".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("THIS_IS_THE_SECOND_CASE", Span::test_data())),
},
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#""this-is-the-first-case" | str screaming-snake-case"#,
result: Some(Value::String {
val: "THIS_IS_THE_FIRST_CASE".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("THIS_IS_THE_FIRST_CASE", Span::test_data())),
},
Example {
description: "convert a column from a table to SCREAMING_SNAKE_CASE",
@ -78,10 +69,7 @@ impl Command for SubCommand {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![
Value::String {
val: "NU_TEST".to_string(),
span: Span::test_data(),
},
Value::string("NU_TEST", Span::test_data()),
Value::test_int(100),
],
}],

View File

@ -49,26 +49,17 @@ impl Command for SubCommand {
Example {
description: "convert a string to snake_case",
example: r#" "NuShell" | str snake-case"#,
result: Some(Value::String {
val: "nu_shell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nu_shell", Span::test_data())),
},
Example {
description: "convert a string to snake_case",
example: r#" "this_is_the_second_case" | str snake-case"#,
result: Some(Value::String {
val: "this_is_the_second_case".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("this_is_the_second_case", Span::test_data())),
},
Example {
description: "convert a string to snake_case",
example: r#""this-is-the-first-case" | str snake-case"#,
result: Some(Value::String {
val: "this_is_the_first_case".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("this_is_the_first_case", Span::test_data())),
},
Example {
description: "convert a column from a table to snake_case",
@ -78,10 +69,7 @@ impl Command for SubCommand {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![
Value::String {
val: "nu_test".to_string(),
span: Span::test_data(),
},
Value::string("nu_test", Span::test_data()),
Value::test_int(100),
],
}],

View File

@ -50,18 +50,12 @@ impl Command for SubCommand {
Example {
description: "convert a string to Title Case",
example: "'nu-shell' | str title-case",
result: Some(Value::String {
val: "Nu Shell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("Nu Shell", Span::test_data())),
},
Example {
description: "convert a string to Title Case",
example: "'this is a test case' | str title-case",
result: Some(Value::String {
val: "This Is A Test Case".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("This Is A Test Case", Span::test_data())),
},
Example {
description: "convert a column from a table to Title Case",
@ -71,10 +65,7 @@ impl Command for SubCommand {
span: Span::test_data(),
cols: vec!["title".to_string(), "count".to_string()],
vals: vec![
Value::String {
val: "Nu Test".to_string(),
span: Span::test_data(),
},
Value::string("Nu Test", Span::test_data()),
Value::test_int(100),
],
}],

View File

@ -74,18 +74,12 @@ impl Command for StrCollect {
Example {
description: "Create a string from input",
example: "['nu', 'shell'] | str collect",
result: Some(Value::String {
val: "nushell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nushell", Span::test_data())),
},
Example {
description: "Create a string from input with a separator",
example: "['nu', 'shell'] | str collect '-'",
result: Some(Value::String {
val: "nu-shell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nu-shell", Span::test_data())),
},
]
}

View File

@ -73,18 +73,12 @@ impl Command for SubCommand {
Example {
description: "Check if input contains string",
example: "'my_library.rb' | str contains '.rb'",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Check if input contains string case insensitive",
example: "'my_library.rb' | str contains -i '.RB'",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Check if input contains string in a table",
@ -93,10 +87,7 @@ impl Command for SubCommand {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::Bool {
val: true,
span: Span::test_data(),
},
Value::boolean(true, Span::test_data()),
Value::test_int(100),
],
span: Span::test_data(),
@ -111,10 +102,7 @@ impl Command for SubCommand {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::Bool {
val: true,
span: Span::test_data(),
},
Value::boolean(true, Span::test_data()),
Value::test_int(100),
],
span: Span::test_data(),
@ -129,14 +117,8 @@ impl Command for SubCommand {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::Bool {
val: true,
span: Span::test_data(),
},
Value::Bool {
val: true,
span: Span::test_data(),
},
Value::boolean(true, Span::test_data()),
Value::boolean(true, Span::test_data()),
],
span: Span::test_data(),
}],
@ -146,28 +128,16 @@ impl Command for SubCommand {
Example {
description: "Check if input string contains 'banana'",
example: "'hello' | str contains 'banana'",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
Example {
description: "Check if list contains string",
example: "[one two three] | str contains o",
result: Some(Value::List {
vals: vec![
Value::Bool {
val: true,
span: Span::test_data(),
},
Value::Bool {
val: true,
span: Span::test_data(),
},
Value::Bool {
val: false,
span: Span::test_data(),
},
Value::boolean(true, Span::test_data()),
Value::boolean(true, Span::test_data()),
Value::boolean(false, Span::test_data()),
],
span: Span::test_data(),
}),
@ -177,18 +147,9 @@ impl Command for SubCommand {
example: "[one two three] | str contains -n o",
result: Some(Value::List {
vals: vec![
Value::Bool {
val: false,
span: Span::test_data(),
},
Value::Bool {
val: false,
span: Span::test_data(),
},
Value::Bool {
val: true,
span: Span::test_data(),
},
Value::boolean(false, Span::test_data()),
Value::boolean(false, Span::test_data()),
Value::boolean(true, Span::test_data()),
],
span: Span::test_data(),
}),
@ -208,8 +169,8 @@ fn action(
head: Span,
) -> Value {
match input {
Value::String { val, .. } => Value::Bool {
val: match case_insensitive {
Value::String { val, .. } => Value::boolean(
match case_insensitive {
true => {
if *not_contain {
!val.to_lowercase()
@ -227,8 +188,8 @@ fn action(
}
}
},
span: head,
},
head,
),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(

View File

@ -95,10 +95,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
match &input {
Value::String { val, .. } => {
let distance = levenshtein_distance(val, compare_string);
Value::Int {
val: distance as i64,
span: head,
}
Value::int(distance as i64, head)
}
other => Value::Error {
error: ShellError::UnsupportedInput(

View File

@ -67,18 +67,12 @@ impl Command for SubCommand {
Example {
description: "Checks if string ends with '.rb'",
example: "'my_library.rb' | str ends-with '.rb'",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if string ends with '.txt'",
example: "'my_library.rb' | str ends-with '.txt'",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
@ -86,10 +80,7 @@ impl Command for SubCommand {
fn action(input: &Value, args: &Arguments, head: Span) -> Value {
match input {
Value::String { val, .. } => Value::Bool {
val: val.ends_with(&args.substring),
span: head,
},
Value::String { val, .. } => Value::boolean(val.ends_with(&args.substring), head),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(

View File

@ -126,10 +126,7 @@ fn action(
) -> Value {
let range = match range {
Some(range) => range.clone(),
None => Value::String {
val: "".to_string(),
span: head,
},
None => Value::string("", head),
};
let r = process_range(input, &range, head);
@ -143,26 +140,14 @@ fn action(
if *end {
if let Some(result) = s[start_index..end_index].rfind(&**substring) {
Value::Int {
val: result as i64 + start_index as i64,
span: head,
}
Value::int(result as i64 + start_index as i64, head)
} else {
Value::Int {
val: -1,
span: head,
}
Value::int(-1, head)
}
} else if let Some(result) = s[start_index..end_index].find(&**substring) {
Value::Int {
val: result as i64 + start_index as i64,
span: head,
}
Value::int(result as i64 + start_index as i64, head)
} else {
Value::Int {
val: -1,
span: head,
}
Value::int(-1, head)
}
}
other => Value::Error {

View File

@ -78,18 +78,12 @@ impl Command for StrJoin {
Example {
description: "Create a string from input",
example: "['nu', 'shell'] | str join",
result: Some(Value::String {
val: "nushell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nushell", Span::test_data())),
},
Example {
description: "Create a string from input with a separator",
example: "['nu', 'shell'] | str join '-'",
result: Some(Value::String {
val: "nu-shell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nu-shell", Span::test_data())),
},
]
}

View File

@ -66,10 +66,7 @@ impl Command for SubCommand {
fn action(input: &Value, _arg: &CellPathOnlyArgs, head: Span) -> Value {
match input {
Value::String { val, .. } => Value::Int {
val: val.len() as i64,
span: head,
},
Value::String { val, .. } => Value::int(val.len() as i64, head),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(

View File

@ -82,34 +82,22 @@ impl Command for SubCommand {
Example {
description: "Left-pad a string with asterisks until it's 10 characters wide",
example: "'nushell' | str lpad -l 10 -c '*'",
result: Some(Value::String {
val: "***nushell".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("***nushell", Span::test_data())),
},
Example {
description: "Left-pad a string with zeroes until it's 10 character wide",
example: "'123' | str lpad -l 10 -c '0'",
result: Some(Value::String {
val: "0000000123".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("0000000123", Span::test_data())),
},
Example {
description: "Use lpad to truncate a string to its last three characters",
example: "'123456789' | str lpad -l 3 -c '0'",
result: Some(Value::String {
val: "789".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("789", Span::test_data())),
},
Example {
description: "Use lpad to pad Unicode",
example: "'▉' | str lpad -l 10 -c '▉'",
result: Some(Value::String {
val: "▉▉▉▉▉▉▉▉▉▉".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("▉▉▉▉▉▉▉▉▉▉", Span::test_data())),
},
]
}

View File

@ -94,18 +94,12 @@ impl Command for SubCommand {
Example {
description: "Find and replace contents with capture group",
example: "'my_library.rb' | str replace '(.+).rb' '$1.nu'",
result: Some(Value::String {
val: "my_library.nu".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("my_library.nu", Span::test_data())),
},
Example {
description: "Find and replace all occurrences of find string",
example: "'abc abc abc' | str replace -a 'b' 'z'",
result: Some(Value::String {
val: "azc azc azc".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("azc azc azc", Span::test_data())),
},
Example {
description: "Find and replace all occurrences of find string in table",
@ -115,18 +109,9 @@ impl Command for SubCommand {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::String {
val: "azc".to_string(),
span: Span::test_data(),
},
Value::String {
val: "abc".to_string(),
span: Span::test_data(),
},
Value::String {
val: "ads".to_string(),
span: Span::test_data(),
},
Value::string("azc", Span::test_data()),
Value::string("abc", Span::test_data()),
Value::string("ads", Span::test_data()),
],
span: Span::test_data(),
}],
@ -136,42 +121,27 @@ impl Command for SubCommand {
Example {
description: "Find and replace contents without using the replace parameter as a regular expression",
example: r#"'dogs_$1_cats' | str replace '\$1' '$2' -n"#,
result: Some(Value::String {
val: "dogs_$2_cats".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("dogs_$2_cats", Span::test_data())),
},
Example {
description: "Find and replace the first occurence using string replacement *not* regular expressions",
example: r#"'c:\some\cool\path' | str replace 'c:\some\cool' '~' -s"#,
result: Some(Value::String {
val: "~\\path".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("~\\path", Span::test_data())),
},
Example {
description: "Find and replace all occurences using string replacement *not* regular expressions",
example: r#"'abc abc abc' | str replace -a 'b' 'z' -s"#,
result: Some(Value::String {
val: "azc azc azc".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("azc azc azc", Span::test_data())),
},
Example {
description: "Find and replace with fancy-regex",
example: r#"'a sucessful b' | str replace '\b([sS])uc(?:cs|s?)e(ed(?:ed|ing|s?)|ss(?:es|ful(?:ly)?|i(?:ons?|ve(?:ly)?)|ors?)?)\b' '${1}ucce$2'"#,
result: Some(Value::String {
val: "a successful b".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("a successful b", Span::test_data())),
},
Example {
description: "Find and replace with fancy-regex",
example: r#"'GHIKK-9+*' | str replace '[*[:xdigit:]+]' 'z'"#,
result: Some(Value::String {
val: "GHIKK-z+*".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("GHIKK-z+*", Span::test_data())),
},
]
@ -277,10 +247,7 @@ mod tests {
#[test]
fn can_have_capture_groups() {
let word = Value::String {
val: "Cargo.toml".to_string(),
span: Span::test_data(),
};
let word = Value::string("Cargo.toml", Span::test_data());
let options = Arguments {
find: test_spanned_string("Cargo.(.+)"),
@ -292,12 +259,6 @@ mod tests {
};
let actual = action(&word, &options, Span::test_data());
assert_eq!(
actual,
Value::String {
val: "Carga.toml".to_string(),
span: Span::test_data()
}
);
assert_eq!(actual, Value::string("Carga.toml", Span::test_data()));
}
}

View File

@ -51,28 +51,16 @@ impl Command for SubCommand {
Example {
description: "Reverse a single string",
example: "'Nushell' | str reverse",
result: Some(Value::String {
val: "llehsuN".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("llehsuN", Span::test_data())),
},
Example {
description: "Reverse multiple strings in a list",
example: "['Nushell' 'is' 'cool'] | str reverse",
result: Some(Value::List {
vals: vec![
Value::String {
val: "llehsuN".to_string(),
span: Span::test_data(),
},
Value::String {
val: "si".to_string(),
span: Span::test_data(),
},
Value::String {
val: "looc".to_string(),
span: Span::test_data(),
},
Value::string("llehsuN", Span::test_data()),
Value::string("si", Span::test_data()),
Value::string("looc", Span::test_data()),
],
span: Span::test_data(),
}),

View File

@ -82,34 +82,22 @@ impl Command for SubCommand {
Example {
description: "Right-pad a string with asterisks until it's 10 characters wide",
example: "'nushell' | str rpad -l 10 -c '*'",
result: Some(Value::String {
val: "nushell***".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("nushell***", Span::test_data())),
},
Example {
description: "Right-pad a string with zeroes until it's 10 characters wide",
example: "'123' | str rpad -l 10 -c '0'",
result: Some(Value::String {
val: "1230000000".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("1230000000", Span::test_data())),
},
Example {
description: "Use rpad to truncate a string to its first three characters",
example: "'123456789' | str rpad -l 3 -c '0'",
result: Some(Value::String {
val: "123".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("123", Span::test_data())),
},
Example {
description: "Use rpad to pad Unicode",
example: "'▉' | str rpad -l 10 -c '▉'",
result: Some(Value::String {
val: "▉▉▉▉▉▉▉▉▉▉".to_string(),
span: Span::test_data(),
}),
result: Some(Value::string("▉▉▉▉▉▉▉▉▉▉", Span::test_data())),
},
]
}

View File

@ -70,26 +70,17 @@ impl Command for SubCommand {
Example {
description: "Checks if input string starts with 'my'",
example: "'my_library.rb' | str starts-with 'my'",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if input string starts with 'my'",
example: "'Cargo.toml' | str starts-with 'Car'",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if input string starts with 'my'",
example: "'Cargo.toml' | str starts-with '.toml'",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
@ -99,10 +90,7 @@ fn action(input: &Value, Arguments { substring, .. }: &Arguments, head: Span) ->
match input {
Value::String { val: s, .. } => {
let starts_with = s.starts_with(substring);
Value::Bool {
val: starts_with,
span: head,
}
Value::boolean(starts_with, head)
}
other => Value::Error {
error: ShellError::UnsupportedInput(

View File

@ -134,10 +134,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
if start < len && end >= 0 {
match start.cmp(&end) {
Ordering::Equal => Value::String {
val: "".to_string(),
span: head,
},
Ordering::Equal => Value::string("", head),
Ordering::Greater => Value::Error {
error: ShellError::UnsupportedInput(
"End must be greater than or equal to Start".to_string(),
@ -165,10 +162,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
},
}
} else {
Value::String {
val: "".to_string(),
span: head,
}
Value::string("", head)
}
}
other => Value::Error {
@ -304,10 +298,7 @@ mod tests {
#[test]
fn substrings_indexes() {
let word = Value::String {
val: "andres".to_string(),
span: Span::test_data(),
};
let word = Value::string("andres", Span::test_data());
let cases = vec![
expectation("a", (0, 1)),
@ -346,13 +337,7 @@ mod tests {
Span::test_data(),
);
assert_eq!(
actual,
Value::String {
val: expected.to_string(),
span: Span::test_data()
}
);
assert_eq!(actual, Value::string(expected, Span::test_data()));
}
}
}

View File

@ -295,10 +295,7 @@ mod tests {
cols: cols.iter().map(|x| x.to_string()).collect(),
vals: vals
.iter()
.map(|x| Value::String {
val: x.to_string(),
span: Span::test_data(),
})
.map(|x| Value::string(x.to_string(), Span::test_data()))
.collect(),
span: Span::test_data(),
}
@ -308,10 +305,7 @@ mod tests {
Value::List {
vals: vals
.iter()
.map(|x| Value::String {
val: x.to_string(),
span: Span::test_data(),
})
.map(|x| Value::string(x.to_string(), Span::test_data()))
.collect(),
span: Span::test_data(),
}
@ -475,35 +469,17 @@ mod tests {
fn global_trims_table_all_white_space() {
let row = Value::List {
vals: vec![
Value::String {
val: " nu shell ".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: " d".to_string(),
span: Span::test_data(),
},
Value::string(" nu shell ", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string(" d", Span::test_data()),
],
span: Span::test_data(),
};
let expected = Value::List {
vals: vec![
Value::String {
val: "nushell".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: "d".to_string(),
span: Span::test_data(),
},
Value::string("nushell", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string("d", Span::test_data()),
],
span: Span::test_data(),
};
@ -565,35 +541,17 @@ mod tests {
fn global_trims_table_all_custom_character() {
let row = Value::List {
vals: vec![
Value::String {
val: "##nu####shell##".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: "#d".to_string(),
span: Span::test_data(),
},
Value::string("##nu####shell##", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string("#d", Span::test_data()),
],
span: Span::test_data(),
};
let expected = Value::List {
vals: vec![
Value::String {
val: "nushell".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: "d".to_string(),
span: Span::test_data(),
},
Value::string("nushell", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string("d", Span::test_data()),
],
span: Span::test_data(),
};
@ -693,35 +651,17 @@ mod tests {
fn global_trim_left_table() {
let row = Value::List {
vals: vec![
Value::String {
val: " a ".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: " d".to_string(),
span: Span::test_data(),
},
Value::string(" a ", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string(" d", Span::test_data()),
],
span: Span::test_data(),
};
let expected = Value::List {
vals: vec![
Value::String {
val: "a ".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: "d".to_string(),
span: Span::test_data(),
},
Value::string("a ", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string("d", Span::test_data()),
],
span: Span::test_data(),
};
@ -836,35 +776,17 @@ mod tests {
fn global_trim_right_table() {
let row = Value::List {
vals: vec![
Value::String {
val: " a ".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: " d".to_string(),
span: Span::test_data(),
},
Value::string(" a ", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string(" d", Span::test_data()),
],
span: Span::test_data(),
};
let expected = Value::List {
vals: vec![
Value::String {
val: " a".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: " d".to_string(),
span: Span::test_data(),
},
Value::string(" a", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string(" d", Span::test_data()),
],
span: Span::test_data(),
};
@ -981,35 +903,17 @@ mod tests {
fn global_trim_format_flag_table() {
let row = Value::List {
vals: vec![
Value::String {
val: " a b c d ".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: " b c d e f".to_string(),
span: Span::test_data(),
},
Value::string(" a b c d ", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string(" b c d e f", Span::test_data()),
],
span: Span::test_data(),
};
let expected = Value::List {
vals: vec![
Value::String {
val: "a b c d".to_string(),
span: Span::test_data(),
},
Value::Int {
val: 65,
span: Span::test_data(),
},
Value::String {
val: "b c d e f".to_string(),
span: Span::test_data(),
},
Value::string("a b c d", Span::test_data()),
Value::int(65, Span::test_data()),
Value::string("b c d e f", Span::test_data()),
],
span: Span::test_data(),
};