summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Ignat <robert.ignat91@gmail.com>2017-06-04 01:34:21 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-07-08 10:44:14 +0200
commit6f42e7d0028cd105b6fe0adb7a36a520faf92ce6 (patch)
treea34bf534bd83e02e1a14ad4514cf0bba5ee4a649
parent0def6a3d5a77bb2004934a9a98714b3c45759155 (diff)
Add configuration traits
-rw-r--r--libimagrt/src/configuration.rs55
-rw-r--r--libimagrt/src/runtime.rs20
2 files changed, 49 insertions, 26 deletions
diff --git a/libimagrt/src/configuration.rs b/libimagrt/src/configuration.rs
index fe1c1614..b0f0fdc1 100644
--- a/libimagrt/src/configuration.rs
+++ b/libimagrt/src/configuration.rs
@@ -22,6 +22,7 @@ use std::result::Result as RResult;
use std::ops::Deref;
use toml::Value;
+use clap::App;
use error::RuntimeErrorKind as REK;
use libimagerror::into::IntoError;
@@ -72,24 +73,22 @@ impl Configuration {
/// with all variants.
///
/// If that doesn't work either, an error is returned.
- pub fn new(rtp: &PathBuf) -> Result<Configuration> {
- fetch_config(&rtp).map(|cfg| {
- let verbosity = get_verbosity(&cfg);
- let editor = get_editor(&cfg);
- let editor_opts = get_editor_opts(&cfg);
-
- debug!("Building configuration");
- debug!(" - verbosity : {:?}", verbosity);
- debug!(" - editor : {:?}", editor);
- debug!(" - editor-opts: {}", editor_opts);
-
- Configuration {
- config: cfg,
- verbosity: verbosity,
- editor: editor,
- editor_opts: editor_opts,
- }
- })
+ pub fn new(toml_config: Value) -> Configuration {
+ let verbosity = get_verbosity(&toml_config);
+ let editor = get_editor(&toml_config);
+ let editor_opts = get_editor_opts(&toml_config);
+
+ debug!("Building configuration");
+ debug!(" - verbosity : {:?}", verbosity);
+ debug!(" - editor : {:?}", editor);
+ debug!(" - editor-opts: {}", editor_opts);
+
+ Configuration {
+ config: toml_config,
+ verbosity: verbosity,
+ editor: editor,
+ editor_opts: editor_opts,
+ }
}
/// Get the Editor setting from the configuration
@@ -274,3 +273,23 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
.nth(0)
.ok_or(ConfigErrorKind::NoConfigFileFound.into())
}
+
+pub trait GetConfiguration {
+ fn get_configuration(rtp: &PathBuf) -> Result<Configuration> {
+ fetch_config(rtp).map(Configuration::new)
+ }
+}
+
+impl<'a> GetConfiguration for App<'a, 'a> {}
+
+pub trait InternalConfiguration {
+ fn enable_hooks() -> bool {
+ true
+ }
+
+ fn enable_logging() -> bool {
+ true
+ }
+}
+
+impl<'a> InternalConfiguration for App<'a, 'a> {}
diff --git a/libimagrt/src/runtime.rs b/libimagrt/src/runtime.rs
index 0c1b919b..e5d88c62 100644
--- a/libimagrt/src/runtime.rs
+++ b/libimagrt/src/runtime.rs
@@ -29,7 +29,7 @@ use clap::{Arg, ArgMatches};
use log;
use log::LogLevelFilter;
-use configuration::Configuration;
+use configuration::{Configuration, GetConfiguration, InternalConfiguration};
use error::RuntimeError;
use error::RuntimeErrorKind;
use error::MapErrInto;
@@ -55,8 +55,10 @@ impl<'a> Runtime<'a> {
/// in $HOME/.imag/config, $XDG_CONFIG_DIR/imag/config or from env("$IMAG_CONFIG")
/// and builds the Runtime object with it.
///
- /// The cli_spec object should be initially build with the ::get_default_cli_builder() function.
- pub fn new<C: Clone + CliSpec<'a>>(mut cli_spec: C) -> Result<Runtime<'a>, RuntimeError> {
+ /// The cli_app object should be initially build with the ::get_default_cli_builder() function.
+ pub fn new<C>(mut cli_app: C) -> Result<Runtime<'a>, RuntimeError>
+ where C: Clone + CliSpec<'a> + GetConfiguration + InternalConfiguration
+ {
use std::env;
use std::io::stdout;
@@ -67,20 +69,22 @@ impl<'a> Runtime<'a> {
use configuration::error::ConfigErrorKind;
- let matches = cli_spec.clone().matches();
+ let matches = cli_app.clone().matches();
let is_debugging = matches.is_present("debugging");
let is_verbose = matches.is_present("verbosity");
let colored = !matches.is_present("no-color-output");
- Runtime::init_logger(is_debugging, is_verbose, colored);
+ if C::enable_logging() {
+ Runtime::init_logger(is_debugging, is_verbose, colored);
+ }
match matches.value_of(Runtime::arg_generate_compl()) {
Some(shell) => {
debug!("Generating shell completion script, writing to stdout");
let shell = shell.parse::<Shell>().unwrap(); // clap has our back here.
- let appname = String::from(cli_spec.name());
- cli_spec.completions(appname, shell, &mut stdout());
+ let appname = String::from(cli_app.name());
+ cli_app.completions(appname, shell, &mut stdout());
},
_ => debug!("Not generating shell completion script"),
}
@@ -108,7 +112,7 @@ impl<'a> Runtime<'a> {
debug!("Store path = {:?}", storepath);
debug!("Config path = {:?}", configpath);
- let cfg = match Configuration::new(&configpath) {
+ let cfg = match C::get_configuration(&configpath) {
Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound {
return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e)));
} else {