Skip to content

Commit

Permalink
refactor(libmake): 🎨 used idiomatic error handling and removed unnece…
Browse files Browse the repository at this point in the history
…ssary clones by directly using the values
  • Loading branch information
sebastienrousseau committed Nov 6, 2023
1 parent 52b89c6 commit ff3ed50
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 58 deletions.
103 changes: 47 additions & 56 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -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<dyn Error>> {
// Extracting optional argument values from the parsed matches.
let author = matches.get_one::<String>("author");
let build = matches.get_one::<String>("build");
let categories = matches.get_one::<String>("categories");
let description = matches.get_one::<String>("description");
let documentation = matches.get_one::<String>("documentation");
let edition = matches.get_one::<String>("edition");
let email = matches.get_one::<String>("email");
let homepage = matches.get_one::<String>("homepage");
let keywords = matches.get_one::<String>("keywords");
let license = matches.get_one::<String>("license");
let name = matches.get_one::<String>("name");
let output = matches.get_one::<String>("output");
let readme = matches.get_one::<String>("readme");
let repository = matches.get_one::<String>("repository");
let rustversion = matches.get_one::<String>("rustversion");
let version = matches.get_one::<String>("version");
let website = matches.get_one::<String>("website");
let author = matches.get_one::<String>("author").cloned();
let build = matches.get_one::<String>("build").cloned();
let categories = matches.get_one::<String>("categories").cloned();
let description = matches.get_one::<String>("description").cloned();
let documentation = matches.get_one::<String>("documentation").cloned();
let edition = matches.get_one::<String>("edition").cloned();
let email = matches.get_one::<String>("email").cloned();
let homepage = matches.get_one::<String>("homepage").cloned();
let keywords = matches.get_one::<String>("keywords").cloned();
let license = matches.get_one::<String>("license").cloned();
let name = matches.get_one::<String>("name").cloned();
let output = matches.get_one::<String>("output").cloned();
let readme = matches.get_one::<String>("readme").cloned();
let repository = matches.get_one::<String>("repository").cloned();
let rustversion = matches.get_one::<String>("rustversion").cloned();
let version = matches.get_one::<String>("version").cloned();
let website = matches.get_one::<String>("website").cloned();

// Check which subcommand was used and perform the corresponding action.
if matches.contains_id("csv") {
let csv_file_path = matches.get_one::<String>("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::<String>("yml")
{
generate_from_yaml(yaml_file_path)
.expect("Failed to generate the template files");
} else if let Some(json_file_path) =
matches.get_one::<String>("json")
{
generate_from_json(json_file_path)
.expect("Failed to generate the template files");
} else if let Some(toml_file_path) =
matches.get_one::<String>("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::<String>("yml") {
generate_from_yaml(yaml_file_path)?;
} else if let Some(json_file_path) = matches.get_one::<String>("json") {
generate_from_json(json_file_path)?;
} else if let Some(toml_file_path) = matches.get_one::<String>("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(())
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {

// 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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ff3ed50

Please sign in to comment.