Remove configuration member, reduce configuration
We only need the configuration to check whether creating the store directory is actually allowed.
This commit is contained in:
parent
38b56df406
commit
174d8d76e9
3 changed files with 22 additions and 64 deletions
|
@ -21,48 +21,23 @@ use toml::Value;
|
|||
|
||||
use store::Result;
|
||||
use error::StoreError as SE;
|
||||
use error::StoreErrorKind as SEK;
|
||||
|
||||
/// Check whether the configuration is valid for the store
|
||||
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))
|
||||
},
|
||||
}
|
||||
}
|
||||
use toml_query::read::TomlValueReadExt;
|
||||
|
||||
/// Checks whether the store configuration has a key "implicit-create" which maps to a boolean
|
||||
/// value. If that key is present, the boolean is returned, otherwise false is returned.
|
||||
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)
|
||||
pub fn config_implicit_store_create_allowed(config: Option<&Value>) -> Result<bool> {
|
||||
let key = "implicit-create";
|
||||
|
||||
if let Some(t) = config {
|
||||
t.read(key)?
|
||||
.ok_or(SE::from_kind(SEK::ConfigKeyMissingError(key)))?
|
||||
.as_bool()
|
||||
.ok_or(SE::from_kind(SEK::ConfigTypeError(key, "boolean")))
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -40,14 +40,14 @@ error_chain! {
|
|||
display("Store Configuration Error")
|
||||
}
|
||||
|
||||
ConfigTypeError {
|
||||
ConfigTypeError(key: &'static str, expected: &'static str) {
|
||||
description("Store configuration type error")
|
||||
display("Store configuration type error")
|
||||
display("Store configuration type error at '{}', expected {}", key, expected)
|
||||
}
|
||||
|
||||
ConfigKeyMissingError {
|
||||
ConfigKeyMissingError(key: &'static str) {
|
||||
description("Configuration Key missing")
|
||||
display("Configuration Key missing")
|
||||
display("Configuration Key missing: '{}'", key)
|
||||
}
|
||||
|
||||
VersionError {
|
||||
|
|
|
@ -204,11 +204,6 @@ impl Drop for StoreEntry {
|
|||
pub struct Store {
|
||||
location: PathBuf,
|
||||
|
||||
///
|
||||
/// Configuration object of the store
|
||||
///
|
||||
configuration: Option<Value>,
|
||||
|
||||
///
|
||||
/// Internal Path->File cache map
|
||||
///
|
||||
|
@ -228,10 +223,8 @@ impl Store {
|
|||
|
||||
/// Create a new Store object
|
||||
///
|
||||
/// This opens a Store in `location` using the configuration from `store_config` (if absent, it
|
||||
/// uses defaults).
|
||||
///
|
||||
/// If the configuration is not valid, this fails.
|
||||
/// This opens a Store in `location`. The store_config is used to check whether creating the
|
||||
/// store implicitely is allowed.
|
||||
///
|
||||
/// If the location does not exist, creating directories is by default denied and the operation
|
||||
/// fails, if not configured otherwise.
|
||||
|
@ -243,7 +236,7 @@ impl Store {
|
|||
///
|
||||
/// - On success: Store object
|
||||
///
|
||||
pub fn new(location: PathBuf, store_config: Option<Value>) -> Result<Store> {
|
||||
pub fn new(location: PathBuf, store_config: Option<&Value>) -> Result<Store> {
|
||||
let backend = Box::new(FSFileAbstraction::new());
|
||||
Store::new_with_backend(location, store_config, backend)
|
||||
}
|
||||
|
@ -253,16 +246,13 @@ impl Store {
|
|||
///
|
||||
/// Do not use directly, only for testing purposes.
|
||||
pub fn new_with_backend(location: PathBuf,
|
||||
store_config: Option<Value>,
|
||||
store_config: Option<&Value>,
|
||||
backend: Box<FileAbstraction>) -> Result<Store> {
|
||||
use configuration::*;
|
||||
|
||||
debug!("Validating Store configuration");
|
||||
let _ = config_is_valid(&store_config).chain_err(|| SEK::ConfigurationError)?;
|
||||
|
||||
debug!("Building new Store object");
|
||||
if !location.exists() {
|
||||
if !config_implicit_store_create_allowed(store_config.as_ref()) {
|
||||
if !config_implicit_store_create_allowed(store_config)? {
|
||||
return Err(SE::from_kind(SEK::CreateStoreDirDenied))
|
||||
.chain_err(|| SEK::FileError)
|
||||
.chain_err(|| SEK::IoError);
|
||||
|
@ -279,7 +269,6 @@ impl Store {
|
|||
|
||||
let store = Store {
|
||||
location: location.clone(),
|
||||
configuration: store_config,
|
||||
entries: Arc::new(RwLock::new(HashMap::new())),
|
||||
backend: backend,
|
||||
};
|
||||
|
@ -319,11 +308,6 @@ impl Store {
|
|||
.map(|_| self.backend = backend)
|
||||
}
|
||||
|
||||
/// Get the store configuration
|
||||
pub fn config(&self) -> Option<&Value> {
|
||||
self.configuration.as_ref()
|
||||
}
|
||||
|
||||
/// Creates the Entry at the given location (inside the entry)
|
||||
///
|
||||
/// # Return value
|
||||
|
@ -786,7 +770,6 @@ impl Debug for Store {
|
|||
write!(fmt, " --- Store ---\n")?;
|
||||
write!(fmt, "\n")?;
|
||||
write!(fmt, " - location : {:?}\n", self.location)?;
|
||||
write!(fmt, " - configuration : {:?}\n", self.configuration)?;
|
||||
write!(fmt, "\n")?;
|
||||
write!(fmt, "Entries:\n")?;
|
||||
write!(fmt, "{:?}", self.entries)?;
|
||||
|
|
Loading…
Reference in a new issue