Skip to content

Commit

Permalink
Improvements from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
clnoll authored and dandavison committed Jul 29, 2023
1 parent e45079e commit f52d74c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn run_app() -> std::io::Result<i32> {
} 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
};
Expand Down
28 changes: 13 additions & 15 deletions src/subcommands/doctor/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
44 changes: 23 additions & 21 deletions src/subcommands/doctor/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,47 @@ use crate::subcommands::doctor::shared::Diagnostic;
use tabled::{settings::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<Box<dyn Diagnostic>> = 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<dyn Diagnostic>) -> 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,
description: String,
remedy: String,
}

impl Report {
pub fn from_diagnostic(diagnostic: Box<dyn Diagnostic>) -> 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<Report>) {
let mut table = Table::new(&report);
table.with(Style::modern());
Expand Down

0 comments on commit f52d74c

Please sign in to comment.