From 6b299cee0ff72eb56f96565ebed52cc87cc3649f Mon Sep 17 00:00:00 2001 From: Catherine Noll Date: Wed, 23 Nov 2022 13:36:50 -0500 Subject: [PATCH] Improvements from code review --- src/main.rs | 2 +- src/subcommands/doctor/less.rs | 28 ++++++++++---------- src/subcommands/doctor/report.rs | 44 +++++++++++++++++--------------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index 57375182c..7aeac9b08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,7 +110,7 @@ fn run_app() -> std::io::Result { } else if opt.parse_ansi { Some(subcommands::parse_ansi::parse_ansi()) } else if opt.doctor { - Some(subcommands::doctor::report::run_diagnostics()) + Some(subcommands::doctor::report::print_doctor_report()) } else { None }; diff --git a/src/subcommands/doctor/less.rs b/src/subcommands/doctor/less.rs index c83c062a4..b196d4162 100644 --- a/src/subcommands/doctor/less.rs +++ b/src/subcommands/doctor/less.rs @@ -27,23 +27,21 @@ impl Less { impl Diagnostic for Less { fn report(&self) -> (String, bool) { match self.version { - Some(n) => match n < self.min_version { - true => ( - format!( - "`less` version >= {} is required (your version: {})", - MIN_LESS_VERSION, n - ), - false, + Some(n) if n < self.min_version => ( + format!( + "`less` version >= {} is required (your version: {})", + MIN_LESS_VERSION, n ), - false => ( - format!( - "`less` version >= {} is required (your version: {})", - MIN_LESS_VERSION, n - ), - true, + false, + ), + Some(n) if n >= self.min_version => ( + format!( + "`less` version >= {} is required (your version: {})", + MIN_LESS_VERSION, n ), - }, - None => ("`less` version >= {} is required".to_string(), false), + true, + ), + _ => ("`less` version >= {} is required".to_string(), false), } } diff --git a/src/subcommands/doctor/report.rs b/src/subcommands/doctor/report.rs index 4c053e431..54a26c4cc 100644 --- a/src/subcommands/doctor/report.rs +++ b/src/subcommands/doctor/report.rs @@ -5,38 +5,22 @@ use crate::subcommands::doctor::shared::Diagnostic; use tabled::{Style, Table, Tabled}; #[cfg(not(tarpaulin_include))] -pub fn run_diagnostics() -> std::io::Result<()> { +pub fn print_doctor_report() -> std::io::Result<()> { let diagnostics: Vec> = vec![ Box::new(Less::probe()), Box::new(GitConfigEnvVars::probe()), Box::new(PagerEnvVars::probe()), ]; - let mut reports = Vec::new(); - for d in diagnostics { - reports.push(build_report_row(d)); - } + let reports = diagnostics + .into_iter() + .map(|d| Report::from_diagnostic(d)) + .collect(); print_table(reports); Ok(()) } -fn build_report_row(diagnostic: Box) -> Report { - let remedy = diagnostic.remedy(); - match remedy { - Some(r) => Report { - result: "❌".to_string(), - description: diagnostic.report().0, - remedy: r, - }, - None => Report { - result: "✅".to_string(), - description: diagnostic.report().0, - remedy: "".to_string(), - }, - } -} - #[derive(Tabled)] struct Report { result: String, @@ -44,6 +28,24 @@ struct Report { remedy: String, } +impl Report { + pub fn from_diagnostic(diagnostic: Box) -> Self { + let remedy = diagnostic.remedy(); + match remedy { + Some(r) => Report { + result: "❌".to_string(), + description: diagnostic.report().0, + remedy: r, + }, + None => Report { + result: "✅".to_string(), + description: diagnostic.report().0, + remedy: "".to_string(), + }, + } + } +} + fn print_table(report: Vec) { let mut table = Table::new(&report); table.with(Style::modern());