From db07657e40a6d370e7b8634a21495eef19c522ee Mon Sep 17 00:00:00 2001 From: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> Date: Sat, 21 Jan 2023 14:47:00 +0100 Subject: [PATCH] Reduce number of `match` calls (#7813) --- .../src/completions/custom_completions.rs | 9 ++- crates/nu-cli/src/prompt.rs | 30 +++++---- crates/nu-cli/src/prompt_update.rs | 20 +++--- crates/nu-cli/src/repl.rs | 66 +++++++++---------- 4 files changed, 59 insertions(+), 66 deletions(-) diff --git a/crates/nu-cli/src/completions/custom_completions.rs b/crates/nu-cli/src/completions/custom_completions.rs index 85018287be..950685d45f 100644 --- a/crates/nu-cli/src/completions/custom_completions.rs +++ b/crates/nu-cli/src/completions/custom_completions.rs @@ -74,8 +74,8 @@ impl Completer for CustomCompletion { let mut custom_completion_options = None; // Parse result - let suggestions = match result { - Ok(pd) => { + let suggestions = result + .map(|pd| { let value = pd.into_value(span); match &value { Value::Record { .. } => { @@ -132,9 +132,8 @@ impl Completer for CustomCompletion { Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset), _ => vec![], } - } - _ => vec![], - }; + }) + .unwrap_or_default(); if let Some(custom_completion_options) = custom_completion_options { filter(&prefix, suggestions, &custom_completion_options) diff --git a/crates/nu-cli/src/prompt.rs b/crates/nu-cli/src/prompt.rs index 842e0a5fbb..4abc3ff74f 100644 --- a/crates/nu-cli/src/prompt.rs +++ b/crates/nu-cli/src/prompt.rs @@ -130,32 +130,36 @@ impl Prompt for NushellPrompt { fn render_prompt_indicator(&self, edit_mode: PromptEditMode) -> Cow { match edit_mode { PromptEditMode::Default => match &self.default_prompt_indicator { - Some(indicator) => indicator.as_str().into(), - None => "〉".into(), - }, + Some(indicator) => indicator, + None => "〉", + } + .into(), PromptEditMode::Emacs => match &self.default_prompt_indicator { - Some(indicator) => indicator.as_str().into(), - None => "〉".into(), - }, + Some(indicator) => indicator, + None => "〉", + } + .into(), PromptEditMode::Vi(vi_mode) => match vi_mode { PromptViMode::Normal => match &self.default_vi_normal_prompt_indicator { - Some(indicator) => indicator.as_str().into(), - None => ": ".into(), + Some(indicator) => indicator, + None => ": ", }, PromptViMode::Insert => match &self.default_vi_insert_prompt_indicator { - Some(indicator) => indicator.as_str().into(), - None => "〉".into(), + Some(indicator) => indicator, + None => "〉", }, - }, + } + .into(), PromptEditMode::Custom(str) => self.default_wrapped_custom_string(str).into(), } } fn render_prompt_multiline_indicator(&self) -> Cow { match &self.default_multiline_indicator { - Some(indicator) => indicator.as_str().into(), - None => "::: ".into(), + Some(indicator) => indicator, + None => "::: ", } + .into() } fn render_prompt_history_search_indicator( diff --git a/crates/nu-cli/src/prompt_update.rs b/crates/nu-cli/src/prompt_update.rs index 9dbef9d972..5dac7674e1 100644 --- a/crates/nu-cli/src/prompt_update.rs +++ b/crates/nu-cli/src/prompt_update.rs @@ -46,14 +46,12 @@ fn get_prompt_string( column!() ); - match ret_val { - Ok(ret_val) => Some(ret_val), - Err(err) => { + ret_val + .map_err(|err| { let working_set = StateWorkingSet::new(engine_state); report_error(&working_set, &err); - None - } - } + }) + .ok() } Value::Block { val: block_id, .. } => { let block = engine_state.get_block(block_id); @@ -66,14 +64,12 @@ fn get_prompt_string( column!() ); - match ret_val { - Ok(ret_val) => Some(ret_val), - Err(err) => { + ret_val + .map_err(|err| { let working_set = StateWorkingSet::new(engine_state); report_error(&working_set, &err); - None - } - } + }) + .ok() } Value::String { .. } => Some(PipelineData::Value(v.clone(), None)), _ => None, diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index f5145cb0ee..ba7ecf9f9f 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -91,10 +91,10 @@ pub fn evaluate_repl( let mut line_editor = Reedline::create(); // Now that reedline is created, get the history session id and store it in engine_state - let hist_sesh = match line_editor.get_history_session_id() { - Some(id) => i64::from(id), - None => 0, - }; + let hist_sesh = line_editor + .get_history_session_id() + .map(i64::from) + .unwrap_or(0); engine_state.history_session_id = hist_sesh; let config = engine_state.get_config(); @@ -217,14 +217,11 @@ pub fn evaluate_repl( line_editor.disable_hints() }; - line_editor = match add_menus(line_editor, engine_reference, stack, config) { - Ok(line_editor) => line_editor, - Err(e) => { - let working_set = StateWorkingSet::new(engine_state); - report_error(&working_set, &e); - Reedline::create() - } - }; + line_editor = add_menus(line_editor, engine_reference, stack, config).unwrap_or_else(|e| { + let working_set = StateWorkingSet::new(engine_state); + report_error(&working_set, &e); + Reedline::create() + }); let buffer_editor = if !config.buffer_editor.is_empty() { Some(config.buffer_editor.clone()) @@ -983,32 +980,29 @@ fn run_hook_block( } } - match eval_block_with_early_return(engine_state, &mut callee_stack, block, input, false, false) - { - Ok(pipeline_data) => { - if let PipelineData::Value(Value::Error { error }, _) = pipeline_data { - return Err(error); - } + let pipeline_data = + eval_block_with_early_return(engine_state, &mut callee_stack, block, input, false, false)?; - // If all went fine, preserve the environment of the called block - let caller_env_vars = stack.get_env_var_names(engine_state); - - // remove env vars that are present in the caller but not in the callee - // (the callee hid them) - for var in caller_env_vars.iter() { - if !callee_stack.has_env_var(engine_state, var) { - stack.remove_env_var(engine_state, var); - } - } - - // add new env vars from callee to caller - for (var, value) in callee_stack.get_stack_env_vars() { - stack.add_env_var(var, value); - } - Ok(pipeline_data) - } - Err(err) => Err(err), + if let PipelineData::Value(Value::Error { error }, _) = pipeline_data { + return Err(error); } + + // If all went fine, preserve the environment of the called block + let caller_env_vars = stack.get_env_var_names(engine_state); + + // remove env vars that are present in the caller but not in the callee + // (the callee hid them) + for var in caller_env_vars.iter() { + if !callee_stack.has_env_var(engine_state, var) { + stack.remove_env_var(engine_state, var); + } + } + + // add new env vars from callee to caller + for (var, value) in callee_stack.get_stack_env_vars() { + stack.add_env_var(var, value); + } + Ok(pipeline_data) } fn run_ansi_sequence(seq: &str) -> Result<(), ShellError> {