Move reading functions to seperate file

This commit is contained in:
Jake Howard 2017-11-24 22:39:37 +00:00
parent 1faed7ed86
commit a7d0961d3c
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 40 additions and 32 deletions

View file

@ -1,13 +1,12 @@
use serde_yaml;
use std::path::PathBuf; use std::path::PathBuf;
use std::collections::HashMap; use std::collections::HashMap;
use utils::{result_prefix, resolve_path, result_override}; use utils::{result_prefix, resolve_path, result_override};
use std::fs::File;
use std::env::current_dir; use std::env::current_dir;
use std::io::Read;
pub mod consts; pub mod consts;
pub mod csl; pub mod csl;
pub mod read;
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone)] #[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
pub enum OutputType { pub enum OutputType {
@ -65,31 +64,3 @@ impl References {
fn default_verbosity() -> u64 { fn default_verbosity() -> u64 {
return 0; return 0;
} }
fn get_config_path() -> PathBuf {
let mut working_dir = current_dir().unwrap();
working_dir.push(consts::CONFIG_FILE_NAME);
return working_dir;
}
fn read() -> Result<String, String> {
let config_path = get_config_path();
let mut config_file = try!(result_override(
File::open(&config_path),
format!("Unable to find config file at {}", config_path.display())
));
let mut contents = String::new();
try!(result_override(
config_file.read_to_string(&mut contents),
format!("Failed to read config file at {}.", config_path.display())
));
return Ok(contents);
}
pub fn get_config() -> Result<Config, String> {
let config_str = try!(read());
let config: Config =
try!(result_prefix(serde_yaml::from_str(&config_str), "Config Parse Error".into()));
return Ok(config);
}

37
src/config/read.rs Normal file
View file

@ -0,0 +1,37 @@
use serde_yaml;
use utils::{result_prefix, result_override};
use config::Config;
use config::consts;
use std::io::Read;
use std::fs::File;
use std::env::current_dir;
use std::path::PathBuf;
fn get_config_path() -> PathBuf {
let mut working_dir = current_dir().unwrap();
working_dir.push(consts::CONFIG_FILE_NAME);
return working_dir;
}
fn read() -> Result<String, String> {
let config_path = get_config_path();
let mut config_file = try!(result_override(
File::open(&config_path),
format!("Unable to find config file at {}", config_path.display())
));
let mut contents = String::new();
try!(result_override(
config_file.read_to_string(&mut contents),
format!("Failed to read config file at {}.", config_path.display())
));
return Ok(contents);
}
pub fn get_config() -> Result<Config, String> {
let config_str = try!(read());
let config: Config =
try!(result_prefix(serde_yaml::from_str(&config_str), "Config Parse Error".into()));
return Ok(config);
}

View file

@ -43,7 +43,7 @@ fn build(config: Config) -> Result<(), String> {
} }
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::read::get_config());
config.verbosity = args::get_verbose(args); config.verbosity = args::get_verbose(args);
return config; return config;
} }