Configuration is optional

This commit is contained in:
Matthias Beyer 2016-03-05 13:01:09 +01:00
parent 21d5d32d50
commit 2fa10067fb
2 changed files with 14 additions and 8 deletions

View file

@ -16,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,
} }
@ -54,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",
} }

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,
} }
@ -34,6 +34,8 @@ impl<'a> Runtime<'a> {
use std::env; use std::env;
use std::error::Error; 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")
.map(PathBuf::from) .map(PathBuf::from)
@ -54,11 +56,17 @@ impl<'a> Runtime<'a> {
}); });
let cfg = Configuration::new(&rtp); let cfg = Configuration::new(&rtp);
if cfg.is_err() { let cfg = if cfg.is_err() {
let cause : Option<Box<Error>> = Some(Box::new(cfg.err().unwrap())); 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)); return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause));
} else {
None
} }
let cfg = cfg.unwrap(); } else {
Some(cfg.unwrap())
};
Store::new(storepath).map(|store| { Store::new(storepath).map(|store| {
Runtime { Runtime {