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
Replace various Value::Float{} with Value::float()
  • Loading branch information
webbedspace committed Dec 9, 2022
commit 3df44230b0a5ce25f8324f7b2c4d671867808dc2
7 changes: 2 additions & 5 deletions crates/nu-command/src/conversions/into/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
let other = s.trim();

match other.parse::<f64>() {
Ok(x) => Value::Float { val: x, span: head },
Ok(x) => Value::float(x, head),
Err(reason) => Value::Error {
error: ShellError::CantConvert(
"float".to_string(),
Expand All @@ -97,10 +97,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
},
}
}
Value::Int { val: v, span } => Value::Float {
val: *v as f64,
span: *span,
},
Value::Int { val: v, span } => Value::float(*v as f64, *span),
Value::Bool { val: b, span } => Value::Float {
val: match b {
true => 1.0,
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/formats/from/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ fn from_ods(
let value = match cell {
DataType::Empty => Value::nothing(head),
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::Float {
val: *f,
span: head,
},
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::boolean(*b, head),
_ => Value::nothing(head),
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/formats/from/xlsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ fn from_xlsx(
let value = match cell {
DataType::Empty => Value::nothing(head),
DataType::String(s) => Value::string(s, head),
DataType::Float(f) => Value::Float {
val: *f,
span: head,
},
DataType::Float(f) => Value::float(*f, head),
DataType::Int(i) => Value::int(*i, head),
DataType::Bool(b) => Value::boolean(*b, head),
_ => Value::nothing(head),
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/generators/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ impl Iterator for FloatSeq {
return None;
}
self.index += 1;
Some(Value::Float {
val: count,
span: self.span,
})
Some(Value::float(count, self.span))
}
}

Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/math/abs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ impl Command for SubCommand {
result: Some(Value::List {
vals: vec![
Value::test_int(50),
Value::Float {
val: 100.0,
span: Span::test_data(),
},
Value::float(100.0, Span::test_data()),
Value::test_int(25),
],
span: Span::test_data(),
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/math/avg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ impl Command for SubCommand {
vec![Example {
description: "Compute the average of a list of numbers",
example: "[-50 100.0 25] | math avg",
result: Some(Value::Float {
val: 25.0,
span: Span::test_data(),
}),
result: Some(Value::float(25.0, Span::test_data())),
}]
}
}
Expand Down
6 changes: 1 addition & 5 deletions crates/nu-command/src/math/euler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ impl Command for SubCommand {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::E,
span: call.head,
}
.into_pipeline_data())
Ok(Value::float(std::f64::consts::E, call.head).into_pipeline_data())
}
}

Expand Down
10 changes: 2 additions & 8 deletions crates/nu-command/src/math/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ impl Command for SubCommand {
vec![Example {
description: "Evaluate math in the pipeline",
example: "'10 / 4' | math eval",
result: Some(Value::Float {
val: 2.5,
span: Span::test_data(),
}),
result: Some(Value::float(2.5, Span::test_data())),
}]
}
}
Expand Down Expand Up @@ -104,10 +101,7 @@ pub fn parse(math_expression: &str, span: &Span) -> Result<Value, String> {
ctx.var("tau", std::f64::consts::TAU);
match meval::eval_str_with_context(math_expression, &ctx) {
Ok(num) if num.is_infinite() || num.is_nan() => Err("cannot represent result".to_string()),
Ok(num) => Ok(Value::Float {
val: num,
span: *span,
}),
Ok(num) => Ok(Value::float(num, *span)),
Err(error) => Err(error.to_string().to_lowercase()),
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/math/median.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ impl Command for SubCommand {
Example {
description: "Compute the median of a list of numbers",
example: "[3 8 9 12 12 15] | math median",
result: Some(Value::Float {
val: 10.5,
span: Span::test_data(),
}),
result: Some(Value::float(10.5, Span::test_data())),
},
Example {
description: "Compute the medians of the columns of a table",
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-command/src/math/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ fn recreate_value(hashable_value: &HashableType, head: Span) -> Value {
let bytes = hashable_value.bytes;
match &hashable_value.original_type {
NumberTypes::Int => Value::int(i64::from_ne_bytes(bytes), head),
NumberTypes::Float => Value::Float {
val: f64::from_ne_bytes(bytes),
span: head,
},
NumberTypes::Float => Value::float(f64::from_ne_bytes(bytes), head),
NumberTypes::Duration => Value::Duration {
val: i64::from_ne_bytes(bytes),
span: head,
Expand Down
6 changes: 1 addition & 5 deletions crates/nu-command/src/math/pi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ impl Command for SubCommand {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::PI,
span: call.head,
}
.into_pipeline_data())
Ok(Value::float(std::f64::consts::PI, call.head).into_pipeline_data())
}
}

Expand Down
15 changes: 3 additions & 12 deletions crates/nu-command/src/math/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,9 @@ impl Command for SubCommand {
example: "[1.555 2.333 -3.111] | math round -p 2",
result: Some(Value::List {
vals: vec![
Value::Float {
val: 1.56,
span: Span::test_data(),
},
Value::Float {
val: 2.33,
span: Span::test_data(),
},
Value::Float {
val: -3.11,
span: Span::test_data(),
},
Value::float(1.56, Span::test_data()),
Value::float(2.33, Span::test_data()),
Value::float(-3.11, Span::test_data()),
],
span: Span::test_data(),
}),
Expand Down
10 changes: 2 additions & 8 deletions crates/nu-command/src/math/stddev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,12 @@ impl Command for SubCommand {
Example {
description: "Compute the standard deviation of a list of numbers",
example: "[1 2 3 4 5] | math stddev",
result: Some(Value::Float {
val: std::f64::consts::SQRT_2,
span: Span::test_data(),
}),
result: Some(Value::float(std::f64::consts::SQRT_2, Span::test_data())),
},
Example {
description: "Compute the sample standard deviation of a list of numbers",
example: "[1 2 3 4 5] | math stddev -s",
result: Some(Value::Float {
val: 1.5811388300841898,
span: Span::test_data(),
}),
result: Some(Value::float(1.5811388300841898, Span::test_data())),
},
]
}
Expand Down
6 changes: 1 addition & 5 deletions crates/nu-command/src/math/tau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ impl Command for SubCommand {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::Float {
val: std::f64::consts::TAU,
span: call.head,
}
.into_pipeline_data())
Ok(Value::float(std::f64::consts::TAU, call.head).into_pipeline_data())
}
}

Expand Down
10 changes: 2 additions & 8 deletions crates/nu-command/src/math/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,12 @@ impl Command for SubCommand {
Example {
description: "Get the variance of a list of numbers",
example: "echo [1 2 3 4 5] | math variance",
result: Some(Value::Float {
val: 2.0,
span: Span::test_data(),
}),
result: Some(Value::float(2.0, Span::test_data())),
},
Example {
description: "Get the sample variance of a list of numbers",
example: "[1 2 3 4 5] | math variance -s",
result: Some(Value::Float {
val: 2.5,
span: Span::test_data(),
}),
result: Some(Value::float(2.5, Span::test_data())),
},
]
}
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-engine/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,7 @@ pub fn eval_expression(
match &expr.expr {
Expr::Bool(b) => Ok(Value::boolean(*b, expr.span)),
Expr::Int(i) => Ok(Value::int(*i, expr.span)),
Expr::Float(f) => Ok(Value::Float {
val: *f,
span: expr.span,
}),
Expr::Float(f) => Ok(Value::float(*f, expr.span)),
Expr::Binary(b) => Ok(Value::Binary {
val: b.clone(),
span: expr.span,
Expand Down
20 changes: 4 additions & 16 deletions crates/nu-protocol/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ impl Clone for Value {
val: val.clone(),
span: *span,
},
Value::Float { val, span } => Value::Float {
val: *val,
span: *span,
},
Value::Float { val, span } => Value::float(*val, *span),
Value::String { val, span } => Value::String {
val: val.clone(),
span: *span,
Expand Down Expand Up @@ -3374,29 +3371,20 @@ mod tests {
span: Span::unknown(),
};
let list_of_floats = Value::List {
vals: vec![Value::Float {
val: 0.0,
span: Span::unknown(),
}],
vals: vec![Value::float(0.0, Span::unknown())],
span: Span::unknown(),
};
let list_of_ints_and_floats = Value::List {
vals: vec![
Value::int(0, Span::unknown()),
Value::Float {
val: 0.0,
span: Span::unknown(),
},
Value::float(0.0, Span::unknown()),
],
span: Span::unknown(),
};
let list_of_ints_and_floats_and_bools = Value::List {
vals: vec![
Value::int(0, Span::unknown()),
Value::Float {
val: 0.0,
span: Span::unknown(),
},
Value::float(0.0, Span::unknown()),
Value::boolean(false, Span::unknown()),
],
span: Span::unknown(),
Expand Down
5 changes: 1 addition & 4 deletions crates/nu-protocol/tests/test_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ fn test_comparison_nothing() {
let values = vec![
Value::int(1, Span::test_data()),
Value::string("string", Span::test_data()),
Value::Float {
val: 1.0,
span: Span::test_data(),
},
Value::float(1.0, Span::test_data()),
];

let nothing = Value::Nothing {
Expand Down