imag/src/configuration.rs

88 lines
2 KiB
Rust
Raw Normal View History

2015-10-26 22:51:44 +00:00
extern crate clap;
2015-10-26 23:02:42 +00:00
use cli::CliConfig;
2015-10-26 22:51:44 +00:00
use std::path::Path;
use config::reader::from_file;
use config::types::Config as Cfg;
2015-11-27 18:22:21 +00:00
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Error;
2015-10-26 22:51:44 +00:00
pub struct Configuration {
pub rtp : String,
2015-10-26 22:56:54 +00:00
pub store_sub : String,
2015-10-26 22:51:44 +00:00
pub verbose : bool,
pub debugging : bool,
}
impl Configuration {
2015-10-26 23:02:42 +00:00
pub fn new(config: &CliConfig) -> Configuration {
2015-10-26 22:51:44 +00:00
let rtp = rtp_path(config);
let mut verbose = false;
let mut debugging = false;
2015-10-26 22:56:54 +00:00
let mut store_sub = String::from("/store");
2015-10-26 22:51:44 +00:00
if let Some(cfg) = fetch_config(&rtp) {
if let Some(v) = cfg.lookup_boolean("verbose") {
verbose = v;
}
if let Some(d) = cfg.lookup_boolean("debug") {
debugging = d;
}
2015-10-26 22:56:54 +00:00
if let Some(s) = cfg.lookup_str("store") {
store_sub = String::from(s);
}
2015-10-26 22:51:44 +00:00
}
Configuration {
verbose: verbose,
debugging: debugging,
2015-10-26 22:56:54 +00:00
store_sub: store_sub,
2015-10-26 22:51:44 +00:00
rtp: rtp,
}
}
pub fn is_verbose(&self) -> bool {
self.verbose
}
pub fn is_debugging(&self) -> bool {
self.debugging
}
2015-10-26 22:56:54 +00:00
pub fn store_path_str(&self) -> String {
format!("{}{}", self.rtp, self.store_sub)
}
pub fn get_rtp(&self) -> String {
self.rtp.clone()
}
2015-10-26 22:51:44 +00:00
}
2015-10-26 23:02:42 +00:00
fn rtp_path(config: &CliConfig) -> String {
2015-10-26 22:51:44 +00:00
String::from(config.cli_matches.value_of("rtp").unwrap_or("~/.imag/store/"))
}
fn fetch_config(rtp: &String) -> Option<Cfg> {
from_file(Path::new(&(rtp.clone() + "/config"))).ok()
}
2015-11-27 18:22:21 +00:00
impl Debug for Configuration {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "Configuration (verbose: {}, debugging: {}, rtp: {}, store path: {})",
self.is_verbose(),
self.is_debugging(),
self.get_rtp(),
self.store_path_str()
)
}
}