From 33f8d03d6beb7ad0cc83443f8652e2e9eb501a6d Mon Sep 17 00:00:00 2001 From: David Peter Date: Sun, 6 Feb 2022 18:22:55 +0100 Subject: [PATCH] Move build_export_manager to ExportManager --- src/export/mod.rs | 20 ++++++++++++++++++++ src/main.rs | 23 ++--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/export/mod.rs b/src/export/mod.rs index 1c72c7d..9b6beee 100644 --- a/src/export/mod.rs +++ b/src/export/mod.rs @@ -15,6 +15,7 @@ use crate::benchmark_result::BenchmarkResult; use crate::units::Unit; use anyhow::{Context, Result}; +use clap::ArgMatches; /// The desired form of exporter to use for a given file. #[derive(Clone)] @@ -50,6 +51,25 @@ pub struct ExportManager { } impl ExportManager { + /// Build the ExportManager that will export the results specified + /// in the given ArgMatches + pub fn from_cli_arguments(matches: &ArgMatches) -> Result { + let mut export_manager = Self::default(); + { + let mut add_exporter = |flag, exporttype| -> Result<()> { + if let Some(filename) = matches.value_of(flag) { + export_manager.add_exporter(exporttype, filename)?; + } + Ok(()) + }; + add_exporter("export-asciidoc", ExportType::Asciidoc)?; + add_exporter("export-json", ExportType::Json)?; + add_exporter("export-csv", ExportType::Csv)?; + add_exporter("export-markdown", ExportType::Markdown)?; + } + Ok(export_manager) + } + /// Add an additional exporter to the ExportManager pub fn add_exporter(&mut self, export_type: ExportType, filename: &str) -> Result<()> { let _ = File::create(filename) diff --git a/src/main.rs b/src/main.rs index a3755d8..819f0e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ use benchmark::{mean_shell_spawning_time, run_benchmark}; use benchmark_result::BenchmarkResult; use command::Command; use error::OptionsError; -use export::{ExportManager, ExportType}; +use export::ExportManager; use options::{Options, OutputStyleOption}; use parameter_range::get_parameterized_commands; use tokenize::tokenize; @@ -118,7 +118,7 @@ fn run() -> Result<()> { let matches = get_arg_matches(env::args_os()); let options = Options::from_cli_arguments(&matches)?; let commands = build_commands(&matches)?; - let export_manager = build_export_manager(&matches)?; + let export_manager = ExportManager::from_cli_arguments(&matches)?; run_benchmarks_and_print_comparison(&commands, &options, &export_manager) } @@ -133,25 +133,6 @@ fn main() { } } -/// Build the ExportManager that will export the results specified -/// in the given ArgMatches -fn build_export_manager(matches: &ArgMatches) -> Result { - let mut export_manager = ExportManager::default(); - { - let mut add_exporter = |flag, exporttype| -> Result<()> { - if let Some(filename) = matches.value_of(flag) { - export_manager.add_exporter(exporttype, filename)?; - } - Ok(()) - }; - add_exporter("export-asciidoc", ExportType::Asciidoc)?; - add_exporter("export-json", ExportType::Json)?; - add_exporter("export-csv", ExportType::Csv)?; - add_exporter("export-markdown", ExportType::Markdown)?; - } - Ok(export_manager) -} - /// Build the commands to benchmark fn build_commands(matches: &ArgMatches) -> Result> { let command_names = matches.values_of("command-name");