From 52bbe725e5f15e57c2052b313ddb71593a01c296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ismael=20Gonz=C3=A1lez?= Date: Mon, 6 Dec 2021 20:40:08 +0100 Subject: [PATCH] refactor: move AppCommand to config module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ismael González --- src/app.rs | 96 ++++++++++++++---------------------------------- src/config.rs | 45 ++++++++++++++++++++++- src/fileutils.rs | 2 +- 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/src/app.rs b/src/app.rs index 3a6bef6..dfe645d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,29 +2,10 @@ use clap::{App, AppSettings, Arg, SubCommand}; use std::ffi::{OsStr, OsString}; /// From file subcommand name. -const FROM_FILE_SUBCOMMAND: &str = "from-file"; +pub const FROM_FILE_SUBCOMMAND: &str = "from-file"; /// To ASCII subcommand name. -const TO_ASCII_SUBCOMMAND: &str = "to-ascii"; - -/// Application commands -#[derive(Debug, PartialEq)] -pub enum AppCommand { - Root, - FromFile, - ToASCII, -} - -impl AppCommand { - pub fn from_str(name: &str) -> Result { - match name { - "" => Ok(AppCommand::Root), - FROM_FILE_SUBCOMMAND => Ok(AppCommand::FromFile), - TO_ASCII_SUBCOMMAND => Ok(AppCommand::ToASCII), - _ => Err(format!("Non-registered subcommand '{}'", name)), - } - } -} +pub const TO_ASCII_SUBCOMMAND: &str = "to-ascii"; /// Create application using clap. It sets all options and command-line help. pub fn create_app<'a>() -> App<'a, 'a> { @@ -124,31 +105,33 @@ pub fn create_app<'a>() -> App<'a, 'a> { ) .args(&common_args) .args(&path_args) - .subcommand(SubCommand::with_name(FROM_FILE_SUBCOMMAND) - .args(&common_args) - .arg( - Arg::with_name("DUMPFILE") - .takes_value(true) - .required(true) - .value_name("DUMPFILE") - .validator_os(is_valid_string) - .index(1), - ) - .arg( - Arg::with_name("undo") - .long("undo") - .short("u") - .help("Undo the operations from the dump file"), - ) - .about("Read operations from a dump file"), + .subcommand( + SubCommand::with_name(FROM_FILE_SUBCOMMAND) + .args(&common_args) + .arg( + Arg::with_name("DUMPFILE") + .takes_value(true) + .required(true) + .value_name("DUMPFILE") + .validator_os(is_valid_string) + .index(1), + ) + .arg( + Arg::with_name("undo") + .long("undo") + .short("u") + .help("Undo the operations from the dump file"), + ) + .about("Read operations from a dump file"), + ) + .subcommand( + SubCommand::with_name(TO_ASCII_SUBCOMMAND) + .args(&common_args) + .args(&path_args) + .about("Replace file name UTF-8 chars with ASCII chars representation."), ) - .subcommand(SubCommand::with_name(TO_ASCII_SUBCOMMAND) - .args(&common_args) - .args(&path_args) - .about("Replace file name UTF-8 chars with ASCII chars representation.") - ) } -#[allow(clippy::all)] + /// Check if the input provided is valid unsigned integer fn is_integer(arg_value: String) -> Result<(), String> { match arg_value.parse::() { @@ -156,6 +139,7 @@ fn is_integer(arg_value: String) -> Result<(), String> { Err(_) => Err("Value provided is not an integer".to_string()), } } + /// Check if the input provided is valid UTF-8 fn is_valid_string(os_str: &OsStr) -> Result<(), OsString> { match os_str.to_str() { @@ -163,27 +147,3 @@ fn is_valid_string(os_str: &OsStr) -> Result<(), OsString> { None => Err(OsString::from("Value provided is not a valid UTF-8 string")), } } - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn app_command_from_str() { - assert_eq!(AppCommand::from_str("").unwrap(), AppCommand::Root); - assert_eq!( - AppCommand::from_str(FROM_FILE_SUBCOMMAND).unwrap(), - AppCommand::FromFile - ); - assert_eq!( - AppCommand::from_str(TO_ASCII_SUBCOMMAND).unwrap(), - AppCommand::ToASCII - ); - } - - #[test] - #[should_panic] - fn app_command_from_str_unknown_error() { - AppCommand::from_str("this-command-does-not-exists").unwrap(); - } -} diff --git a/src/config.rs b/src/config.rs index 8748cd7..6432a0d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use app::{create_app, AppCommand}; +use app::{create_app, FROM_FILE_SUBCOMMAND, TO_ASCII_SUBCOMMAND}; use atty; use clap::ArgMatches; use output::Printer; @@ -50,6 +50,25 @@ pub enum ReplaceMode { ToASCII, } +/// Application commands +#[derive(Debug, PartialEq)] +pub enum AppCommand { + Root, + FromFile, + ToASCII, +} + +impl AppCommand { + pub fn from_str(name: &str) -> Result { + match name { + "" => Ok(AppCommand::Root), + FROM_FILE_SUBCOMMAND => Ok(AppCommand::FromFile), + TO_ASCII_SUBCOMMAND => Ok(AppCommand::ToASCII), + _ => Err(format!("Non-registered subcommand '{}'", name)), + } + } +} + struct ArgumentParser<'a> { matches: &'a ArgMatches<'a>, printer: &'a Printer, @@ -195,3 +214,27 @@ fn detect_output_color() -> Printer { Printer::no_color() } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn app_command_from_str() { + assert_eq!(AppCommand::from_str("").unwrap(), AppCommand::Root); + assert_eq!( + AppCommand::from_str(FROM_FILE_SUBCOMMAND).unwrap(), + AppCommand::FromFile + ); + assert_eq!( + AppCommand::from_str(TO_ASCII_SUBCOMMAND).unwrap(), + AppCommand::ToASCII + ); + } + + #[test] + #[should_panic] + fn app_command_from_str_unknown_error() { + AppCommand::from_str("this-command-does-not-exists").unwrap(); + } +} diff --git a/src/fileutils.rs b/src/fileutils.rs index 389255a..8d8a554 100644 --- a/src/fileutils.rs +++ b/src/fileutils.rs @@ -1,9 +1,9 @@ -use std::path::Path; use config::RunMode; use error::*; use path_abs::PathAbs; use std::collections::HashMap; use std::fs; +use std::path::Path; use std::path::PathBuf; use walkdir::{DirEntry, WalkDir};