Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace various Value::Int {} with Value::int()
  • Loading branch information
webbedspace committed Dec 9, 2022
commit 60518242bd2d618aa206d59ab4b5d9fe927bb677
5 changes: 1 addition & 4 deletions crates/nu-cli/src/menus/menu_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ impl Completer for NuMenuCompleter {

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
8 changes: 1 addition & 7 deletions crates/nu-cli/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,7 @@ pub fn evaluate_repl(
},
);

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
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
21 changes: 6 additions & 15 deletions crates/nu-command/src/conversions/into/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ impl Command for SubCommand {
Example {
description: "Convert file size to integer",
example: "4KB | into int",
result: Some(Value::Int {
val: 4000,
span: Span::test_data(),
}),
result: Some(Value::int(4000, Span::test_data())),
},
Example {
description: "Convert bool to integer",
Expand Down Expand Up @@ -233,20 +230,14 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
}
val.resize(8, 0);

Value::Int {
val: LittleEndian::read_i64(&val),
span: *span,
}
Value::int(LittleEndian::read_i64(&val), *span)
} else {
while val.len() < 8 {
val.insert(0, 0);
}
val.resize(8, 0);

Value::Int {
val: BigEndian::read_i64(&val),
span: *span,
}
Value::int(BigEndian::read_i64(&val), *span)
}
}
_ => Value::Error {
Expand All @@ -269,13 +260,13 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
// octal
{
match int_from_string(val, head) {
Ok(x) => return Value::Int { val: x, span: head },
Ok(x) => return Value::int(x, head),
Err(e) => return Value::Error { error: e },
}
} else if val.starts_with("00") {
// It's a padded string
match i64::from_str_radix(val, radix) {
Ok(n) => return Value::Int { val: n, span: head },
Ok(n) => return Value::int(n, head),
Err(e) => {
return Value::Error {
error: ShellError::CantConvert(
Expand All @@ -300,7 +291,7 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
}
};
match i64::from_str_radix(i.trim(), radix) {
Ok(n) => Value::Int { val: n, span: head },
Ok(n) => Value::int(n, head),
Err(_reason) => Value::Error {
error: ShellError::CantConvert("string".to_string(), "int".to_string(), head, None),
},
Expand Down
16 changes: 2 additions & 14 deletions crates/nu-command/src/core_commands/for_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,7 @@ impl Command for For {
if numbered {
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span: head,
},
x,
],
vals: vec![Value::int(idx as i64, head), x],
span: head,
}
} else {
Expand Down Expand Up @@ -136,13 +130,7 @@ impl Command for For {
if numbered {
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span: head,
},
x,
],
vals: vec![Value::int(idx as i64, head), x],
span: head,
}
} else {
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/core_commands/loop_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ impl Command for Loop {
vec![Example {
description: "Loop while a condition is true",
example: "mut x = 0; loop { if $x > 10 { break }; $x = $x + 1 }; $x",
result: Some(Value::Int {
val: 11,
span: Span::test_data(),
}),
result: Some(Value::int(11, Span::test_data())),
}]
}
}
Expand Down
10 changes: 2 additions & 8 deletions crates/nu-command/src/dataframe/eager/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,9 @@ impl Command for ListDF {
span: call.head,
};

let columns = Value::Int {
val: df.as_ref().width() as i64,
span: call.head,
};
let columns = Value::int(df.as_ref().width() as i64, call.head);

let rows = Value::Int {
val: df.as_ref().height() as i64,
span: call.head,
};
let rows = Value::int(df.as_ref().height() as i64, call.head);

let cols = vec![
"name".to_string(),
Expand Down
10 changes: 2 additions & 8 deletions crates/nu-command/src/dataframe/eager/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,9 @@ fn command(
) -> Result<PipelineData, ShellError> {
let df = NuDataFrame::try_from_pipeline(input, call.head)?;

let rows = Value::Int {
val: df.as_ref().height() as i64,
span: call.head,
};
let rows = Value::int(df.as_ref().height() as i64, call.head);

let cols = Value::Int {
val: df.as_ref().width() as i64,
span: call.head,
};
let cols = Value::int(df.as_ref().width() as i64, call.head);

let rows_col = Column::new("rows".to_string(), vec![rows]);
let cols_col = Column::new("columns".to_string(), vec![cols]);
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/dataframe/series/n_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ fn command(
let df = NuDataFrame::try_from_pipeline(input, call.head)?;

let res = df.as_series(call.head)?.null_count();
let value = Value::Int {
val: res as i64,
span: call.head,
};
let value = Value::int(res as i64, call.head);

NuDataFrame::try_from_columns(vec![Column::new("count_null".to_string(), vec![value])])
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/dataframe/series/n_unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ fn command(
)
})?;

let value = Value::Int {
val: res as i64,
span: call.head,
};
let value = Value::int(res as i64, call.head);

NuDataFrame::try_from_columns(vec![Column::new("count_unique".to_string(), vec![value])])
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
Expand Down
30 changes: 6 additions & 24 deletions crates/nu-command/src/date/to_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,12 @@ fn parse_date_into_table(date: Result<DateTime<FixedOffset>, Value>, head: Span)
match date {
Ok(x) => {
let vals = vec![
Value::Int {
val: x.year() as i64,
span: head,
},
Value::Int {
val: x.month() as i64,
span: head,
},
Value::Int {
val: x.day() as i64,
span: head,
},
Value::Int {
val: x.hour() as i64,
span: head,
},
Value::Int {
val: x.minute() as i64,
span: head,
},
Value::Int {
val: x.second() as i64,
span: head,
},
Value::int(x.year() as i64, head),
Value::int(x.month() as i64, head),
Value::int(x.day() as i64, head),
Value::int(x.hour() as i64, head),
Value::int(x.minute() as i64, head),
Value::int(x.second() as i64, head),
Value::String {
val: x.offset().to_string(),
span: head,
Expand Down
30 changes: 6 additions & 24 deletions crates/nu-command/src/date/to_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,12 @@ fn parse_date_into_table(date: Result<DateTime<FixedOffset>, Value>, head: Span)
match date {
Ok(x) => {
let vals = vec![
Value::Int {
val: x.year() as i64,
span: head,
},
Value::Int {
val: x.month() as i64,
span: head,
},
Value::Int {
val: x.day() as i64,
span: head,
},
Value::Int {
val: x.hour() as i64,
span: head,
},
Value::Int {
val: x.minute() as i64,
span: head,
},
Value::Int {
val: x.second() as i64,
span: head,
},
Value::int(x.year() as i64, head),
Value::int(x.month() as i64, head),
Value::int(x.day() as i64, head),
Value::int(x.hour() as i64, head),
Value::int(x.minute() as i64, head),
Value::int(x.second() as i64, head),
Value::String {
val: x.offset().to_string(),
span: head,
Expand Down
Loading