archive
/
md-pdf-rs
Archived
1
Fork 0

Dont panic, exit nicely

This commit is contained in:
Jake Howard 2017-08-14 21:22:53 +01:00
parent 0e2e596071
commit a060780681
Signed by: jake
GPG Key ID: 57AFB45680EDD477
4 changed files with 39 additions and 8 deletions

View File

@ -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")
}

View File

@ -5,6 +5,9 @@ extern crate serde_derive;
extern crate serde_yaml;
use std::io::{self, Write};
use std::process::exit;
mod args;
mod config;
mod process;
@ -14,11 +17,38 @@ mod utils;
#[cfg(test)]
mod tests;
use clap::ArgMatches;
use config::Config;
fn ok_or_exit<T>(res: Result<T, String>) -> T {
return match res {
Ok(k) => k,
Err(err) => {
writeln!(io::stderr(), "Error: {:?}", err).unwrap();
exit(1);
}
};
}
fn get_config(args: &ArgMatches) -> Config {
let mut config = ok_or_exit(config::get_config());
config.verbosity = args::get_verbose(args);
return config;
}
fn main() {
let args = args::get_matches();
if args.subcommand_name().unwrap() == "build" {
let mut config = config::get_config().expect("Config error");
config.verbosity = args::get_verbose(args);
process::build(config);
let subcommand = args.subcommand_name().expect("subcommand error");
match subcommand {
"build" => {
let config = get_config(&args);
ok_or_exit(process::build(config));
}
cmd => {
writeln!(io::stderr(), "Unknown command {}.", cmd).unwrap();
exit(1);
}
}
}

View File

@ -2,7 +2,8 @@ use input::read_input_files;
use config::Config;
pub fn build(config: Config) {
let input = read_input_files(config.input).unwrap();
pub fn build(config: Config) -> Result<(), String> {
let input = try!(read_input_files(config.input));
println!("{}", input);
return Ok(());
}

View File

@ -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);