From 3770a5eed1ce6d194031257539ce99c23659910f Mon Sep 17 00:00:00 2001 From: Wind Date: Thu, 6 Feb 2025 21:00:25 +0800 Subject: [PATCH] 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 --- crates/nu-command/src/math/log.rs | 63 ++++++++++++------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/crates/nu-command/src/math/log.rs b/crates/nu-command/src/math/log.rs index 4c262017ca..daf231a497 100644 --- a/crates/nu-command/src/math/log.rs +++ b/crates/nu-command/src/math/log.rs @@ -1,4 +1,5 @@ use nu_engine::command_prelude::*; +use nu_protocol::Signals; #[derive(Clone)] pub struct SubCommand; @@ -45,26 +46,8 @@ impl Command for SubCommand { call: &Call, input: PipelineData, ) -> Result { - let head = call.head; let base: Spanned = call.req(engine_state, stack, 0)?; - - 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(), - ) + log(base, call.head, input, engine_state.signals()) } fn run_const( @@ -73,26 +56,8 @@ impl Command for SubCommand { call: &Call, input: PipelineData, ) -> Result { - let head = call.head; let base: Spanned = call.req_const(working_set, 0)?; - - 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(), - ) + log(base, call.head, input, working_set.permanent().signals()) } fn examples(&self) -> Vec { @@ -118,6 +83,28 @@ impl Command for SubCommand { } } +fn log( + base: Spanned, + head: Span, + input: PipelineData, + signals: &Signals, +) -> Result { + 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 { let span = value.span(); match value {