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);
|
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")
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ pub mod pandoc;
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
|
|
||||||
pub fn build_input(config: &Config, input: String) -> Result<String, String> {
|
pub fn build_input(config: Config, input: String) -> Result<String, String> {
|
||||||
pandoc::render(&config, input);
|
pandoc::render(config, input);
|
||||||
return Ok("".into());
|
return Ok("".into());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use pandoc::Pandoc;
|
use pandoc::Pandoc;
|
||||||
|
|
||||||
fn build_pandoc(config: &Config) -> Pandoc {
|
fn build_pandoc(config: Config) -> Pandoc {
|
||||||
return Pandoc::new();
|
return Pandoc::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn render(config: &Config, input: String) {
|
pub fn render(config: Config, input: String) {
|
||||||
let renderer = build_pandoc(config);
|
let renderer = build_pandoc(config);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ pub mod consts;
|
||||||
pub mod validate_types;
|
pub mod validate_types;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub input: Vec<PathBuf>,
|
pub input: Vec<PathBuf>,
|
||||||
pub output: HashMap<String, PathBuf>,
|
pub output: HashMap<String, PathBuf>,
|
||||||
|
@ -21,8 +21,8 @@ pub struct Config {
|
||||||
impl Config {
|
impl Config {
|
||||||
fn new(raw: Value) -> Config {
|
fn new(raw: Value) -> Config {
|
||||||
return Config {
|
return Config {
|
||||||
input: read::get_input_files(&raw),
|
input: read::get_input_files(raw.clone()),
|
||||||
output: read::get_output_files(&raw),
|
output: read::get_output_files(raw.clone()),
|
||||||
title: read::get_string(&raw, "title"),
|
title: read::get_string(&raw, "title"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
@ -32,8 +32,8 @@ impl Config {
|
||||||
|
|
||||||
pub fn get_config() -> Result<Config, String> {
|
pub fn get_config() -> Result<Config, String> {
|
||||||
let config_str = try!(read::read());
|
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(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));
|
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 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(to_string(&x))).collect();
|
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 working_dir = current_dir().unwrap();
|
||||||
let output_raw = conf.get("output").unwrap().as_mapping().unwrap();
|
let output_raw = conf.get("output").unwrap().as_mapping().unwrap();
|
||||||
let mut output_map: HashMap<String, PathBuf> = HashMap::new();
|
let mut output_map: HashMap<String, PathBuf> = HashMap::new();
|
||||||
|
|
|
@ -7,7 +7,7 @@ use config::read;
|
||||||
use config::validate_types::check_config_types;
|
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() {
|
for key in vec!["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));
|
||||||
|
@ -16,7 +16,7 @@ fn check_required_keys(config: &Value) -> ValidationResult {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_input_files(config: &Value) -> ValidationResult {
|
fn check_input_files(config: Value) -> ValidationResult {
|
||||||
let files = read::get_input_files(config);
|
let files = read::get_input_files(config);
|
||||||
|
|
||||||
for file in files.iter() {
|
for file in files.iter() {
|
||||||
|
@ -27,7 +27,7 @@ fn check_input_files(config: &Value) -> ValidationResult {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_output_files(config: &Value) -> ValidationResult {
|
fn check_output_files(config: Value) -> ValidationResult {
|
||||||
let files = read::get_output_files(config);
|
let files = read::get_output_files(config);
|
||||||
let output_types = vec!["pdf".into()];
|
let output_types = vec!["pdf".into()];
|
||||||
for file_def in files.iter() {
|
for file_def in files.iter() {
|
||||||
|
@ -42,18 +42,15 @@ fn check_output_files(config: &Value) -> ValidationResult {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unwrap_group(
|
pub fn unwrap_group(config: Value, funcs: Vec<&Fn(Value) -> ValidationResult>) -> ValidationResult {
|
||||||
config: &Value,
|
|
||||||
funcs: Vec<&Fn(&Value) -> ValidationResult>,
|
|
||||||
) -> ValidationResult {
|
|
||||||
for func in funcs.iter() {
|
for func in funcs.iter() {
|
||||||
try!(func(config));
|
try!(func(config.clone()));
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn validate(config: &Value) -> ValidationResult {
|
pub fn validate(config: Value) -> ValidationResult {
|
||||||
return unwrap_group(
|
return unwrap_group(
|
||||||
config,
|
config,
|
||||||
vec![&check_required_keys, &check_config_types, &check_input_files, &check_output_files]
|
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;
|
use serde_yaml::Value;
|
||||||
|
|
||||||
|
|
||||||
fn check_root(config: &Value) -> ValidationResult {
|
fn check_root(config: Value) -> ValidationResult {
|
||||||
if !config.is_mapping() {
|
if !config.is_mapping() {
|
||||||
return Err("Config should be a mapping".into());
|
return Err("Config should be a mapping".into());
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_input(config: &Value) -> ValidationResult {
|
fn check_input(config: Value) -> ValidationResult {
|
||||||
let input = config.get("input").unwrap();
|
let input = config.get("input").unwrap();
|
||||||
if !input.is_sequence() {
|
if !input.is_sequence() {
|
||||||
return Err("Input must be sequence".into());
|
return Err("Input must be sequence".into());
|
||||||
|
@ -27,7 +27,7 @@ fn check_input(config: &Value) -> ValidationResult {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_output(config: &Value) -> ValidationResult {
|
fn check_output(config: Value) -> ValidationResult {
|
||||||
let output = config.get("output").unwrap();
|
let output = config.get("output").unwrap();
|
||||||
if !output.is_mapping() {
|
if !output.is_mapping() {
|
||||||
return Err("Output must be mapping".into());
|
return Err("Output must be mapping".into());
|
||||||
|
@ -48,7 +48,7 @@ fn check_output(config: &Value) -> ValidationResult {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_title(config: &Value) -> ValidationResult {
|
fn check_title(config: Value) -> ValidationResult {
|
||||||
if !config.get("title").unwrap().is_string() {
|
if !config.get("title").unwrap().is_string() {
|
||||||
return Err("Title should be a string".into());
|
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]);
|
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());
|
let mut config = ok_or_exit(config::get_config());
|
||||||
config.verbosity = args::get_verbose(args);
|
config.verbosity = args::get_verbose(args);
|
||||||
return config;
|
return config;
|
||||||
|
@ -45,7 +45,7 @@ fn main() {
|
||||||
|
|
||||||
match subcommand {
|
match subcommand {
|
||||||
"build" => {
|
"build" => {
|
||||||
let config = get_config(&args);
|
let config = get_config(args.clone());
|
||||||
ok_or_exit(process::build(config));
|
ok_or_exit(process::build(config));
|
||||||
}
|
}
|
||||||
cmd => {
|
cmd => {
|
||||||
|
|
|
@ -6,6 +6,6 @@ use build::build_input;
|
||||||
pub fn build(config: Config) -> Result<(), String> {
|
pub fn build(config: Config) -> Result<(), String> {
|
||||||
let input = try!(read_input_files(config.input.clone()));
|
let input = try!(read_input_files(config.input.clone()));
|
||||||
println!("{}", input);
|
println!("{}", input);
|
||||||
build_input(&config, input);
|
build_input(config.clone(), input);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ fn incorrect_subcommand() {
|
||||||
#[test]
|
#[test]
|
||||||
fn verbose_number() {
|
fn verbose_number() {
|
||||||
fn get_verbose_level(arg_list: Vec<&str>) -> u64 {
|
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);
|
assert_eq!(get_verbose_level(vec!["mdp", "build", "-v"]), 1);
|
||||||
|
|
Reference in a new issue