Clone rather than reference
This commit is contained in:
parent
6ae9459230
commit
83a1b082cd
10 changed files with 27 additions and 30 deletions
|
@ -37,7 +37,7 @@ pub fn get_matches_for(args: Vec<&str>) -> Result<ArgMatches<'static>> {
|
|||
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")
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ pub mod pandoc;
|
|||
use config::Config;
|
||||
|
||||
|
||||
pub fn build_input(config: &Config, input: String) -> Result<String, String> {
|
||||
pandoc::render(&config, input);
|
||||
pub fn build_input(config: Config, input: String) -> Result<String, String> {
|
||||
pandoc::render(config, input);
|
||||
return Ok("".into());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<PathBuf>,
|
||||
pub output: HashMap<String, PathBuf>,
|
||||
|
@ -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<Config, String> {
|
||||
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));
|
||||
}
|
||||
|
|
|
@ -39,14 +39,14 @@ pub fn get_string(conf: &Value, key: &str) -> 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 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<String, PathBuf> {
|
||||
pub fn get_output_files(conf: Value) -> HashMap<String, PathBuf> {
|
||||
let working_dir = current_dir().unwrap();
|
||||
let output_raw = conf.get("output").unwrap().as_mapping().unwrap();
|
||||
let mut output_map: HashMap<String, PathBuf> = HashMap::new();
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ fn ok_or_exit<T>(res: Result<T, String>) -> 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 => {
|
||||
|
|
|
@ -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(());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Reference in a new issue