Skip to content

Commit

Permalink
Reduced LOC by replacing several instances of Value::Int {}, `Value…
Browse files Browse the repository at this point in the history
…::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.
  • Loading branch information
webbedspace committed Dec 9, 2022
1 parent b56ad92 commit 220b105
Show file tree
Hide file tree
Showing 139 changed files with 540 additions and 2,141 deletions.
5 changes: 1 addition & 4 deletions crates/nu-cli/src/completions/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ impl NuCompleter {
Value::List {
vals: spans
.iter()
.map(|it| Value::String {
val: it.to_string(),
span: Span::unknown(),
})
.map(|it| Value::string(it, Span::unknown()))
.collect(),
span: Span::unknown(),
},
Expand Down
10 changes: 2 additions & 8 deletions crates/nu-cli/src/menus/menu_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,14 @@ impl Completer for NuMenuCompleter {

if let Some(buffer) = block.signature.get_positional(0) {
if let Some(buffer_id) = &buffer.var_id {
let line_buffer = Value::String {
val: parsed.remainder.to_string(),
span: self.span,
};
let line_buffer = Value::string(parsed.remainder, self.span);
self.stack.add_var(*buffer_id, line_buffer);
}
}

if let Some(position) = block.signature.get_positional(1) {
if let Some(position_id) = &position.var_id {
let line_buffer = Value::Int {
val: pos as i64,
span: self.span,
};
let line_buffer = Value::int(pos as i64, self.span);
self.stack.add_var(*position_id, line_buffer);
}
}
Expand Down
55 changes: 11 additions & 44 deletions crates/nu-cli/src/reedline_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,10 +993,7 @@ mod test {
#[test]
fn test_send_event() {
let cols = vec!["send".to_string()];
let vals = vec![Value::String {
val: "Enter".to_string(),
span: Span::test_data(),
}];
let vals = vec![Value::string("Enter", Span::test_data())];

let span = Span::test_data();
let b = EventType::try_from_columns(&cols, &vals, &span).unwrap();
Expand All @@ -1016,10 +1013,7 @@ mod test {
#[test]
fn test_edit_event() {
let cols = vec!["edit".to_string()];
let vals = vec![Value::String {
val: "Clear".to_string(),
span: Span::test_data(),
}];
let vals = vec![Value::string("Clear", Span::test_data())];

let span = Span::test_data();
let b = EventType::try_from_columns(&cols, &vals, &span).unwrap();
Expand All @@ -1043,14 +1037,8 @@ mod test {
fn test_send_menu() {
let cols = vec!["send".to_string(), "name".to_string()];
let vals = vec![
Value::String {
val: "Menu".to_string(),
span: Span::test_data(),
},
Value::String {
val: "history_menu".to_string(),
span: Span::test_data(),
},
Value::string("Menu", Span::test_data()),
Value::string("history_menu", Span::test_data()),
];

let span = Span::test_data();
Expand All @@ -1076,14 +1064,8 @@ mod test {
// Menu event
let cols = vec!["send".to_string(), "name".to_string()];
let vals = vec![
Value::String {
val: "Menu".to_string(),
span: Span::test_data(),
},
Value::String {
val: "history_menu".to_string(),
span: Span::test_data(),
},
Value::string("Menu", Span::test_data()),
Value::string("history_menu", Span::test_data()),
];

let menu_event = Value::Record {
Expand All @@ -1094,10 +1076,7 @@ mod test {

// Enter event
let cols = vec!["send".to_string()];
let vals = vec![Value::String {
val: "Enter".to_string(),
span: Span::test_data(),
}];
let vals = vec![Value::string("Enter", Span::test_data())];

let enter_event = Value::Record {
cols,
Expand Down Expand Up @@ -1138,14 +1117,8 @@ mod test {
// Menu event
let cols = vec!["send".to_string(), "name".to_string()];
let vals = vec![
Value::String {
val: "Menu".to_string(),
span: Span::test_data(),
},
Value::String {
val: "history_menu".to_string(),
span: Span::test_data(),
},
Value::string("Menu", Span::test_data()),
Value::string("history_menu", Span::test_data()),
];

let menu_event = Value::Record {
Expand All @@ -1156,10 +1129,7 @@ mod test {

// Enter event
let cols = vec!["send".to_string()];
let vals = vec![Value::String {
val: "Enter".to_string(),
span: Span::test_data(),
}];
let vals = vec![Value::string("Enter", Span::test_data())];

let enter_event = Value::Record {
cols,
Expand Down Expand Up @@ -1187,10 +1157,7 @@ mod test {
#[test]
fn test_error() {
let cols = vec!["not_exist".to_string()];
let vals = vec![Value::String {
val: "Enter".to_string(),
span: Span::test_data(),
}];
let vals = vec![Value::string("Enter", Span::test_data())];

let span = Span::test_data();
let b = EventType::try_from_columns(&cols, &vals, &span);
Expand Down
13 changes: 2 additions & 11 deletions crates/nu-cli/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,10 @@ pub fn evaluate_repl(
// seed env vars
stack.add_env_var(
"CMD_DURATION_MS".into(),
Value::String {
val: "0823".to_string(),
span: Span::unknown(),
},
Value::string("0823", Span::unknown()),
);

stack.add_env_var(
"LAST_EXIT_CODE".into(),
Value::Int {
val: 0,
span: Span::unknown(),
},
);
stack.add_env_var("LAST_EXIT_CODE".into(), Value::int(0, Span::unknown()));

info!(
"load config initially {}:{}:{}",
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,7 @@ pub fn eval_source(
fn set_last_exit_code(stack: &mut Stack, exit_code: i64) {
stack.add_env_var(
"LAST_EXIT_CODE".to_string(),
Value::Int {
val: exit_code,
span: Span::unknown(),
},
Value::int(exit_code, Span::unknown()),
);
}

Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/bits/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ impl Command for SubCommand {
Example {
description: "Apply bits and to two numbers",
example: "2 | bits and 2",
result: Some(Value::Int {
val: 2,
span: Span::test_data(),
}),
result: Some(Value::int(2, Span::test_data())),
},
Example {
description: "Apply logical and to a list of numbers",
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/bits/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ impl Command for SubCommand {
Example {
description: "Apply bits or to two numbers",
example: "2 | bits or 6",
result: Some(Value::Int {
val: 6,
span: Span::test_data(),
}),
result: Some(Value::int(6, Span::test_data())),
},
Example {
description: "Apply logical or to a list of numbers",
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/bits/rotate_left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ impl Command for SubCommand {
Example {
description: "Rotate left a number with 2 bits",
example: "17 | bits rol 2",
result: Some(Value::Int {
val: 68,
span: Span::test_data(),
}),
result: Some(Value::int(68, Span::test_data())),
},
Example {
description: "Rotate left a list of numbers with 2 bits",
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/bits/rotate_right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ impl Command for SubCommand {
Example {
description: "Rotate right a number with 60 bits",
example: "17 | bits ror 60",
result: Some(Value::Int {
val: 272,
span: Span::test_data(),
}),
result: Some(Value::int(272, Span::test_data())),
},
Example {
description: "Rotate right a list of numbers of one byte",
Expand Down
15 changes: 3 additions & 12 deletions crates/nu-command/src/bits/shift_left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,17 @@ impl Command for SubCommand {
Example {
description: "Shift left a number by 7 bits",
example: "2 | bits shl 7",
result: Some(Value::Int {
val: 256,
span: Span::test_data(),
}),
result: Some(Value::int(256, Span::test_data())),
},
Example {
description: "Shift left a number with 1 byte by 7 bits",
example: "2 | bits shl 7 -n 1",
result: Some(Value::Int {
val: 0,
span: Span::test_data(),
}),
result: Some(Value::int(0, Span::test_data())),
},
Example {
description: "Shift left a signed number by 1 bit",
example: "0x7F | bits shl 1 -s",
result: Some(Value::Int {
val: 254,
span: Span::test_data(),
}),
result: Some(Value::int(254, Span::test_data())),
},
Example {
description: "Shift left a list of numbers",
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/bits/shift_right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ impl Command for SubCommand {
Example {
description: "Shift right a number with 2 bits",
example: "8 | bits shr 2",
result: Some(Value::Int {
val: 2,
span: Span::test_data(),
}),
result: Some(Value::int(2, Span::test_data())),
},
Example {
description: "Shift right a list of numbers",
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/bits/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ impl Command for SubCommand {
Example {
description: "Apply bits xor to two numbers",
example: "2 | bits xor 2",
result: Some(Value::Int {
val: 0,
span: Span::test_data(),
}),
result: Some(Value::int(0, Span::test_data())),
},
Example {
description: "Apply logical xor to a list of numbers",
Expand Down
20 changes: 4 additions & 16 deletions crates/nu-command/src/bytes/ends_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,17 @@ impl Command for BytesEndsWith {
Example {
description: "Checks if binary ends with `0x[AA]`",
example: "0x[1F FF AA AA] | bytes ends-with 0x[AA]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary ends with `0x[FF AA AA]`",
example: "0x[1F FF AA AA] | bytes ends-with 0x[FF AA AA]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary ends with `0x[11]`",
example: "0x[1F FF AA AA] | bytes ends-with 0x[11]",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
Expand All @@ -98,10 +89,7 @@ fn ends_with(val: &Value, args: &Arguments, span: Span) -> Value {
Value::Binary {
val,
span: val_span,
} => Value::Bool {
val: val.ends_with(&args.pattern),
span: *val_span,
},
} => Value::boolean(val.ends_with(&args.pattern), *val_span),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/bytes/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ fn length(val: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
Value::Binary {
val,
span: val_span,
} => Value::Int {
val: val.len() as i64,
span: *val_span,
},
} => Value::int(val.len() as i64, *val_span),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(
Expand Down
20 changes: 4 additions & 16 deletions crates/nu-command/src/bytes/starts_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,17 @@ impl Command for BytesStartsWith {
Example {
description: "Checks if binary starts with `0x[1F FF AA]`",
example: "0x[1F FF AA AA] | bytes starts-with 0x[1F FF AA]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary starts with `0x[1F]`",
example: "0x[1F FF AA AA] | bytes starts-with 0x[1F]",
result: Some(Value::Bool {
val: true,
span: Span::test_data(),
}),
result: Some(Value::boolean(true, Span::test_data())),
},
Example {
description: "Checks if binary starts with `0x[1F]`",
example: "0x[1F FF AA AA] | bytes starts-with 0x[11]",
result: Some(Value::Bool {
val: false,
span: Span::test_data(),
}),
result: Some(Value::boolean(false, Span::test_data())),
},
]
}
Expand All @@ -104,10 +95,7 @@ fn starts_with(val: &Value, args: &Arguments, span: Span) -> Value {
Value::Binary {
val,
span: val_span,
} => Value::Bool {
val: val.starts_with(&args.pattern),
span: *val_span,
},
} => Value::boolean(val.starts_with(&args.pattern), *val_span),
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(
Expand Down

0 comments on commit 220b105

Please sign in to comment.