Merge pull request #231 from matthiasbeyer/libimagrt/config-in-toml

Switch configuration language to TOML
This commit is contained in:
Matthias Beyer 2016-03-07 11:22:23 +01:00
commit 71d6008105
4 changed files with 76 additions and 34 deletions

View file

@ -5,7 +5,7 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies] [dependencies]
clap = "2.1.1" clap = "2.1.1"
config = "0.1.3" toml = "0.1.27"
log = "0.3.4" log = "0.3.4"
xdg-basedir = "0.2.2" xdg-basedir = "0.2.2"
itertools = "0.4.6" itertools = "0.4.6"

View file

@ -1,9 +1,7 @@
use std::default::Default;
use std::path::PathBuf; use std::path::PathBuf;
use std::result::Result as RResult; use std::result::Result as RResult;
pub use config::types::Config; use toml::{Parser, Value};
pub use config::reader::from_file;
/** /**
* Errors which are related to configuration-file loading * Errors which are related to configuration-file loading
@ -18,7 +16,6 @@ pub mod error {
*/ */
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum ConfigErrorKind { pub enum ConfigErrorKind {
ConfigNotFound,
ConfigParsingFailed, ConfigParsingFailed,
NoConfigFileFound, NoConfigFileFound,
} }
@ -56,7 +53,6 @@ pub mod error {
*/ */
pub fn as_str(e: &ConfigError) -> &'static str { pub fn as_str(e: &ConfigError) -> &'static str {
match e.kind() { match e.kind() {
ConfigErrorKind::ConfigNotFound => "Config not found",
ConfigErrorKind::ConfigParsingFailed => "Config parsing failed", ConfigErrorKind::ConfigParsingFailed => "Config parsing failed",
ConfigErrorKind::NoConfigFileFound => "No config file found", ConfigErrorKind::NoConfigFileFound => "No config file found",
} }
@ -104,6 +100,11 @@ pub type Result<T> = RResult<T, ConfigError>;
#[derive(Debug)] #[derive(Debug)]
pub struct Configuration { pub struct Configuration {
/**
* The plain configuration object for direct access if necessary
*/
config: Value,
/** /**
* The verbosity the program should run with * The verbosity the program should run with
*/ */
@ -133,9 +134,9 @@ impl Configuration {
*/ */
pub fn new(rtp: &PathBuf) -> Result<Configuration> { pub fn new(rtp: &PathBuf) -> Result<Configuration> {
fetch_config(&rtp).map(|cfg| { fetch_config(&rtp).map(|cfg| {
let verbosity = cfg.lookup_boolean("verbosity").unwrap_or(false); let verbosity = get_verbosity(&cfg);
let editor = cfg.lookup_str("editor").map(String::from); let editor = get_editor(&cfg);
let editor_opts = String::from(cfg.lookup_str("editor-opts").unwrap_or("")); let editor_opts = get_editor_opts(&cfg);
debug!("Building configuration"); debug!("Building configuration");
debug!(" - verbosity : {:?}", verbosity); debug!(" - verbosity : {:?}", verbosity);
@ -143,6 +144,7 @@ impl Configuration {
debug!(" - editor-opts: {}", editor_opts); debug!(" - editor-opts: {}", editor_opts);
Configuration { Configuration {
config: cfg,
verbosity: verbosity, verbosity: verbosity,
editor: editor, editor: editor,
editor_opts: editor_opts, editor_opts: editor_opts,
@ -150,6 +152,36 @@ impl Configuration {
}) })
} }
pub fn config(&self) -> &Value {
&self.config
}
}
fn get_verbosity(v: &Value) -> bool {
match v {
&Value::Table(ref t) => t.get("verbose")
.map(|v| match v { &Value::Boolean(b) => b, _ => false, })
.unwrap_or(false),
_ => false,
}
}
fn get_editor(v: &Value) -> Option<String> {
match v {
&Value::Table(ref t) => t.get("editor")
.and_then(|v| match v { &Value::String(ref s) => Some(s.clone()), _ => None, }),
_ => None,
}
}
fn get_editor_opts(v: &Value) -> String {
match v {
&Value::Table(ref t) => t.get("editor-opts")
.and_then(|v| match v { &Value::String(ref s) => Some(s.clone()), _ => None, })
.unwrap_or(String::new()),
_ => String::new(),
}
} }
/** /**
@ -157,8 +189,10 @@ impl Configuration {
* *
* 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<Config> { fn fetch_config(rtp: &PathBuf) -> Result<Value> {
use std::env; use std::env;
use std::fs::File;
use std::io::Read;
use xdg_basedir; use xdg_basedir;
use itertools::Itertools; use itertools::Itertools;
@ -184,29 +218,20 @@ fn fetch_config(rtp: &PathBuf) -> Result<Config> {
.flatten() .flatten()
.filter(|path| path.exists()) .filter(|path| path.exists())
.map(|path| { .map(|path| {
from_file(&path) let content = {
.map_err(|e| { let mut s = String::new();
ConfigError::new(ConfigErrorKind::ConfigParsingFailed, Some(Box::new(e))) let mut f = File::open(path);
}) if f.is_err() {
}
let mut f = f.unwrap();
f.read_to_string(&mut s);
s
};
Parser::new(&content[..]).parse()
}) })
.filter(|loaded| loaded.is_ok()) .filter(|loaded| loaded.is_some())
.nth(0) .nth(0)
.map(|inner| inner.unwrap()) .map(|inner| Value::Table(inner.unwrap()))
.ok_or(ConfigError::new(ConfigErrorKind::NoConfigFileFound, None)) .ok_or(ConfigError::new(ConfigErrorKind::NoConfigFileFound, None))
} }
impl Default for Configuration {
/**
* Get a default configuration object
*/
fn default() -> Configuration {
Configuration {
verbosity: false,
editor: Some(String::from("nano")),
editor_opts: String::from(""),
}
}
}

View file

@ -3,7 +3,7 @@
#[cfg(unix)] extern crate xdg_basedir; #[cfg(unix)] extern crate xdg_basedir;
extern crate clap; extern crate clap;
extern crate config; extern crate toml;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;

View file

@ -15,7 +15,7 @@ use libimagstore::store::Store;
pub struct Runtime<'a> { pub struct Runtime<'a> {
rtp: PathBuf, rtp: PathBuf,
configuration: Configuration, configuration: Option<Configuration>,
cli_matches: ArgMatches<'a>, cli_matches: ArgMatches<'a>,
store: Store, store: Store,
} }
@ -32,6 +32,9 @@ impl<'a> Runtime<'a> {
*/ */
pub fn new(cli_spec: App<'a, 'a>) -> Result<Runtime<'a>, RuntimeError> { pub fn new(cli_spec: App<'a, 'a>) -> Result<Runtime<'a>, RuntimeError> {
use std::env; use std::env;
use std::error::Error;
use configuration::error::ConfigErrorKind;
let matches = cli_spec.get_matches(); let matches = cli_spec.get_matches();
let rtp : PathBuf = matches.value_of("runtimepath") let rtp : PathBuf = matches.value_of("runtimepath")
@ -51,10 +54,24 @@ impl<'a> Runtime<'a> {
spath.push("store"); spath.push("store");
spath spath
}); });
let cfg = Configuration::new(&rtp);
let cfg = if cfg.is_err() {
let e = cfg.err().unwrap();
if e.kind() != ConfigErrorKind::NoConfigFileFound {
let cause : Option<Box<Error>> = Some(Box::new(e));
return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause));
} else {
None
}
} else {
Some(cfg.unwrap())
};
Store::new(storepath).map(|store| { Store::new(storepath).map(|store| {
Runtime { Runtime {
cli_matches: matches, cli_matches: matches,
configuration: Configuration::new(&rtp).unwrap_or(Configuration::default()), configuration: cfg,
rtp: rtp, rtp: rtp,
store: store, store: store,
} }