Format with rustfmt
This commit is contained in:
parent
6b94a298c0
commit
37566249a7
6 changed files with 38 additions and 38 deletions
19
src/args.rs
19
src/args.rs
|
@ -4,7 +4,7 @@ use clap::{App, AppSettings, ArgMatches, Arg, SubCommand};
|
||||||
use clap::Result;
|
use clap::Result;
|
||||||
|
|
||||||
fn get_build_command() -> App<'static, 'static> {
|
fn get_build_command() -> App<'static, 'static> {
|
||||||
return SubCommand::with_name("build")
|
return SubCommand::with_name("build");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build() -> App<'static, 'static> {
|
fn build() -> App<'static, 'static> {
|
||||||
|
@ -16,12 +16,13 @@ fn build() -> App<'static, 'static> {
|
||||||
.global_setting(AppSettings::VersionlessSubcommands)
|
.global_setting(AppSettings::VersionlessSubcommands)
|
||||||
.global_setting(AppSettings::ColoredHelp)
|
.global_setting(AppSettings::ColoredHelp)
|
||||||
.global_setting(AppSettings::StrictUtf8)
|
.global_setting(AppSettings::StrictUtf8)
|
||||||
.arg(Arg::with_name("verbose")
|
.arg(
|
||||||
.global(true)
|
Arg::with_name("verbose")
|
||||||
.short("v")
|
.global(true)
|
||||||
.long("verbose")
|
.short("v")
|
||||||
.help("Show verbose output")
|
.long("verbose")
|
||||||
.multiple(true)
|
.help("Show verbose output")
|
||||||
|
.multiple(true)
|
||||||
)
|
)
|
||||||
.subcommand(get_build_command());
|
.subcommand(get_build_command());
|
||||||
}
|
}
|
||||||
|
@ -32,11 +33,11 @@ pub fn get_matches() -> ArgMatches<'static> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn get_matches_for(args : Vec<&str>) -> Result<ArgMatches<'static>> {
|
pub fn get_matches_for(args: Vec<&str>) -> Result<ArgMatches<'static>> {
|
||||||
return build().get_matches_from_safe(args);
|
return build().get_matches_from_safe(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_verbose(m : ArgMatches) -> u64 {
|
pub fn get_verbose(m: ArgMatches) -> u64 {
|
||||||
let sub = m.subcommand_matches(&m.subcommand_name().unwrap()).unwrap();
|
let sub = m.subcommand_matches(&m.subcommand_name().unwrap()).unwrap();
|
||||||
m.occurrences_of("verbose") + sub.occurrences_of("verbose")
|
m.occurrences_of("verbose") + sub.occurrences_of("verbose")
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,7 @@ pub mod consts;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
input: Vec<PathBuf>,
|
input: Vec<PathBuf>
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
|
@ -25,5 +25,8 @@ pub fn read() -> String {
|
||||||
pub fn get_input_files(conf: &Value) -> Vec<PathBuf> {
|
pub fn get_input_files(conf: &Value) -> Vec<PathBuf> {
|
||||||
let working_dir = current_dir().unwrap();
|
let working_dir = current_dir().unwrap();
|
||||||
let input_values = conf.get("input").unwrap().as_sequence().unwrap().to_vec();
|
let input_values = conf.get("input").unwrap().as_sequence().unwrap().to_vec();
|
||||||
return input_values.into_iter().map(|x| working_dir.join(x.as_str().unwrap().to_string())).collect();
|
return input_values
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| working_dir.join(x.as_str().unwrap().to_string()))
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,7 @@ use config::read;
|
||||||
|
|
||||||
|
|
||||||
fn check_required_keys(config: &Value) -> ValidationResult {
|
fn check_required_keys(config: &Value) -> ValidationResult {
|
||||||
for key in vec!(
|
for key in vec!["input", "output", "title"].iter() {
|
||||||
"input",
|
|
||||||
"output",
|
|
||||||
"title"
|
|
||||||
).iter() {
|
|
||||||
if config.get(key).is_none() {
|
if config.get(key).is_none() {
|
||||||
return Err(format!("Missing required key {}.", key));
|
return Err(format!("Missing required key {}.", key));
|
||||||
}
|
}
|
||||||
|
@ -30,7 +26,10 @@ fn check_input_files(config: &Value) -> ValidationResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn unwrap_group(config: &Value, funcs: Vec<&Fn(&Value) -> ValidationResult>) -> ValidationResult {
|
pub fn unwrap_group(
|
||||||
|
config: &Value,
|
||||||
|
funcs: Vec<&Fn(&Value) -> ValidationResult>,
|
||||||
|
) -> ValidationResult {
|
||||||
for func in funcs.iter() {
|
for func in funcs.iter() {
|
||||||
let func_result = func(config);
|
let func_result = func(config);
|
||||||
if func_result.is_err() {
|
if func_result.is_err() {
|
||||||
|
@ -42,8 +41,5 @@ pub fn unwrap_group(config: &Value, funcs: Vec<&Fn(&Value) -> ValidationResult>)
|
||||||
|
|
||||||
|
|
||||||
pub fn validate(config: &Value) -> ValidationResult {
|
pub fn validate(config: &Value) -> ValidationResult {
|
||||||
return unwrap_group(config, vec!(
|
return unwrap_group(config, vec![&check_required_keys, &check_input_files]);
|
||||||
&check_required_keys,
|
|
||||||
&check_input_files
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#[macro_use] extern crate clap;
|
#[macro_use]
|
||||||
#[macro_use] extern crate serde_derive;
|
extern crate clap;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
|
||||||
extern crate serde_yaml;
|
extern crate serde_yaml;
|
||||||
|
|
||||||
|
@ -16,4 +18,3 @@ fn main() {
|
||||||
println!("{:?}", config::get_config());
|
println!("{:?}", config::get_config());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,11 @@ use clap::ErrorKind;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn incorrect_subcommand() {
|
fn incorrect_subcommand() {
|
||||||
let out = args::get_matches_for(vec!("mdp"));
|
let out = args::get_matches_for(vec!["mdp"]);
|
||||||
assert!(out.is_err());
|
assert!(out.is_err());
|
||||||
assert_eq!(out.unwrap_err().kind, ErrorKind::MissingArgumentOrSubcommand);
|
assert_eq!(out.unwrap_err().kind, ErrorKind::MissingArgumentOrSubcommand);
|
||||||
|
|
||||||
let out = args::get_matches_for(vec!("mdp", "invalid"));
|
let out = args::get_matches_for(vec!["mdp", "invalid"]);
|
||||||
assert!(out.is_err());
|
assert!(out.is_err());
|
||||||
assert_eq!(out.unwrap_err().kind, ErrorKind::UnknownArgument);
|
assert_eq!(out.unwrap_err().kind, ErrorKind::UnknownArgument);
|
||||||
}
|
}
|
||||||
|
@ -19,20 +19,20 @@ fn verbose_number() {
|
||||||
return args::get_verbose(args::get_matches_for(arg_list).unwrap());
|
return args::get_verbose(args::get_matches_for(arg_list).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "build", "-v")), 1);
|
assert_eq!(get_verbose_level(vec!["mdp", "build", "-v"]), 1);
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "build", "-vv")), 2);
|
assert_eq!(get_verbose_level(vec!["mdp", "build", "-vv"]), 2);
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "-v", "build", "-vv")), 3);
|
assert_eq!(get_verbose_level(vec!["mdp", "-v", "build", "-vv"]), 3);
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "-vv", "build", "-v")), 3);
|
assert_eq!(get_verbose_level(vec!["mdp", "-vv", "build", "-v"]), 3);
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "-v", "build", "-v")), 2);
|
assert_eq!(get_verbose_level(vec!["mdp", "-v", "build", "-v"]), 2);
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "-v", "build")), 1);
|
assert_eq!(get_verbose_level(vec!["mdp", "-v", "build"]), 1);
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "--verbose", "build", "-v")), 2);
|
assert_eq!(get_verbose_level(vec!["mdp", "--verbose", "build", "-v"]), 2);
|
||||||
assert_eq!(get_verbose_level(vec!("mdp", "-v", "build", "--verbose")), 2);
|
assert_eq!(get_verbose_level(vec!["mdp", "-v", "build", "--verbose"]), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn build_subcommand() {
|
fn build_subcommand() {
|
||||||
let out = args::get_matches_for(vec!("mdp", "build"));
|
let out = args::get_matches_for(vec!["mdp", "build"]);
|
||||||
assert!(out.is_ok());
|
assert!(out.is_ok());
|
||||||
assert_eq!(out.unwrap().subcommand_name().unwrap(), "build");
|
assert_eq!(out.unwrap().subcommand_name().unwrap(), "build");
|
||||||
}
|
}
|
||||||
|
@ -40,11 +40,11 @@ fn build_subcommand() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn version_string() {
|
fn version_string() {
|
||||||
let out = args::get_matches_for(vec!("mdp", "--version"));
|
let out = args::get_matches_for(vec!["mdp", "--version"]);
|
||||||
assert!(out.is_err());
|
assert!(out.is_err());
|
||||||
assert_eq!(out.unwrap_err().kind, ErrorKind::VersionDisplayed);
|
assert_eq!(out.unwrap_err().kind, ErrorKind::VersionDisplayed);
|
||||||
|
|
||||||
let out = args::get_matches_for(vec!("mdp", "build", "--version"));
|
let out = args::get_matches_for(vec!["mdp", "build", "--version"]);
|
||||||
assert!(out.is_err());
|
assert!(out.is_err());
|
||||||
assert_eq!(out.unwrap_err().kind, ErrorKind::UnknownArgument);
|
assert_eq!(out.unwrap_err().kind, ErrorKind::UnknownArgument);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue