From de18b9ca2c883460d509f64ba2e75c1af24b5cbc Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Sat, 25 Jul 2020 08:40:35 -0400 Subject: [PATCH] Match cleanup (#2248) --- crates/nu-cli/src/cli.rs | 26 +++----- crates/nu-cli/src/commands/cal.rs | 9 +-- crates/nu-cli/src/commands/default.rs | 8 +-- crates/nu-cli/src/commands/run_external.rs | 5 +- crates/nu-cli/src/commands/save.rs | 5 +- crates/nu-cli/src/commands/split_by.rs | 5 +- crates/nu-cli/src/commands/table.rs | 11 ++-- crates/nu-cli/src/git.rs | 11 ++-- crates/nu-cli/src/shell/filesystem_shell.rs | 5 +- crates/nu-protocol/src/call_info.rs | 5 +- crates/nu-protocol/src/value/dict.rs | 5 +- crates/nu_plugin_textview/src/textview.rs | 68 ++++----------------- 12 files changed, 40 insertions(+), 123 deletions(-) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index d8606c981..9f8152343 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -581,17 +581,11 @@ pub fn set_rustyline_configuration() -> (Editor, IndexMap // } // _ => rustyline::config::HistoryDuplicates::AlwaysAdd, // }; - let history_duplicates = match value.as_bool() { - Ok(b) => b, - _ => true, - }; + let history_duplicates = value.as_bool().unwrap_or(true); rl.set_history_ignore_dups(history_duplicates); } "history_ignore_space" => { - let history_ignore_space = match value.as_bool() { - Ok(b) => b, - _ => true, - }; + let history_ignore_space = value.as_bool().unwrap_or(true); rl.set_history_ignore_space(history_ignore_space); } "completion_type" => { @@ -635,10 +629,7 @@ pub fn set_rustyline_configuration() -> (Editor, IndexMap rl.set_edit_mode(edit_mode); } "auto_add_history" => { - let auto_add_history = match value.as_bool() { - Ok(b) => b, - _ => true, - }; + let auto_add_history = value.as_bool().unwrap_or(true); rl.set_auto_add_history(auto_add_history); } "bell_style" => { @@ -720,13 +711,10 @@ pub async fn cli( let _ = load_plugins(&mut context); let (mut rl, config) = set_rustyline_configuration(); - let use_starship = match config.get("use_starship") { - Some(b) => match b.as_bool() { - Ok(b) => b, - _ => false, - }, - _ => false, - }; + let use_starship = config + .get("use_starship") + .map(|x| x.is_true()) + .unwrap_or(false); #[cfg(windows)] { diff --git a/crates/nu-cli/src/commands/cal.rs b/crates/nu-cli/src/commands/cal.rs index 9bf9ad0d8..5b84d1004 100644 --- a/crates/nu-cli/src/commands/cal.rs +++ b/crates/nu-cli/src/commands/cal.rs @@ -100,7 +100,7 @@ pub async fn cal( (current_month, current_month) }; - let add_months_of_year_to_table_result = add_months_of_year_to_table( + add_months_of_year_to_table( &args, &mut calendar_vec_deque, &tag, @@ -108,12 +108,9 @@ pub async fn cal( month_range, current_month, current_day_option, - ); + )?; - match add_months_of_year_to_table_result { - Ok(()) => Ok(futures::stream::iter(calendar_vec_deque).to_output_stream()), - Err(error) => Err(error), - } + Ok(futures::stream::iter(calendar_vec_deque).to_output_stream()) } fn get_invalid_year_shell_error(year_tag: &Tag) -> ShellError { diff --git a/crates/nu-cli/src/commands/default.rs b/crates/nu-cli/src/commands/default.rs index b18882126..83c69bcd1 100644 --- a/crates/nu-cli/src/commands/default.rs +++ b/crates/nu-cli/src/commands/default.rs @@ -60,13 +60,13 @@ async fn default( Ok(input .map(move |item| { - let should_add = match item { + let should_add = matches!( + item, Value { value: UntaggedValue::Row(ref r), .. - } => r.get_data(&column.item).borrow().is_none(), - _ => false, - }; + } if r.get_data(&column.item).borrow().is_none() + ); if should_add { match item.insert_data_at_path(&column.item, value.clone()) { diff --git a/crates/nu-cli/src/commands/run_external.rs b/crates/nu-cli/src/commands/run_external.rs index 042242628..bb9ba0b0d 100644 --- a/crates/nu-cli/src/commands/run_external.rs +++ b/crates/nu-cli/src/commands/run_external.rs @@ -140,10 +140,7 @@ impl WholeStreamCommand for RunExternalCommand { external::run_external_command(command, &mut external_context, input, &scope, is_last) .await; - match result { - Ok(stream) => Ok(stream.to_output_stream()), - Err(e) => Err(e), - } + Ok(result?.to_output_stream()) } } diff --git a/crates/nu-cli/src/commands/save.rs b/crates/nu-cli/src/commands/save.rs index 44890267e..2d77977a3 100644 --- a/crates/nu-cli/src/commands/save.rs +++ b/crates/nu-cli/src/commands/save.rs @@ -251,10 +251,7 @@ async fn save( }; }; - match content { - Ok(save_data) => shell_manager.save(&full_path, &save_data, name.span), - Err(e) => Err(e), - } + shell_manager.save(&full_path, &content?, name.span) } fn string_from(input: &[Value]) -> String { diff --git a/crates/nu-cli/src/commands/split_by.rs b/crates/nu-cli/src/commands/split_by.rs index cfb918fda..b7ba16892 100644 --- a/crates/nu-cli/src/commands/split_by.rs +++ b/crates/nu-cli/src/commands/split_by.rs @@ -91,10 +91,7 @@ pub fn split( crate::utils::data::split(&values, &Some(block), &name) } Grouper::ByColumn(None) => { - let block = Box::new(move |_, row: &Value| match as_string(row) { - Ok(group_key) => Ok(group_key), - Err(reason) => Err(reason), - }); + let block = Box::new(move |_, row: &Value| as_string(row)); crate::utils::data::split(&values, &Some(block), &name) } diff --git a/crates/nu-cli/src/commands/table.rs b/crates/nu-cli/src/commands/table.rs index bf30de8f9..df8a32d6e 100644 --- a/crates/nu-cli/src/commands/table.rs +++ b/crates/nu-cli/src/commands/table.rs @@ -74,13 +74,10 @@ pub fn from_list(values: &[Value], starting_idx: usize) -> nu_table::Table { _ => ansi_term::Color::Green, }; - let header_bold = match config.get("header_bold") { - Some(b) => match b.as_bool() { - Ok(b) => b, - _ => true, - }, - _ => true, - }; + let header_bold = config + .get("header_bold") + .map(|x| x.as_bool().unwrap_or(true)) + .unwrap_or(true); TextStyle { alignment: header_align, diff --git a/crates/nu-cli/src/git.rs b/crates/nu-cli/src/git.rs index 04ef7830f..b1141b042 100644 --- a/crates/nu-cli/src/git.rs +++ b/crates/nu-cli/src/git.rs @@ -2,13 +2,10 @@ use crate::prelude::*; pub fn current_branch() -> Option { if let Ok(config) = crate::data::config::config(Tag::unknown()) { - let use_starship = match config.get("use_starship") { - Some(b) => match b.as_bool() { - Ok(b) => b, - _ => false, - }, - _ => false, - }; + let use_starship = config + .get("use_starship") + .map(|x| x.is_true()) + .unwrap_or(false); if !use_starship { #[cfg(feature = "git2")] diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-cli/src/shell/filesystem_shell.rs index 6b3e6bd06..7bd10857e 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-cli/src/shell/filesystem_shell.rs @@ -292,10 +292,7 @@ impl Shell for FilesystemShell { )); } - let any_source_is_dir = sources.iter().any(|f| match f { - Ok(f) => f.is_dir(), - Err(_) => false, - }); + let any_source_is_dir = sources.iter().any(|f| matches!(f, Ok(f) if f.is_dir())); if any_source_is_dir && !recursive.item { return Err(ShellError::labeled_error( diff --git a/crates/nu-protocol/src/call_info.rs b/crates/nu-protocol/src/call_info.rs index 3c3442944..e2bbbd251 100644 --- a/crates/nu-protocol/src/call_info.rs +++ b/crates/nu-protocol/src/call_info.rs @@ -69,10 +69,7 @@ impl EvaluatedArgs { /// Return true if the set of named arguments contains the name provided pub fn has(&self, name: &str) -> bool { - match &self.named { - None => false, - Some(named) => named.contains_key(name), - } + matches!(&self.named, Some(named) if named.contains_key(name)) } /// Gets the corresponding Value for the named argument given, if possible diff --git a/crates/nu-protocol/src/value/dict.rs b/crates/nu-protocol/src/value/dict.rs index b66307dec..9a82d6cc7 100644 --- a/crates/nu-protocol/src/value/dict.rs +++ b/crates/nu-protocol/src/value/dict.rs @@ -64,10 +64,7 @@ impl Ord for Dictionary { impl PartialEq for Dictionary { /// Test a dictionary against a Value for equality fn eq(&self, other: &Value) -> bool { - match &other.value { - UntaggedValue::Row(d) => self == d, - _ => false, - } + matches!(&other.value, UntaggedValue::Row(d) if self == d) } } diff --git a/crates/nu_plugin_textview/src/textview.rs b/crates/nu_plugin_textview/src/textview.rs index 598bd7c43..4ce9f3474 100644 --- a/crates/nu_plugin_textview/src/textview.rs +++ b/crates/nu_plugin_textview/src/textview.rs @@ -48,48 +48,15 @@ pub fn view_text_value(value: &Value) { _ => 4u64, } } - "colored_output" => { - colored_output = match value.as_bool() { - Ok(b) => b, - _ => true, - } - } - "true_color" => { - true_color = match value.as_bool() { - Ok(b) => b, - _ => true, - } - } - "header" => { - header = match value.as_bool() { - Ok(b) => b, - _ => true, - } - } - "line_numbers" => { - line_numbers = match value.as_bool() { - Ok(b) => b, - _ => true, - } - } - "grid" => { - grid = match value.as_bool() { - Ok(b) => b, - _ => true, - } - } + "colored_output" => colored_output = value.as_bool().unwrap_or(true), + "true_color" => true_color = value.as_bool().unwrap_or(true), + "header" => header = value.as_bool().unwrap_or(true), + "line_numbers" => line_numbers = value.as_bool().unwrap_or(true), + "grid" => grid = value.as_bool().unwrap_or(true), "vcs_modification_markers" => { - vcs_modification_markers = match value.as_bool() { - Ok(b) => b, - _ => true, - } - } - "snip" => { - snip = match value.as_bool() { - Ok(b) => b, - _ => true, - } + vcs_modification_markers = value.as_bool().unwrap_or(true) } + "snip" => snip = value.as_bool().unwrap_or(true), "wrapping_mode" => { wrapping_mode = match value.as_string() { Ok(s) if s.to_lowercase() == "nowrapping" => { @@ -101,12 +68,7 @@ pub fn view_text_value(value: &Value) { _ => bat::WrappingMode::NoWrapping, } } - "use_italics" => { - use_italics = match value.as_bool() { - Ok(b) => b, - _ => true, - } - } + "use_italics" => use_italics = value.as_bool().unwrap_or(true), "paging_mode" => { paging_mode = match value.as_string() { Ok(s) if s.to_lowercase() == "always" => bat::PagingMode::Always, @@ -117,19 +79,13 @@ pub fn view_text_value(value: &Value) { _ => bat::PagingMode::QuitIfOneScreen, } } - "pager" => { - pager = match value.as_string() { - Ok(s) => s, - _ => "less".to_string(), - } - } + "pager" => pager = value.as_string().unwrap_or_else(|_| "less".to_string()), "line_ranges" => line_ranges = bat::line_range::LineRanges::all(), // not real sure what to do with this "highlight_range" => _highlight_range = "0,0", //ignore config value for now "theme" => { - theme = match value.as_string() { - Ok(s) => s, - _ => "OneDarkHalf".to_string(), - } + theme = value + .as_string() + .unwrap_or_else(|_| "OneDarkHalf".to_string()) } _ => (), }