Merge pull request #1024 from matthiasbeyer/libimagrt/config-loading-fix

Libimagrt/config loading fix
This commit is contained in:
Matthias Beyer 2017-08-27 14:57:31 +02:00 committed by GitHub
commit 31254071e5

View file

@ -36,7 +36,8 @@ generate_error_module!(
); );
); );
pub use self::error::{ConfigError, ConfigErrorKind}; pub use self::error::{ConfigError, ConfigErrorKind, MapErrInto};
use libimagerror::into::IntoError;
/// Result type of this module. Either `T` or `ConfigError` /// Result type of this module. Either `T` or `ConfigError`
pub type Result<T> = RResult<T, ConfigError>; pub type Result<T> = RResult<T, ConfigError>;
@ -70,8 +71,8 @@ impl Configuration {
/// with all variants. /// with all variants.
/// ///
/// If that doesn't work either, an error is returned. /// If that doesn't work either, an error is returned.
pub fn new(rtp: &PathBuf) -> Result<Configuration> { pub fn new(config_searchpath: &PathBuf) -> Result<Configuration> {
fetch_config(&rtp).map(|cfg| { fetch_config(&config_searchpath).map(|cfg| {
let verbosity = get_verbosity(&cfg); let verbosity = get_verbosity(&cfg);
let editor = get_editor(&cfg); let editor = get_editor(&cfg);
let editor_opts = get_editor_opts(&cfg); let editor_opts = get_editor_opts(&cfg);
@ -223,7 +224,7 @@ fn get_editor_opts(v: &Value) -> String {
/// Helper to fetch the config file /// Helper to fetch the config file
/// ///
/// Tests several variants for the config file path and uses the first one which works. /// Tests several variants for the config file path and uses the first one which works.
fn fetch_config(rtp: &PathBuf) -> Result<Value> { fn fetch_config(searchpath: &PathBuf) -> Result<Value> {
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -244,8 +245,8 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
}; };
vec![ vec![
vec![rtp.clone()], vec![searchpath.clone()],
gen_vars(rtp.clone(), variants.clone(), &modifier), gen_vars(searchpath.clone(), variants.clone(), &modifier),
env::var("HOME").map(|home| gen_vars(PathBuf::from(home), variants.clone(), &modifier)) env::var("HOME").map(|home| gen_vars(PathBuf::from(home), variants.clone(), &modifier))
.unwrap_or(vec![]), .unwrap_or(vec![]),
@ -255,30 +256,37 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
].iter() ].iter()
.flatten() .flatten()
.filter(|path| path.exists() && path.is_file()) .filter(|path| path.exists() && path.is_file())
.map(|path| { .filter_map(|path| {
let content = { let content = {
let mut s = String::new();
let f = File::open(path); let f = File::open(path);
if f.is_err() { if f.is_err() {
let _ = write!(stderr(), "Error opening file: {:?}", f);
return None return None
} }
let mut f = f.unwrap(); let mut f = f.unwrap();
let mut s = String::new();
f.read_to_string(&mut s).ok(); f.read_to_string(&mut s).ok();
s s
}; };
match ::toml::de::from_str(&content[..]) { match ::toml::de::from_str::<::toml::Value>(&content[..]) {
Ok(res) => res, Ok(res) => Some(res),
Err(e) => { Err(e) => {
write!(stderr(), "Config file parser error:").ok(); let line_col = e
trace_error(&e); .line_col()
.map(|(line, col)| {
format!("Line {}, Column {}", line, col)
})
.unwrap_or_else(|| String::from("Line unknown, Column unknown"));
let _ = write!(stderr(), "Config file parser error at {}", line_col);
trace_error(&ConfigErrorKind::TOMLParserError.into_error_with_cause(Box::new(e)));
None None
} }
} }
}) })
.filter(|loaded| loaded.is_some())
.nth(0) .nth(0)
.map(|inner| Value::Table(inner.unwrap()))
.ok_or(ConfigErrorKind::NoConfigFileFound.into()) .ok_or(ConfigErrorKind::NoConfigFileFound.into())
} }