From ff3ed50a44a75c3168bbd2782f86c2666474bff4 Mon Sep 17 00:00:00 2001 From: Sebastien Rousseau Date: Mon, 6 Nov 2023 23:37:30 +0000 Subject: [PATCH] refactor(libmake): :art: used idiomatic error handling and removed unnecessary clones by directly using the values --- src/args.rs | 103 +++++++++++++++++++++------------------------ src/lib.rs | 2 +- tests/test_args.rs | 2 +- 3 files changed, 49 insertions(+), 58 deletions(-) diff --git a/src/args.rs b/src/args.rs index 876dd33..8f99bfd 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,15 +1,15 @@ + // Copyright © 2023 LibMake. All rights reserved. // SPDX-License-Identifier: Apache-2.0 OR MIT use clap::ArgMatches; - use crate::generator::{ generate_from_json, generate_from_toml, generate_from_yaml, }; - use super::generator::{ generate_files, generate_from_csv, FileGenerationParams, }; +use std::error::Error; /// Processes the command line arguments provided to the program. /// @@ -18,72 +18,63 @@ use super::generator::{ /// * `matches` - An instance of `clap::ArgMatches` containing the /// parsed command line arguments. /// -pub fn process_arguments(matches: &ArgMatches) { +pub fn process_arguments(matches: &ArgMatches) -> Result<(), Box> { // Extracting optional argument values from the parsed matches. - let author = matches.get_one::("author"); - let build = matches.get_one::("build"); - let categories = matches.get_one::("categories"); - let description = matches.get_one::("description"); - let documentation = matches.get_one::("documentation"); - let edition = matches.get_one::("edition"); - let email = matches.get_one::("email"); - let homepage = matches.get_one::("homepage"); - let keywords = matches.get_one::("keywords"); - let license = matches.get_one::("license"); - let name = matches.get_one::("name"); - let output = matches.get_one::("output"); - let readme = matches.get_one::("readme"); - let repository = matches.get_one::("repository"); - let rustversion = matches.get_one::("rustversion"); - let version = matches.get_one::("version"); - let website = matches.get_one::("website"); + let author = matches.get_one::("author").cloned(); + let build = matches.get_one::("build").cloned(); + let categories = matches.get_one::("categories").cloned(); + let description = matches.get_one::("description").cloned(); + let documentation = matches.get_one::("documentation").cloned(); + let edition = matches.get_one::("edition").cloned(); + let email = matches.get_one::("email").cloned(); + let homepage = matches.get_one::("homepage").cloned(); + let keywords = matches.get_one::("keywords").cloned(); + let license = matches.get_one::("license").cloned(); + let name = matches.get_one::("name").cloned(); + let output = matches.get_one::("output").cloned(); + let readme = matches.get_one::("readme").cloned(); + let repository = matches.get_one::("repository").cloned(); + let rustversion = matches.get_one::("rustversion").cloned(); + let version = matches.get_one::("version").cloned(); + let website = matches.get_one::("website").cloned(); // Check which subcommand was used and perform the corresponding action. if matches.contains_id("csv") { let csv_file_path = matches.get_one::("csv").unwrap(); - generate_from_csv(csv_file_path) - .expect("Failed to generate the template files"); - } else if let Some(yaml_file_path) = - matches.get_one::("yml") - { - generate_from_yaml(yaml_file_path) - .expect("Failed to generate the template files"); - } else if let Some(json_file_path) = - matches.get_one::("json") - { - generate_from_json(json_file_path) - .expect("Failed to generate the template files"); - } else if let Some(toml_file_path) = - matches.get_one::("toml") - { - generate_from_toml(toml_file_path) - .expect("Failed to generate the template files"); + generate_from_csv(csv_file_path)?; + } else if let Some(yaml_file_path) = matches.get_one::("yml") { + generate_from_yaml(yaml_file_path)?; + } else if let Some(json_file_path) = matches.get_one::("json") { + generate_from_json(json_file_path)?; + } else if let Some(toml_file_path) = matches.get_one::("toml") { + generate_from_toml(toml_file_path)?; } else if !matches.args_present() { // If no subcommand is used and there are additional arguments, // create a parameter struct and generate files. let params = FileGenerationParams { - author: author.cloned(), - build: build.cloned(), - categories: categories.cloned(), - description: description.cloned(), - documentation: documentation.cloned(), - edition: edition.cloned(), - email: email.cloned(), - homepage: homepage.cloned(), - keywords: keywords.cloned(), - license: license.cloned(), - name: name.cloned(), - output: output.cloned(), - readme: readme.cloned(), - repository: repository.cloned(), - rustversion: rustversion.cloned(), - version: version.cloned(), - website: website.cloned(), + author, + build, + categories, + description, + documentation, + edition, + email, + homepage, + keywords, + license, + name, + output, + readme, + repository, + rustversion, + version, + website, }; - generate_files(params) - .expect("Failed to generate the template files"); + generate_files(params)?; println!("\n\nTemplate files generated successfully!"); } else { println!("❌ No arguments provided. Please provide the required arguments to generate the template files."); } + + Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 4312083..b28e412 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,7 +118,7 @@ pub fn run() -> Result<(), Box> { // Build the command-line interface and process the arguments let matches = cli::build_cli()?; - args::process_arguments(&matches); + args::process_arguments(&matches)?; // Check the number of arguments, provide a welcome message if no arguments were passed if std::env::args().len() == 1 { diff --git a/tests/test_args.rs b/tests/test_args.rs index dfbd532..33716f2 100644 --- a/tests/test_args.rs +++ b/tests/test_args.rs @@ -105,7 +105,7 @@ mod tests { Some(&file_path.to_string()) ); - process_arguments(&matches); + process_arguments(&matches).unwrap(); // Check that the files were generated let expected_files = vec![ "Cargo.toml",