mirror of
https://github.com/nushell/nushell.git
synced 2025-05-09 04:24:26 +02:00
remove duplicate code in math/log.rs (#15022)
# Description I have investigated all const commands and found that math log contains some duplicate code, which can be eliminated by introducing a new helper function. So this pr is going to do this # User-Facing Changes NaN # Tests + Formatting NaN # After Submitting NaN
This commit is contained in:
parent
0705fb9cd1
commit
3770a5eed1
@ -1,4 +1,5 @@
|
|||||||
use nu_engine::command_prelude::*;
|
use nu_engine::command_prelude::*;
|
||||||
|
use nu_protocol::Signals;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
@ -45,26 +46,8 @@ impl Command for SubCommand {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let head = call.head;
|
|
||||||
let base: Spanned<f64> = call.req(engine_state, stack, 0)?;
|
let base: Spanned<f64> = call.req(engine_state, stack, 0)?;
|
||||||
|
log(base, call.head, input, engine_state.signals())
|
||||||
if base.item <= 0.0f64 {
|
|
||||||
return Err(ShellError::UnsupportedInput {
|
|
||||||
msg: "Base has to be greater 0".into(),
|
|
||||||
input: "value originates from here".into(),
|
|
||||||
msg_span: head,
|
|
||||||
input_span: base.span,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// This doesn't match explicit nulls
|
|
||||||
if matches!(input, PipelineData::Empty) {
|
|
||||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
|
||||||
}
|
|
||||||
let base = base.item;
|
|
||||||
input.map(
|
|
||||||
move |value| operate(value, head, base),
|
|
||||||
engine_state.signals(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_const(
|
fn run_const(
|
||||||
@ -73,26 +56,8 @@ impl Command for SubCommand {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let head = call.head;
|
|
||||||
let base: Spanned<f64> = call.req_const(working_set, 0)?;
|
let base: Spanned<f64> = call.req_const(working_set, 0)?;
|
||||||
|
log(base, call.head, input, working_set.permanent().signals())
|
||||||
if base.item <= 0.0f64 {
|
|
||||||
return Err(ShellError::UnsupportedInput {
|
|
||||||
msg: "Base has to be greater 0".into(),
|
|
||||||
input: "value originates from here".into(),
|
|
||||||
msg_span: head,
|
|
||||||
input_span: base.span,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// This doesn't match explicit nulls
|
|
||||||
if matches!(input, PipelineData::Empty) {
|
|
||||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
|
||||||
}
|
|
||||||
let base = base.item;
|
|
||||||
input.map(
|
|
||||||
move |value| operate(value, head, base),
|
|
||||||
working_set.permanent().signals(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
@ -118,6 +83,28 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn log(
|
||||||
|
base: Spanned<f64>,
|
||||||
|
head: Span,
|
||||||
|
input: PipelineData,
|
||||||
|
signals: &Signals,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
if base.item <= 0.0f64 {
|
||||||
|
return Err(ShellError::UnsupportedInput {
|
||||||
|
msg: "Base has to be greater 0".into(),
|
||||||
|
input: "value originates from here".into(),
|
||||||
|
msg_span: head,
|
||||||
|
input_span: base.span,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// This doesn't match explicit nulls
|
||||||
|
if matches!(input, PipelineData::Empty) {
|
||||||
|
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||||
|
}
|
||||||
|
let base = base.item;
|
||||||
|
input.map(move |value| operate(value, head, base), signals)
|
||||||
|
}
|
||||||
|
|
||||||
fn operate(value: Value, head: Span, base: f64) -> Value {
|
fn operate(value: Value, head: Span, base: f64) -> Value {
|
||||||
let span = value.span();
|
let span = value.span();
|
||||||
match value {
|
match value {
|
||||||
|
Loading…
Reference in New Issue
Block a user