Merge pull request #977 from matthiasbeyer/libimagrt/fixes

Libimagrt/fixes
This commit is contained in:
Matthias Beyer 2017-06-21 08:33:25 +02:00 committed by GitHub
commit 2b7706424a
2 changed files with 40 additions and 14 deletions

View file

@ -23,6 +23,9 @@ use std::ops::Deref;
use toml::Value; use toml::Value;
use error::RuntimeErrorKind as REK;
use libimagerror::into::IntoError;
generate_error_module!( generate_error_module!(
generate_error_types!(ConfigError, ConfigErrorKind, generate_error_types!(ConfigError, ConfigErrorKind,
TOMLParserError => "TOML Parsing error", TOMLParserError => "TOML Parsing error",
@ -221,14 +224,12 @@ fn fetch_config(rtp: &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;
use std::io::Write;
use std::io::stderr;
use xdg_basedir; use xdg_basedir;
use itertools::Itertools; use itertools::Itertools;
use libimagutil::variants::generate_variants as gen_vars; use libimagutil::variants::generate_variants as gen_vars;
use libimagerror::trace::trace_error; use self::error::MapErrInto;
let variants = vec!["config", "config.toml", "imagrc", "imagrc.toml"]; let variants = vec!["config", "config.toml", "imagrc", "imagrc.toml"];
let modifier = |base: &PathBuf, v: &'static str| { let modifier = |base: &PathBuf, v: &'static str| {
@ -250,6 +251,7 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
.flatten() .flatten()
.filter(|path| path.exists() && path.is_file()) .filter(|path| path.exists() && path.is_file())
.map(|path| { .map(|path| {
debug!("Reading {:?}", path);
let content = { let content = {
let mut s = String::new(); let mut s = String::new();
let f = File::open(path); let f = File::open(path);
@ -261,17 +263,18 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
s s
}; };
match ::toml::de::from_str(&content[..]) { trace!("Contents of config file: \n---\n{}\n---", content);
Ok(res) => res,
Err(e) => { let toml = ::toml::de::from_str(&content[..])
write!(stderr(), "Config file parser error:").ok(); .map_err_into(ConfigErrorKind::TOMLParserError)
trace_error(&e); .map_err(Box::new)
None .map_err(|e| REK::Instantiate.into_error_with_cause(e));
}
} Some(toml)
}) })
.filter(|loaded| loaded.is_some()) .filter_map(|x| x)
.nth(0) .filter(|loaded| loaded.is_ok())
.map(|inner| Value::Table(inner.unwrap())) .map(|inner| Value::Table(inner.unwrap()))
.nth(0)
.ok_or(ConfigErrorKind::NoConfigFileFound.into()) .ok_or(ConfigErrorKind::NoConfigFileFound.into())
} }

View file

@ -103,6 +103,10 @@ impl<'a> Runtime<'a> {
let configpath = matches.value_of("config") let configpath = matches.value_of("config")
.map_or_else(|| rtp.clone(), PathBuf::from); .map_or_else(|| rtp.clone(), PathBuf::from);
debug!("RTP path = {:?}", rtp);
debug!("Store path = {:?}", storepath);
debug!("Config path = {:?}", configpath);
let cfg = match Configuration::new(&configpath) { let cfg = match Configuration::new(&configpath) {
Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound { Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound {
return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e))); return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e)));
@ -354,7 +358,7 @@ impl<'a> Runtime<'a> {
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
let mut input = ::std::io::empty(); let mut input = ::std::io::stdin();
let output = ::std::io::stdout(); let output = ::std::io::stdout();
let output = Rc::new(RefCell::new(output)); let output = Rc::new(RefCell::new(output));
let mapper = JsonMapper::new(); let mapper = JsonMapper::new();
@ -368,6 +372,25 @@ impl<'a> Runtime<'a> {
}) })
} }
pub fn store_backend_to_stdout(&mut self) -> Result<(), RuntimeError> {
use libimagstore::file_abstraction::stdio::mapper::json::JsonMapper;
use libimagstore::file_abstraction::stdio::out::StdoutFileAbstraction;
use std::rc::Rc;
use std::cell::RefCell;
let output = ::std::io::stdout();
let output = Rc::new(RefCell::new(output));
let mapper = JsonMapper::new();
StdoutFileAbstraction::new(output, mapper)
.map_err_into(RuntimeErrorKind::Instantiate)
.and_then(|backend| {
self.store
.reset_backend(Box::new(backend))
.map_err_into(RuntimeErrorKind::Instantiate)
})
}
/// Get a editor command object which can be called to open the $EDITOR /// Get a editor command object which can be called to open the $EDITOR
pub fn editor(&self) -> Option<Command> { pub fn editor(&self) -> Option<Command> {
self.cli() self.cli()