use toml::Value;
use store::Result;
use error::StoreError as SE;
pub fn config_is_valid(config: &Option<Value>) -> Result<()> {
use error::StoreErrorKind as SEK;
if config.is_none() {
return Ok(());
}
match *config {
Some(Value::Table(_)) => Ok(()),
_ => {
warn!("Store config is no table");
Err(SE::from_kind(SEK::ConfigTypeError))
},
}
}
pub fn config_implicit_store_create_allowed(config: Option<&Value>) -> bool {
config.map(|t| {
match *t {
Value::Table(ref t) => {
match t.get("implicit-create") {
Some(&Value::Boolean(b)) => b,
Some(_) => {
warn!("Key 'implicit-create' does not contain a Boolean value");
false
}
None => {
warn!("Key 'implicit-create' in store configuration missing");
false
},
}
}
_ => {
warn!("Store configuration seems to be no Table");
false
},
}
}).unwrap_or(false)
}
#[cfg(test)]
mod tests {
use toml::de::from_str as toml_from_str;
use configuration::*;
#[test]
fn test_implicit_store_create_allowed_no_toml() {
assert!(!config_implicit_store_create_allowed(None));
}
#[test]
fn test_implicit_store_create_allowed_toml_empty() {
let config = toml_from_str("").unwrap();
assert!(!config_implicit_store_create_allowed(Some(config).as_ref()));
}
#[test]
fn test_implicit_store_create_allowed_toml_false() {
let config = toml_from_str(r#"
implicit-create = false
"#).unwrap();
assert!(!config_implicit_store_create_allowed(Some(config).as_ref()));
}
#[test]
fn test_implicit_store_create_allowed_toml_true() {
let config = toml_from_str(r#"
implicit-create = true
"#).unwrap();
assert!(config_implicit_store_create_allowed(Some(config).as_ref()));
}
}