diff --git a/src/args.rs b/src/args.rs index 8a60156..54b37d0 100644 --- a/src/args.rs +++ b/src/args.rs @@ -37,7 +37,7 @@ pub fn get_matches_for(args: Vec<&str>) -> Result> { 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(); m.occurrences_of("verbose") + sub.occurrences_of("verbose") } diff --git a/src/build/mod.rs b/src/build/mod.rs index a0629cd..f060e48 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -3,7 +3,7 @@ pub mod pandoc; use config::Config; -pub fn build_input(config: &Config, input: String) -> Result { - pandoc::render(&config, input); +pub fn build_input(config: Config, input: String) -> Result { + pandoc::render(config, input); return Ok("".into()); } diff --git a/src/build/pandoc.rs b/src/build/pandoc.rs index 401201b..3801117 100644 --- a/src/build/pandoc.rs +++ b/src/build/pandoc.rs @@ -1,11 +1,11 @@ use config::Config; use pandoc::Pandoc; -fn build_pandoc(config: &Config) -> Pandoc { +fn build_pandoc(config: Config) -> Pandoc { return Pandoc::new(); } -pub fn render(config: &Config, input: String) { +pub fn render(config: Config, input: String) { let renderer = build_pandoc(config); } diff --git a/src/config/mod.rs b/src/config/mod.rs index 9be3d58..1ca2c5c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -10,7 +10,7 @@ pub mod consts; pub mod validate_types; -#[derive(Debug, Serialize, Deserialize, Default)] +#[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct Config { pub input: Vec, pub output: HashMap, @@ -21,8 +21,8 @@ pub struct Config { impl Config { fn new(raw: Value) -> Config { return Config { - input: read::get_input_files(&raw), - output: read::get_output_files(&raw), + input: read::get_input_files(raw.clone()), + output: read::get_output_files(raw.clone()), title: read::get_string(&raw, "title"), ..Default::default() }; @@ -32,8 +32,8 @@ impl Config { pub fn get_config() -> Result { let config_str = try!(read::read()); - let config = + let config: Value = try!(result_prefix(serde_yaml::from_str(&config_str), "Config Parse Error".into())); - try!(result_prefix(validate::validate(&config), "Config Validation Error".into())); + try!(result_prefix(validate::validate(config.clone()), "Config Validation Error".into())); return Ok(Config::new(config)); } diff --git a/src/config/read.rs b/src/config/read.rs index 559766f..1166742 100644 --- a/src/config/read.rs +++ b/src/config/read.rs @@ -39,14 +39,14 @@ pub fn get_string(conf: &Value, key: &str) -> String { } -pub fn get_input_files(conf: &Value) -> Vec { +pub fn get_input_files(conf: Value) -> Vec { let working_dir = current_dir().unwrap(); let input_values = conf.get("input").unwrap().as_sequence().unwrap().to_vec(); return input_values.into_iter().map(|x| working_dir.join(to_string(&x))).collect(); } -pub fn get_output_files(conf: &Value) -> HashMap { +pub fn get_output_files(conf: Value) -> HashMap { let working_dir = current_dir().unwrap(); let output_raw = conf.get("output").unwrap().as_mapping().unwrap(); let mut output_map: HashMap = HashMap::new(); diff --git a/src/config/validate.rs b/src/config/validate.rs index 73cd467..19f1f90 100644 --- a/src/config/validate.rs +++ b/src/config/validate.rs @@ -7,7 +7,7 @@ use config::read; use config::validate_types::check_config_types; -fn check_required_keys(config: &Value) -> ValidationResult { +fn check_required_keys(config: Value) -> ValidationResult { for key in vec!["input", "output", "title"].iter() { if config.get(key).is_none() { return Err(format!("Missing required key {}.", key)); @@ -16,7 +16,7 @@ fn check_required_keys(config: &Value) -> ValidationResult { return Ok(()); } -fn check_input_files(config: &Value) -> ValidationResult { +fn check_input_files(config: Value) -> ValidationResult { let files = read::get_input_files(config); for file in files.iter() { @@ -27,7 +27,7 @@ fn check_input_files(config: &Value) -> ValidationResult { return Ok(()); } -fn check_output_files(config: &Value) -> ValidationResult { +fn check_output_files(config: Value) -> ValidationResult { let files = read::get_output_files(config); let output_types = vec!["pdf".into()]; for file_def in files.iter() { @@ -42,18 +42,15 @@ fn check_output_files(config: &Value) -> ValidationResult { return Ok(()); } -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() { - try!(func(config)); + try!(func(config.clone())); } return Ok(()); } -pub fn validate(config: &Value) -> ValidationResult { +pub fn validate(config: Value) -> ValidationResult { return unwrap_group( config, vec![&check_required_keys, &check_config_types, &check_input_files, &check_output_files] diff --git a/src/config/validate_types.rs b/src/config/validate_types.rs index 7f72324..26b1f0e 100644 --- a/src/config/validate_types.rs +++ b/src/config/validate_types.rs @@ -2,14 +2,14 @@ use config::validate::{unwrap_group, ValidationResult}; use serde_yaml::Value; -fn check_root(config: &Value) -> ValidationResult { +fn check_root(config: Value) -> ValidationResult { if !config.is_mapping() { return Err("Config should be a mapping".into()); } return Ok(()); } -fn check_input(config: &Value) -> ValidationResult { +fn check_input(config: Value) -> ValidationResult { let input = config.get("input").unwrap(); if !input.is_sequence() { return Err("Input must be sequence".into()); @@ -27,7 +27,7 @@ fn check_input(config: &Value) -> ValidationResult { return Ok(()); } -fn check_output(config: &Value) -> ValidationResult { +fn check_output(config: Value) -> ValidationResult { let output = config.get("output").unwrap(); if !output.is_mapping() { return Err("Output must be mapping".into()); @@ -48,7 +48,7 @@ fn check_output(config: &Value) -> ValidationResult { return Ok(()); } -fn check_title(config: &Value) -> ValidationResult { +fn check_title(config: Value) -> ValidationResult { if !config.get("title").unwrap().is_string() { return Err("Title should be a string".into()); } @@ -56,6 +56,6 @@ fn check_title(config: &Value) -> ValidationResult { } -pub fn check_config_types(config: &Value) -> ValidationResult { +pub fn check_config_types(config: Value) -> ValidationResult { return unwrap_group(config, vec![&check_root, &check_input, &check_output, &check_title]); } diff --git a/src/main.rs b/src/main.rs index 6f2b588..aa28e86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ fn ok_or_exit(res: Result) -> T { }; } -fn get_config(args: &ArgMatches) -> Config { +fn get_config(args: ArgMatches) -> Config { let mut config = ok_or_exit(config::get_config()); config.verbosity = args::get_verbose(args); return config; @@ -45,7 +45,7 @@ fn main() { match subcommand { "build" => { - let config = get_config(&args); + let config = get_config(args.clone()); ok_or_exit(process::build(config)); } cmd => { diff --git a/src/process.rs b/src/process.rs index 1c84228..3714b15 100644 --- a/src/process.rs +++ b/src/process.rs @@ -6,6 +6,6 @@ use build::build_input; pub fn build(config: Config) -> Result<(), String> { let input = try!(read_input_files(config.input.clone())); println!("{}", input); - build_input(&config, input); + build_input(config.clone(), input); return Ok(()); } diff --git a/src/tests/args.rs b/src/tests/args.rs index e3571e8..fdad971 100644 --- a/src/tests/args.rs +++ b/src/tests/args.rs @@ -16,7 +16,7 @@ fn incorrect_subcommand() { #[test] fn verbose_number() { fn get_verbose_level(arg_list: Vec<&str>) -> u64 { - 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);