Reduce number of match calls (#7813)

This commit is contained in:
Hofer-Julian 2023-01-21 14:47:00 +01:00 committed by GitHub
parent 2d98d0fcc2
commit db07657e40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 66 deletions

View file

@ -74,8 +74,8 @@ impl Completer for CustomCompletion {
let mut custom_completion_options = None; let mut custom_completion_options = None;
// Parse result // Parse result
let suggestions = match result { let suggestions = result
Ok(pd) => { .map(|pd| {
let value = pd.into_value(span); let value = pd.into_value(span);
match &value { match &value {
Value::Record { .. } => { Value::Record { .. } => {
@ -132,9 +132,8 @@ impl Completer for CustomCompletion {
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset), Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
_ => vec![], _ => vec![],
} }
} })
_ => vec![], .unwrap_or_default();
};
if let Some(custom_completion_options) = custom_completion_options { if let Some(custom_completion_options) = custom_completion_options {
filter(&prefix, suggestions, &custom_completion_options) filter(&prefix, suggestions, &custom_completion_options)

View file

@ -130,32 +130,36 @@ impl Prompt for NushellPrompt {
fn render_prompt_indicator(&self, edit_mode: PromptEditMode) -> Cow<str> { fn render_prompt_indicator(&self, edit_mode: PromptEditMode) -> Cow<str> {
match edit_mode { match edit_mode {
PromptEditMode::Default => match &self.default_prompt_indicator { PromptEditMode::Default => match &self.default_prompt_indicator {
Some(indicator) => indicator.as_str().into(), Some(indicator) => indicator,
None => "".into(), None => "",
}, }
.into(),
PromptEditMode::Emacs => match &self.default_prompt_indicator { PromptEditMode::Emacs => match &self.default_prompt_indicator {
Some(indicator) => indicator.as_str().into(), Some(indicator) => indicator,
None => "".into(), None => "",
}, }
.into(),
PromptEditMode::Vi(vi_mode) => match vi_mode { PromptEditMode::Vi(vi_mode) => match vi_mode {
PromptViMode::Normal => match &self.default_vi_normal_prompt_indicator { PromptViMode::Normal => match &self.default_vi_normal_prompt_indicator {
Some(indicator) => indicator.as_str().into(), Some(indicator) => indicator,
None => ": ".into(), None => ": ",
}, },
PromptViMode::Insert => match &self.default_vi_insert_prompt_indicator { PromptViMode::Insert => match &self.default_vi_insert_prompt_indicator {
Some(indicator) => indicator.as_str().into(), Some(indicator) => indicator,
None => "".into(), None => "",
},
}, },
}
.into(),
PromptEditMode::Custom(str) => self.default_wrapped_custom_string(str).into(), PromptEditMode::Custom(str) => self.default_wrapped_custom_string(str).into(),
} }
} }
fn render_prompt_multiline_indicator(&self) -> Cow<str> { fn render_prompt_multiline_indicator(&self) -> Cow<str> {
match &self.default_multiline_indicator { match &self.default_multiline_indicator {
Some(indicator) => indicator.as_str().into(), Some(indicator) => indicator,
None => "::: ".into(), None => "::: ",
} }
.into()
} }
fn render_prompt_history_search_indicator( fn render_prompt_history_search_indicator(

View file

@ -46,14 +46,12 @@ fn get_prompt_string(
column!() column!()
); );
match ret_val { ret_val
Ok(ret_val) => Some(ret_val), .map_err(|err| {
Err(err) => {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err); report_error(&working_set, &err);
None })
} .ok()
}
} }
Value::Block { val: block_id, .. } => { Value::Block { val: block_id, .. } => {
let block = engine_state.get_block(block_id); let block = engine_state.get_block(block_id);
@ -66,14 +64,12 @@ fn get_prompt_string(
column!() column!()
); );
match ret_val { ret_val
Ok(ret_val) => Some(ret_val), .map_err(|err| {
Err(err) => {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err); report_error(&working_set, &err);
None })
} .ok()
}
} }
Value::String { .. } => Some(PipelineData::Value(v.clone(), None)), Value::String { .. } => Some(PipelineData::Value(v.clone(), None)),
_ => None, _ => None,

View file

@ -91,10 +91,10 @@ pub fn evaluate_repl(
let mut line_editor = Reedline::create(); let mut line_editor = Reedline::create();
// Now that reedline is created, get the history session id and store it in engine_state // 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() { let hist_sesh = line_editor
Some(id) => i64::from(id), .get_history_session_id()
None => 0, .map(i64::from)
}; .unwrap_or(0);
engine_state.history_session_id = hist_sesh; engine_state.history_session_id = hist_sesh;
let config = engine_state.get_config(); let config = engine_state.get_config();
@ -217,14 +217,11 @@ pub fn evaluate_repl(
line_editor.disable_hints() line_editor.disable_hints()
}; };
line_editor = match add_menus(line_editor, engine_reference, stack, config) { line_editor = add_menus(line_editor, engine_reference, stack, config).unwrap_or_else(|e| {
Ok(line_editor) => line_editor,
Err(e) => {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e); report_error(&working_set, &e);
Reedline::create() Reedline::create()
} });
};
let buffer_editor = if !config.buffer_editor.is_empty() { let buffer_editor = if !config.buffer_editor.is_empty() {
Some(config.buffer_editor.clone()) Some(config.buffer_editor.clone())
@ -983,9 +980,9 @@ fn run_hook_block(
} }
} }
match eval_block_with_early_return(engine_state, &mut callee_stack, block, input, false, false) let pipeline_data =
{ 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 { if let PipelineData::Value(Value::Error { error }, _) = pipeline_data {
return Err(error); return Err(error);
} }
@ -1007,9 +1004,6 @@ fn run_hook_block(
} }
Ok(pipeline_data) Ok(pipeline_data)
} }
Err(err) => Err(err),
}
}
fn run_ansi_sequence(seq: &str) -> Result<(), ShellError> { fn run_ansi_sequence(seq: &str) -> Result<(), ShellError> {
match io::stdout().write_all(seq.as_bytes()) { match io::stdout().write_all(seq.as_bytes()) {