Skip to content

Commit

Permalink
refactor: move AppCommand to config module
Browse files Browse the repository at this point in the history
Signed-off-by: Ismael González <[email protected]>
  • Loading branch information
ismaelgv committed Dec 6, 2021
1 parent f462368 commit 52bbe72
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 70 deletions.
96 changes: 28 additions & 68 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppCommand, String> {
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> {
Expand Down Expand Up @@ -124,66 +105,45 @@ 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::<usize>() {
Ok(_) => Ok(()),
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() {
Some(_) => Ok(()),
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();
}
}
45 changes: 44 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<AppCommand, String> {
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,
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/fileutils.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down

0 comments on commit 52bbe72

Please sign in to comment.