2016-01-10 14:04:06 +00:00
|
|
|
use std::path::PathBuf;
|
2016-01-19 17:05:00 +00:00
|
|
|
use std::result::Result as RResult;
|
2016-03-26 18:49:58 +00:00
|
|
|
use std::ops::Deref;
|
2016-01-10 14:04:06 +00:00
|
|
|
|
2016-03-05 11:30:40 +00:00
|
|
|
use toml::{Parser, Value};
|
2016-01-10 14:04:06 +00:00
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* Errors which are related to configuration-file loading
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
pub mod error {
|
|
|
|
use std::error::Error;
|
2016-03-25 14:59:18 +00:00
|
|
|
use std::fmt::{Display, Formatter};
|
2016-01-19 17:05:00 +00:00
|
|
|
use std::fmt::Error as FmtError;
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* The kind of an error
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum ConfigErrorKind {
|
|
|
|
ConfigParsingFailed,
|
|
|
|
NoConfigFileFound,
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* Configuration error type
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ConfigError {
|
|
|
|
kind: ConfigErrorKind,
|
|
|
|
cause: Option<Box<Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigError {
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* Instantiate a new ConfigError, optionally with cause
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
pub fn new(kind: ConfigErrorKind, cause: Option<Box<Error>>) -> ConfigError {
|
|
|
|
ConfigError {
|
|
|
|
kind: kind,
|
|
|
|
cause: cause,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* get the Kind of the Error
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
pub fn kind(&self) -> ConfigErrorKind {
|
|
|
|
self.kind.clone()
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* Get the string, the ConfigError can be described with
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
pub fn as_str(e: &ConfigError) -> &'static str {
|
|
|
|
match e.kind() {
|
|
|
|
ConfigErrorKind::ConfigParsingFailed => "Config parsing failed",
|
|
|
|
ConfigErrorKind::NoConfigFileFound => "No config file found",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for ConfigError {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
try!(write!(fmt, "{}", ConfigError::as_str(self)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for ConfigError {
|
|
|
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
ConfigError::as_str(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
self.cause.as_ref().map(|e| &**e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
use self::error::{ConfigError, ConfigErrorKind};
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Result type of this module. Either T or ConfigError
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
pub type Result<T> = RResult<T, ConfigError>;
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* Configuration object
|
|
|
|
*
|
|
|
|
* Holds all config variables which are globally available plus the configuration object from the
|
|
|
|
* config parser, which can be accessed.
|
|
|
|
*/
|
2016-01-19 17:25:13 +00:00
|
|
|
#[derive(Debug)]
|
2016-01-10 14:04:06 +00:00
|
|
|
pub struct Configuration {
|
2016-01-21 20:19:31 +00:00
|
|
|
|
2016-03-05 11:36:11 +00:00
|
|
|
/**
|
|
|
|
* The plain configuration object for direct access if necessary
|
|
|
|
*/
|
|
|
|
config: Value,
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* The verbosity the program should run with
|
|
|
|
*/
|
2016-01-10 14:04:06 +00:00
|
|
|
verbosity: bool,
|
2016-01-21 20:19:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The editor which should be used
|
|
|
|
*/
|
2016-01-10 14:04:06 +00:00
|
|
|
editor: Option<String>,
|
2016-01-21 20:19:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The options the editor should get when opening some file
|
|
|
|
*/
|
2016-01-10 14:04:06 +00:00
|
|
|
editor_opts: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Configuration {
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* Get a new configuration object.
|
|
|
|
*
|
|
|
|
* The passed runtimepath is used for searching the configuration file, whereas several file
|
|
|
|
* names are tested. If that does not work, the home directory and the XDG basedir are tested
|
|
|
|
* with all variants.
|
|
|
|
*
|
|
|
|
* If that doesn't work either, an error is returned.
|
|
|
|
*/
|
2016-01-19 17:05:00 +00:00
|
|
|
pub fn new(rtp: &PathBuf) -> Result<Configuration> {
|
|
|
|
fetch_config(&rtp).map(|cfg| {
|
2016-03-05 11:30:40 +00:00
|
|
|
let verbosity = get_verbosity(&cfg);
|
|
|
|
let editor = get_editor(&cfg);
|
|
|
|
let editor_opts = get_editor_opts(&cfg);
|
2016-01-10 14:04:06 +00:00
|
|
|
|
|
|
|
debug!("Building configuration");
|
|
|
|
debug!(" - verbosity : {:?}", verbosity);
|
|
|
|
debug!(" - editor : {:?}", editor);
|
|
|
|
debug!(" - editor-opts: {}", editor_opts);
|
|
|
|
|
2016-01-19 17:05:00 +00:00
|
|
|
Configuration {
|
2016-03-05 11:36:11 +00:00
|
|
|
config: cfg,
|
2016-01-10 14:04:06 +00:00
|
|
|
verbosity: verbosity,
|
|
|
|
editor: editor,
|
|
|
|
editor_opts: editor_opts,
|
2016-01-19 17:05:00 +00:00
|
|
|
}
|
2016-01-10 14:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-21 09:19:03 +00:00
|
|
|
pub fn editor(&self) -> Option<&String> {
|
|
|
|
self.editor.as_ref()
|
|
|
|
}
|
|
|
|
|
2016-03-05 11:36:11 +00:00
|
|
|
pub fn config(&self) -> &Value {
|
|
|
|
&self.config
|
|
|
|
}
|
|
|
|
|
2016-03-05 17:42:59 +00:00
|
|
|
pub fn store_config(&self) -> Option<&Value> {
|
|
|
|
match &self.config {
|
|
|
|
&Value::Table(ref tabl) => tabl.get("store"),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 18:49:58 +00:00
|
|
|
impl Deref for Configuration {
|
|
|
|
type Target = Value;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Value {
|
|
|
|
&self.config
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-05 11:30:40 +00:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:19:31 +00:00
|
|
|
/**
|
|
|
|
* Helper to fetch the config file
|
|
|
|
*
|
|
|
|
* Tests several variants for the config file path and uses the first one which works.
|
|
|
|
*/
|
2016-03-05 11:30:40 +00:00
|
|
|
fn fetch_config(rtp: &PathBuf) -> Result<Value> {
|
2016-01-10 14:04:06 +00:00
|
|
|
use std::env;
|
2016-03-05 11:30:40 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2016-03-25 14:10:54 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::io::stderr;
|
2016-01-10 14:04:06 +00:00
|
|
|
|
|
|
|
use xdg_basedir;
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
use libimagutil::variants::generate_variants as gen_vars;
|
|
|
|
|
|
|
|
let variants = vec!["config", "config.toml", "imagrc", "imagrc.toml"];
|
|
|
|
let modifier = |base: &PathBuf, v: &'static str| {
|
|
|
|
let mut base = base.clone();
|
2016-03-25 14:09:58 +00:00
|
|
|
base.push(format!("{}", v));
|
2016-01-10 14:04:06 +00:00
|
|
|
base
|
|
|
|
};
|
|
|
|
|
|
|
|
vec![
|
|
|
|
gen_vars(rtp.clone(), variants.clone(), &modifier),
|
|
|
|
|
|
|
|
env::var("HOME").map(|home| gen_vars(PathBuf::from(home), variants.clone(), &modifier))
|
|
|
|
.unwrap_or(vec![]),
|
|
|
|
|
|
|
|
xdg_basedir::get_data_home().map(|data_dir| gen_vars(data_dir, variants.clone(), &modifier))
|
|
|
|
.unwrap_or(vec![]),
|
|
|
|
].iter()
|
|
|
|
.flatten()
|
2016-03-25 14:10:32 +00:00
|
|
|
.filter(|path| path.exists() && path.is_file())
|
2016-01-19 17:05:00 +00:00
|
|
|
.map(|path| {
|
2016-03-05 11:30:40 +00:00
|
|
|
let content = {
|
|
|
|
let mut s = String::new();
|
2016-03-25 15:01:11 +00:00
|
|
|
let f = File::open(path);
|
2016-03-05 11:30:40 +00:00
|
|
|
if f.is_err() {
|
|
|
|
}
|
|
|
|
let mut f = f.unwrap();
|
2016-03-25 15:01:42 +00:00
|
|
|
f.read_to_string(&mut s).ok();
|
2016-03-05 11:30:40 +00:00
|
|
|
s
|
|
|
|
};
|
2016-03-25 14:10:54 +00:00
|
|
|
let mut parser = Parser::new(&content[..]);
|
|
|
|
let res = parser.parse();
|
|
|
|
if res.is_none() {
|
|
|
|
write!(stderr(), "Config file parser error:");
|
|
|
|
for error in parser.errors {
|
|
|
|
write!(stderr(), "At [{}][{}] <> {}", error.lo, error.hi, error);
|
|
|
|
write!(stderr(), "in: '{}'", &content[error.lo..error.hi]);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
res
|
|
|
|
}
|
2016-01-19 17:05:00 +00:00
|
|
|
})
|
2016-03-05 11:30:40 +00:00
|
|
|
.filter(|loaded| loaded.is_some())
|
2016-01-10 14:04:06 +00:00
|
|
|
.nth(0)
|
2016-03-05 11:30:40 +00:00
|
|
|
.map(|inner| Value::Table(inner.unwrap()))
|
2016-01-19 17:05:00 +00:00
|
|
|
.ok_or(ConfigError::new(ConfigErrorKind::NoConfigFileFound, None))
|
2016-01-10 14:04:06 +00:00
|
|
|
}
|
|
|
|
|