2016-01-13 20:47:23 +00:00
|
|
|
use std::collections::HashMap;
|
2016-01-13 20:51:40 +00:00
|
|
|
use std::ops::Drop;
|
2016-01-12 17:52:03 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::result::Result as RResult;
|
2016-01-13 10:53:56 +00:00
|
|
|
use std::sync::Arc;
|
2016-01-17 15:23:35 +00:00
|
|
|
use std::sync::RwLock;
|
2016-01-23 15:26:02 +00:00
|
|
|
use std::collections::BTreeMap;
|
2016-07-21 13:58:34 +00:00
|
|
|
use std::io::Read;
|
2016-02-12 21:07:15 +00:00
|
|
|
use std::convert::From;
|
2016-02-12 21:02:33 +00:00
|
|
|
use std::convert::Into;
|
2016-02-15 21:15:32 +00:00
|
|
|
use std::sync::Mutex;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2016-03-25 12:29:20 +00:00
|
|
|
use std::fmt::Formatter;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fmt::Error as FMTError;
|
2016-01-16 18:32:12 +00:00
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
use toml::{Table, Value};
|
2016-01-23 16:30:01 +00:00
|
|
|
use regex::Regex;
|
2016-03-13 13:32:48 +00:00
|
|
|
use glob::glob;
|
2016-04-16 15:03:41 +00:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
use walkdir::Iter as WalkDirIter;
|
2016-01-12 17:52:03 +00:00
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
use error::{ParserErrorKind, ParserError};
|
2016-05-14 17:05:54 +00:00
|
|
|
use error::{StoreError as SE, StoreErrorKind as SEK};
|
2016-06-27 16:16:43 +00:00
|
|
|
use error::MapErrInto;
|
2016-05-12 15:27:41 +00:00
|
|
|
use storeid::{IntoStoreId, StoreId, StoreIdIterator};
|
2016-07-22 13:14:30 +00:00
|
|
|
use file_abstraction::FileAbstraction;
|
2016-01-12 17:52:03 +00:00
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
use hook::aspect::Aspect;
|
2016-04-18 15:25:19 +00:00
|
|
|
use hook::error::HookErrorKind;
|
|
|
|
use hook::result::HookResult;
|
2016-03-04 15:43:01 +00:00
|
|
|
use hook::accessor::{ MutableHookDataAccessor,
|
2016-03-21 18:39:07 +00:00
|
|
|
StoreIdAccessor};
|
2016-03-04 19:33:08 +00:00
|
|
|
use hook::position::HookPosition;
|
|
|
|
use hook::Hook;
|
2016-02-15 21:15:32 +00:00
|
|
|
|
2016-05-27 08:11:51 +00:00
|
|
|
use libimagerror::into::IntoError;
|
2016-08-25 15:14:09 +00:00
|
|
|
use libimagerror::trace::trace_error;
|
2016-07-14 13:28:50 +00:00
|
|
|
use libimagutil::iter::FoldResult;
|
2016-05-27 08:11:51 +00:00
|
|
|
|
2016-05-12 15:27:41 +00:00
|
|
|
use self::glob_store_iter::*;
|
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// The Result Type returned by any interaction with the store that could fail
|
2016-05-14 17:05:54 +00:00
|
|
|
pub type Result<T> = RResult<T, SE>;
|
2016-01-12 17:52:03 +00:00
|
|
|
|
2016-01-17 14:09:10 +00:00
|
|
|
|
2016-03-25 12:29:20 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2016-01-23 16:03:21 +00:00
|
|
|
enum StoreEntryStatus {
|
|
|
|
Present,
|
2016-01-23 15:15:34 +00:00
|
|
|
Borrowed
|
|
|
|
}
|
|
|
|
|
2016-01-23 16:03:21 +00:00
|
|
|
/// A store entry, depending on the option type it is either borrowed currently
|
|
|
|
/// or not.
|
2016-03-25 12:29:20 +00:00
|
|
|
#[derive(Debug)]
|
2016-01-23 16:03:21 +00:00
|
|
|
struct StoreEntry {
|
|
|
|
id: StoreId,
|
2016-07-22 13:12:56 +00:00
|
|
|
file: FileAbstraction,
|
2016-01-23 16:03:21 +00:00
|
|
|
status: StoreEntryStatus,
|
2016-01-16 19:30:16 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:03:41 +00:00
|
|
|
pub enum StoreObject {
|
|
|
|
Id(StoreId),
|
|
|
|
Collection(PathBuf),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Walk {
|
2016-08-22 10:07:50 +00:00
|
|
|
store_path: PathBuf,
|
2016-04-16 15:03:41 +00:00
|
|
|
dirwalker: WalkDirIter,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Walk {
|
|
|
|
|
|
|
|
fn new(mut store_path: PathBuf, mod_name: &str) -> Walk {
|
2016-08-22 10:07:50 +00:00
|
|
|
let pb = store_path.clone();
|
2016-04-16 15:03:41 +00:00
|
|
|
store_path.push(mod_name);
|
|
|
|
Walk {
|
2016-08-22 10:07:50 +00:00
|
|
|
store_path: pb,
|
2016-04-16 15:03:41 +00:00
|
|
|
dirwalker: WalkDir::new(store_path).into_iter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Deref for Walk {
|
|
|
|
type Target = WalkDirIter;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.dirwalker
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for Walk {
|
|
|
|
type Item = StoreObject;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
while let Some(something) = self.dirwalker.next() {
|
|
|
|
match something {
|
|
|
|
Ok(next) => if next.file_type().is_dir() {
|
|
|
|
return Some(StoreObject::Collection(next.path().to_path_buf()))
|
|
|
|
} else if next.file_type().is_file() {
|
2016-08-22 10:07:50 +00:00
|
|
|
let n = next.path().to_path_buf();
|
2016-08-25 15:14:09 +00:00
|
|
|
let sid = match StoreId::new(Some(self.store_path.clone()), n) {
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2016-08-22 10:07:50 +00:00
|
|
|
return Some(StoreObject::Id(sid))
|
2016-04-16 15:03:41 +00:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Error in Walker");
|
|
|
|
debug!("{:?}", e);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-16 19:30:16 +00:00
|
|
|
impl StoreEntry {
|
2016-01-24 16:28:52 +00:00
|
|
|
|
2016-09-05 14:51:37 +00:00
|
|
|
fn new(id: StoreId) -> Result<StoreEntry> {
|
|
|
|
let pb = try!(id.clone().into_pathbuf());
|
|
|
|
Ok(StoreEntry {
|
|
|
|
id: id,
|
|
|
|
file: FileAbstraction::Absent(pb),
|
2016-01-24 16:28:52 +00:00
|
|
|
status: StoreEntryStatus::Present,
|
2016-09-05 14:51:37 +00:00
|
|
|
})
|
2016-01-24 16:28:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// The entry is currently borrowed, meaning that some thread is currently
|
|
|
|
/// mutating it
|
2016-01-16 19:30:16 +00:00
|
|
|
fn is_borrowed(&self) -> bool {
|
2016-01-23 16:03:21 +00:00
|
|
|
self.status == StoreEntryStatus::Borrowed
|
2016-01-23 15:15:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 16:03:21 +00:00
|
|
|
fn get_entry(&mut self) -> Result<Entry> {
|
|
|
|
if !self.is_borrowed() {
|
2016-07-22 11:39:29 +00:00
|
|
|
let file = self.file.get_file_content();
|
2016-01-23 16:03:21 +00:00
|
|
|
if let Err(err) = file {
|
2016-05-14 17:05:54 +00:00
|
|
|
if err.err_type() == SEK::FileNotFound {
|
2016-01-23 16:03:21 +00:00
|
|
|
Ok(Entry::new(self.id.clone()))
|
|
|
|
} else {
|
|
|
|
Err(err)
|
|
|
|
}
|
2016-01-23 15:15:34 +00:00
|
|
|
} else {
|
2016-01-23 16:03:21 +00:00
|
|
|
// TODO:
|
2016-07-22 11:39:29 +00:00
|
|
|
let entry = Entry::from_reader(self.id.clone(), &mut file.unwrap());
|
2016-01-29 17:17:41 +00:00
|
|
|
entry
|
2016-01-23 15:15:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-14 17:05:54 +00:00
|
|
|
Err(SE::new(SEK::EntryAlreadyBorrowed, None))
|
2016-01-23 15:15:34 +00:00
|
|
|
}
|
2016-01-16 19:30:16 +00:00
|
|
|
}
|
2016-01-24 18:55:47 +00:00
|
|
|
|
|
|
|
fn write_entry(&mut self, entry: &Entry) -> Result<()> {
|
|
|
|
if self.is_borrowed() {
|
|
|
|
assert_eq!(self.id, entry.location);
|
2016-07-22 11:39:29 +00:00
|
|
|
self.file.write_file_content(entry.to_str().as_bytes())
|
|
|
|
.map_err_into(SEK::FileError)
|
|
|
|
.map(|_| ())
|
2016-03-21 18:45:09 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
2016-01-24 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-16 19:30:16 +00:00
|
|
|
}
|
2016-01-16 18:52:06 +00:00
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// The Store itself, through this object one can interact with IMAG's entries
|
2016-01-16 18:04:15 +00:00
|
|
|
pub struct Store {
|
|
|
|
location: PathBuf,
|
2016-01-13 20:47:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
/**
|
|
|
|
* Configuration object of the store
|
|
|
|
*/
|
2016-03-05 17:16:05 +00:00
|
|
|
configuration: Option<Value>,
|
2016-03-05 17:08:31 +00:00
|
|
|
|
2016-02-15 21:15:32 +00:00
|
|
|
/*
|
|
|
|
* Registered hooks
|
|
|
|
*/
|
|
|
|
|
2016-05-26 16:40:58 +00:00
|
|
|
store_unload_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
pre_create_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
post_create_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
pre_retrieve_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
post_retrieve_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
pre_update_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
post_update_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
pre_delete_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
post_delete_aspects : Arc<Mutex<Vec<Aspect>>>,
|
2016-03-19 14:06:10 +00:00
|
|
|
pre_move_aspects : Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
post_move_aspects : Arc<Mutex<Vec<Aspect>>>,
|
2016-02-15 21:15:32 +00:00
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
/**
|
|
|
|
* Internal Path->File cache map
|
|
|
|
*
|
|
|
|
* Caches the files, so they remain flock()ed
|
|
|
|
*
|
|
|
|
* Could be optimized for a threadsafe HashMap
|
|
|
|
*/
|
2016-01-16 19:30:16 +00:00
|
|
|
entries: Arc<RwLock<HashMap<StoreId, StoreEntry>>>,
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Store {
|
2016-01-17 15:04:31 +00:00
|
|
|
|
|
|
|
/// Create a new Store object
|
2016-03-05 17:16:05 +00:00
|
|
|
pub fn new(location: PathBuf, store_config: Option<Value>) -> Result<Store> {
|
2016-03-05 10:37:23 +00:00
|
|
|
use configuration::*;
|
|
|
|
|
|
|
|
debug!("Validating Store configuration");
|
2016-09-19 19:00:45 +00:00
|
|
|
let _ = try!(config_is_valid(&store_config).map_err_into(SEK::ConfigurationError));
|
2016-01-17 16:42:40 +00:00
|
|
|
|
2016-01-28 20:06:49 +00:00
|
|
|
debug!("Building new Store object");
|
2016-01-17 16:42:40 +00:00
|
|
|
if !location.exists() {
|
2016-07-16 20:37:07 +00:00
|
|
|
if !config_implicit_store_create_allowed(store_config.as_ref()) {
|
|
|
|
warn!("Implicitely creating store directory is denied");
|
|
|
|
warn!(" -> Either because configuration does not allow it");
|
|
|
|
warn!(" -> or because there is no configuration");
|
|
|
|
return Err(SEK::CreateStoreDirDenied.into_error())
|
|
|
|
.map_err_into(SEK::FileError)
|
|
|
|
.map_err_into(SEK::IoError);
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:06:49 +00:00
|
|
|
debug!("Creating store path");
|
2016-07-22 13:12:56 +00:00
|
|
|
let c = FileAbstraction::create_dir_all(&location);
|
2016-01-18 22:00:51 +00:00
|
|
|
if c.is_err() {
|
2016-01-28 20:06:49 +00:00
|
|
|
debug!("Failed");
|
2016-06-27 16:16:43 +00:00
|
|
|
return Err(SEK::StorePathCreate.into_error_with_cause(Box::new(c.unwrap_err())));
|
2016-01-18 22:00:51 +00:00
|
|
|
}
|
2016-05-03 21:10:32 +00:00
|
|
|
} else if location.is_file() {
|
|
|
|
debug!("Store path exists as file");
|
2016-06-27 16:16:43 +00:00
|
|
|
return Err(SEK::StorePathExists.into_error());
|
2016-01-17 16:42:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 16:40:58 +00:00
|
|
|
let store_unload_aspects = get_store_unload_aspect_names(&store_config)
|
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let pre_create_aspects = get_pre_create_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let post_create_aspects = get_post_create_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let pre_retrieve_aspects = get_pre_retrieve_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let post_retrieve_aspects = get_post_retrieve_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let pre_update_aspects = get_pre_update_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let post_update_aspects = get_post_update_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let pre_delete_aspects = get_pre_delete_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-05 17:08:31 +00:00
|
|
|
let post_delete_aspects = get_post_delete_aspect_names(&store_config)
|
2016-03-05 17:35:50 +00:00
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
2016-03-05 10:37:23 +00:00
|
|
|
|
2016-03-19 14:06:10 +00:00
|
|
|
let pre_move_aspects = get_pre_move_aspect_names(&store_config)
|
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
let post_move_aspects = get_post_move_aspect_names(&store_config)
|
|
|
|
.into_iter().map(|n| {
|
|
|
|
let cfg = AspectConfig::get_for(&store_config, n.clone());
|
|
|
|
Aspect::new(n, cfg)
|
|
|
|
}).collect();
|
|
|
|
|
2016-03-05 10:37:23 +00:00
|
|
|
let store = Store {
|
2016-05-26 16:40:58 +00:00
|
|
|
location: location.clone(),
|
2016-03-05 17:08:31 +00:00
|
|
|
configuration: store_config,
|
2016-05-26 16:40:58 +00:00
|
|
|
|
|
|
|
store_unload_aspects : Arc::new(Mutex::new(store_unload_aspects)),
|
|
|
|
|
2016-03-05 10:37:23 +00:00
|
|
|
pre_create_aspects : Arc::new(Mutex::new(pre_create_aspects)),
|
|
|
|
post_create_aspects : Arc::new(Mutex::new(post_create_aspects)),
|
|
|
|
pre_retrieve_aspects : Arc::new(Mutex::new(pre_retrieve_aspects)),
|
|
|
|
post_retrieve_aspects : Arc::new(Mutex::new(post_retrieve_aspects)),
|
|
|
|
pre_update_aspects : Arc::new(Mutex::new(pre_update_aspects)),
|
|
|
|
post_update_aspects : Arc::new(Mutex::new(post_update_aspects)),
|
|
|
|
pre_delete_aspects : Arc::new(Mutex::new(pre_delete_aspects)),
|
|
|
|
post_delete_aspects : Arc::new(Mutex::new(post_delete_aspects)),
|
2016-03-19 14:06:10 +00:00
|
|
|
pre_move_aspects : Arc::new(Mutex::new(pre_move_aspects)),
|
|
|
|
post_move_aspects : Arc::new(Mutex::new(post_move_aspects)),
|
2016-01-17 15:04:31 +00:00
|
|
|
entries: Arc::new(RwLock::new(HashMap::new())),
|
2016-03-05 10:37:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
debug!("Store building succeeded");
|
2016-07-15 22:15:10 +00:00
|
|
|
debug!("------------------------");
|
|
|
|
debug!("{:?}", store);
|
|
|
|
debug!("------------------------");
|
|
|
|
|
2016-03-05 10:37:23 +00:00
|
|
|
Ok(store)
|
2016-01-17 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 14:09:21 +00:00
|
|
|
/// Get the store configuration
|
|
|
|
pub fn config(&self) -> Option<&Value> {
|
|
|
|
self.configuration.as_ref()
|
|
|
|
}
|
|
|
|
|
2016-07-16 22:36:37 +00:00
|
|
|
/// Verify the store.
|
|
|
|
///
|
|
|
|
/// This function is not intended to be called by normal programs but only by `imag-store`.
|
|
|
|
#[cfg(feature = "verify")]
|
|
|
|
pub fn verify(&self) -> bool {
|
|
|
|
info!("Header | Content length | Path");
|
|
|
|
info!("-------+----------------+-----");
|
|
|
|
|
|
|
|
WalkDir::new(self.location.clone())
|
|
|
|
.into_iter()
|
|
|
|
.map(|res| {
|
|
|
|
match res {
|
|
|
|
Ok(dent) => {
|
|
|
|
if dent.file_type().is_file() {
|
|
|
|
match self.get(PathBuf::from(dent.path())) {
|
|
|
|
Ok(Some(fle)) => {
|
|
|
|
let p = fle.get_location();
|
|
|
|
let content_len = fle.get_content().len();
|
|
|
|
let header = if fle.get_header().verify().is_ok() {
|
|
|
|
"ok"
|
|
|
|
} else {
|
|
|
|
"broken"
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("{: >6} | {: >14} | {:?}", header, content_len, p.deref());
|
|
|
|
},
|
|
|
|
|
|
|
|
Ok(None) => {
|
|
|
|
info!("{: >6} | {: >14} | {:?}", "?", "couldn't load", dent.path());
|
|
|
|
},
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
debug!("{:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info!("{: >6} | {: >14} | {:?}", "?", "<no file>", dent.path());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
debug!("{:?}", e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
})
|
|
|
|
.all(|b| b)
|
|
|
|
}
|
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Creates the Entry at the given location (inside the entry)
|
2016-05-03 13:49:33 +00:00
|
|
|
pub fn create<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
|
2016-08-25 15:56:55 +00:00
|
|
|
let id = try!(id.into_storeid()).with_base(self.path().clone());
|
2016-03-04 19:33:08 +00:00
|
|
|
if let Err(e) = self.execute_hooks_for_id(self.pre_create_aspects.clone(), &id) {
|
2016-07-06 16:01:00 +00:00
|
|
|
return Err(e)
|
|
|
|
.map_err_into(SEK::PreHookExecuteError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-07-06 16:01:00 +00:00
|
|
|
.map_err_into(SEK::CreateCallError)
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 17:59:43 +00:00
|
|
|
{
|
|
|
|
let mut hsmap = match self.entries.write() {
|
|
|
|
Err(_) => return Err(SEK::LockPoisoned.into_error()).map_err_into(SEK::CreateCallError),
|
|
|
|
Ok(s) => s,
|
|
|
|
};
|
|
|
|
|
|
|
|
if hsmap.contains_key(&id) {
|
|
|
|
return Err(SEK::EntryAlreadyExists.into_error()).map_err_into(SEK::CreateCallError);
|
|
|
|
}
|
|
|
|
hsmap.insert(id.clone(), {
|
2016-09-05 14:51:37 +00:00
|
|
|
let mut se = try!(StoreEntry::new(id.clone()));
|
2016-08-01 17:59:43 +00:00
|
|
|
se.status = StoreEntryStatus::Borrowed;
|
|
|
|
se
|
|
|
|
});
|
2016-01-24 16:28:52 +00:00
|
|
|
}
|
2016-02-16 19:42:51 +00:00
|
|
|
|
2016-07-04 10:46:06 +00:00
|
|
|
let mut fle = FileLockEntry::new(self, Entry::new(id));
|
|
|
|
self.execute_hooks_for_mut_file(self.post_create_aspects.clone(), &mut fle)
|
|
|
|
.map_err_into(SEK::PostHookExecuteError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::CreateCallError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map(|_| fle)
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
2016-01-16 19:18:43 +00:00
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Borrow a given Entry. When the `FileLockEntry` is either `update`d or
|
|
|
|
/// dropped, the new Entry is written to disk
|
2016-05-12 12:52:33 +00:00
|
|
|
///
|
|
|
|
/// Implicitely creates a entry in the store if there is no entry with the id `id`. For a
|
|
|
|
/// non-implicitely-create look at `Store::get`.
|
2016-05-03 13:49:33 +00:00
|
|
|
pub fn retrieve<'a, S: IntoStoreId>(&'a self, id: S) -> Result<FileLockEntry<'a>> {
|
2016-08-25 15:56:55 +00:00
|
|
|
let id = try!(id.into_storeid()).with_base(self.path().clone());
|
2016-03-04 19:33:08 +00:00
|
|
|
if let Err(e) = self.execute_hooks_for_id(self.pre_retrieve_aspects.clone(), &id) {
|
2016-07-06 16:01:00 +00:00
|
|
|
return Err(e)
|
|
|
|
.map_err_into(SEK::PreHookExecuteError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-07-06 16:01:00 +00:00
|
|
|
.map_err_into(SEK::RetrieveCallError)
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:31:11 +00:00
|
|
|
let entry = try!({
|
|
|
|
self.entries
|
|
|
|
.write()
|
|
|
|
.map_err(|_| SE::new(SEK::LockPoisoned, None))
|
|
|
|
.and_then(|mut es| {
|
2016-09-05 14:51:37 +00:00
|
|
|
let new_se = try!(StoreEntry::new(id.clone()));
|
|
|
|
let mut se = es.entry(id.clone()).or_insert(new_se);
|
2016-08-01 18:31:11 +00:00
|
|
|
let entry = se.get_entry();
|
|
|
|
se.status = StoreEntryStatus::Borrowed;
|
|
|
|
entry
|
|
|
|
})
|
|
|
|
.map_err_into(SEK::RetrieveCallError)
|
|
|
|
});
|
2016-08-01 18:28:38 +00:00
|
|
|
|
|
|
|
let mut fle = FileLockEntry::new(self, entry);
|
|
|
|
self.execute_hooks_for_mut_file(self.post_retrieve_aspects.clone(), &mut fle)
|
|
|
|
.map_err_into(SEK::PostHookExecuteError)
|
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::RetrieveCallError)
|
2016-08-01 18:28:38 +00:00
|
|
|
.and(Ok(fle))
|
2016-05-12 12:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an entry from the store if it exists.
|
|
|
|
///
|
|
|
|
/// This executes the {pre,post}_retrieve_aspects hooks.
|
2016-05-27 10:23:26 +00:00
|
|
|
pub fn get<'a, S: IntoStoreId + Clone>(&'a self, id: S) -> Result<Option<FileLockEntry<'a>>> {
|
2016-09-20 14:52:14 +00:00
|
|
|
let id = try!(id.into_storeid()).with_base(self.path().clone());
|
|
|
|
|
|
|
|
let exists = try!(self.entries
|
|
|
|
.read()
|
|
|
|
.map(|map| map.contains_key(&id))
|
|
|
|
.map_err(|_| SE::new(SEK::LockPoisoned, None))
|
|
|
|
.map_err_into(SEK::GetCallError)
|
|
|
|
);
|
|
|
|
|
|
|
|
if !exists && !id.exists() {
|
|
|
|
debug!("Does not exist in internal cache or filesystem: {:?}", id);
|
2016-05-27 10:23:26 +00:00
|
|
|
return Ok(None);
|
2016-05-12 12:52:33 +00:00
|
|
|
}
|
2016-09-20 14:52:14 +00:00
|
|
|
|
2016-06-27 16:16:43 +00:00
|
|
|
self.retrieve(id).map(Some).map_err_into(SEK::GetCallError)
|
2016-05-12 12:52:33 +00:00
|
|
|
}
|
2016-01-16 19:18:43 +00:00
|
|
|
|
2016-01-24 11:17:41 +00:00
|
|
|
/// Iterate over all StoreIds for one module name
|
2016-03-13 13:32:48 +00:00
|
|
|
pub fn retrieve_for_module(&self, mod_name: &str) -> Result<StoreIdIterator> {
|
|
|
|
let mut path = self.path().clone();
|
|
|
|
path.push(mod_name);
|
|
|
|
|
2016-05-14 17:10:25 +00:00
|
|
|
path.to_str()
|
|
|
|
.ok_or(SE::new(SEK::EncodingError, None))
|
|
|
|
.and_then(|path| {
|
2016-05-28 15:10:34 +00:00
|
|
|
let path = [ path, "/**/*" ].join("");
|
2016-05-14 17:10:25 +00:00
|
|
|
debug!("glob()ing with '{}'", path);
|
2016-06-27 16:16:43 +00:00
|
|
|
glob(&path[..]).map_err_into(SEK::GlobError)
|
2016-05-14 17:10:25 +00:00
|
|
|
})
|
2016-08-22 10:22:39 +00:00
|
|
|
.map(|paths| GlobStoreIdIterator::new(paths, self.path().clone()).into())
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::GlobError)
|
|
|
|
.map_err_into(SEK::RetrieveForModuleCallError)
|
2016-01-24 11:17:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 15:03:41 +00:00
|
|
|
// Walk the store tree for the module
|
|
|
|
pub fn walk<'a>(&'a self, mod_name: &str) -> Walk {
|
|
|
|
Walk::new(self.path().clone(), mod_name)
|
|
|
|
}
|
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Return the `FileLockEntry` and write to disk
|
2016-02-22 18:24:23 +00:00
|
|
|
pub fn update<'a>(&'a self, mut entry: FileLockEntry<'a>) -> Result<()> {
|
2016-03-04 19:33:08 +00:00
|
|
|
if let Err(e) = self.execute_hooks_for_mut_file(self.pre_update_aspects.clone(), &mut entry) {
|
2016-07-06 16:01:00 +00:00
|
|
|
return Err(e)
|
|
|
|
.map_err_into(SEK::PreHookExecuteError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-07-06 16:01:00 +00:00
|
|
|
.map_err_into(SEK::UpdateCallError);
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = self._update(&entry) {
|
2016-06-27 16:16:43 +00:00
|
|
|
return Err(e).map_err_into(SEK::UpdateCallError);
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
self.execute_hooks_for_mut_file(self.post_update_aspects.clone(), &mut entry)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::PostHookExecuteError)
|
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::UpdateCallError)
|
2016-01-17 14:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal method to write to the filesystem store.
|
|
|
|
///
|
|
|
|
/// # Assumptions
|
|
|
|
/// This method assumes that entry is dropped _right after_ the call, hence
|
|
|
|
/// it is not public.
|
|
|
|
fn _update<'a>(&'a self, entry: &FileLockEntry<'a>) -> Result<()> {
|
2016-05-14 17:12:13 +00:00
|
|
|
let mut hsmap = match self.entries.write() {
|
|
|
|
Err(_) => return Err(SE::new(SEK::LockPoisoned, None)),
|
|
|
|
Ok(e) => e,
|
|
|
|
};
|
|
|
|
|
2016-05-26 16:22:14 +00:00
|
|
|
let mut se = try!(hsmap.get_mut(&entry.location).ok_or(SE::new(SEK::IdNotFound, None)));
|
2016-01-24 18:55:47 +00:00
|
|
|
|
2016-01-29 15:50:20 +00:00
|
|
|
assert!(se.is_borrowed(), "Tried to update a non borrowed entry.");
|
2016-01-24 18:55:47 +00:00
|
|
|
|
2016-02-06 18:39:20 +00:00
|
|
|
debug!("Verifying Entry");
|
2016-02-06 17:50:39 +00:00
|
|
|
try!(entry.entry.verify());
|
|
|
|
|
2016-02-06 18:39:20 +00:00
|
|
|
debug!("Writing Entry");
|
2016-01-24 18:55:47 +00:00
|
|
|
try!(se.write_entry(&entry.entry));
|
|
|
|
se.status = StoreEntryStatus::Present;
|
|
|
|
|
|
|
|
Ok(())
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
2016-01-16 19:18:43 +00:00
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Retrieve a copy of a given entry, this cannot be used to mutate
|
|
|
|
/// the one on disk
|
2016-05-03 13:49:33 +00:00
|
|
|
pub fn retrieve_copy<S: IntoStoreId>(&self, id: S) -> Result<Entry> {
|
2016-08-25 15:56:55 +00:00
|
|
|
let id = try!(id.into_storeid()).with_base(self.path().clone());
|
2016-05-14 17:14:11 +00:00
|
|
|
let entries = match self.entries.write() {
|
2016-05-28 21:30:50 +00:00
|
|
|
Err(_) => {
|
|
|
|
return Err(SE::new(SEK::LockPoisoned, None))
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::RetrieveCopyCallError);
|
2016-05-28 21:30:50 +00:00
|
|
|
},
|
2016-05-14 17:14:11 +00:00
|
|
|
Ok(e) => e,
|
|
|
|
};
|
2016-01-25 21:26:00 +00:00
|
|
|
|
|
|
|
// if the entry is currently modified by the user, we cannot drop it
|
|
|
|
if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) {
|
2016-06-27 16:16:43 +00:00
|
|
|
return Err(SE::new(SEK::IdLocked, None)).map_err_into(SEK::RetrieveCopyCallError);
|
2016-01-25 21:26:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 14:51:37 +00:00
|
|
|
try!(StoreEntry::new(id)).get_entry()
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
2016-01-16 19:18:43 +00:00
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// Delete an entry
|
2016-05-03 13:49:33 +00:00
|
|
|
pub fn delete<S: IntoStoreId>(&self, id: S) -> Result<()> {
|
2016-08-25 15:56:55 +00:00
|
|
|
let id = try!(id.into_storeid()).with_base(self.path().clone());
|
2016-03-04 19:33:08 +00:00
|
|
|
if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) {
|
2016-07-06 16:01:00 +00:00
|
|
|
return Err(e)
|
|
|
|
.map_err_into(SEK::PreHookExecuteError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-07-06 16:01:00 +00:00
|
|
|
.map_err_into(SEK::DeleteCallError)
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:31:11 +00:00
|
|
|
{
|
|
|
|
let mut entries = match self.entries.write() {
|
|
|
|
Err(_) => return Err(SE::new(SEK::LockPoisoned, None))
|
|
|
|
.map_err_into(SEK::DeleteCallError),
|
|
|
|
Ok(e) => e,
|
|
|
|
};
|
2016-01-17 15:22:50 +00:00
|
|
|
|
2016-08-01 18:31:11 +00:00
|
|
|
// if the entry is currently modified by the user, we cannot drop it
|
2016-09-19 09:07:38 +00:00
|
|
|
match entries.get(&id) {
|
|
|
|
None => {
|
|
|
|
return Err(SEK::FileNotFound.into_error()).map_err_into(SEK::DeleteCallError)
|
|
|
|
},
|
|
|
|
Some(e) => if e.is_borrowed() {
|
|
|
|
return Err(SE::new(SEK::IdLocked, None)).map_err_into(SEK::DeleteCallError)
|
|
|
|
}
|
2016-08-01 18:31:11 +00:00
|
|
|
}
|
2016-01-17 15:22:50 +00:00
|
|
|
|
2016-08-01 18:31:11 +00:00
|
|
|
// remove the entry first, then the file
|
|
|
|
entries.remove(&id);
|
2016-09-05 14:51:37 +00:00
|
|
|
let pb = try!(id.clone().with_base(self.path().clone()).into_pathbuf());
|
|
|
|
if let Err(e) = FileAbstraction::remove_file(&pb) {
|
2016-08-01 18:31:11 +00:00
|
|
|
return Err(SEK::FileError.into_error_with_cause(Box::new(e)))
|
|
|
|
.map_err_into(SEK::DeleteCallError);
|
|
|
|
}
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
self.execute_hooks_for_id(self.post_delete_aspects.clone(), &id)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::PostHookExecuteError)
|
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::DeleteCallError)
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
2016-02-06 17:46:54 +00:00
|
|
|
|
2016-04-14 15:15:19 +00:00
|
|
|
/// Save a copy of the Entry in another place
|
|
|
|
/// Executes the post_move_aspects for the new id
|
|
|
|
pub fn save_to(&self, entry: &FileLockEntry, new_id: StoreId) -> Result<()> {
|
2016-04-17 10:30:34 +00:00
|
|
|
self.save_to_other_location(entry, new_id, false)
|
2016-04-14 15:15:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 12:40:06 +00:00
|
|
|
/// Save an Entry in another place
|
|
|
|
/// Removes the original entry
|
|
|
|
/// Executes the post_move_aspects for the new id
|
2016-02-21 14:34:49 +00:00
|
|
|
pub fn save_as(&self, entry: FileLockEntry, new_id: StoreId) -> Result<()> {
|
2016-04-17 10:30:34 +00:00
|
|
|
self.save_to_other_location(&entry, new_id, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_to_other_location(&self, entry: &FileLockEntry, new_id: StoreId, remove_old: bool)
|
|
|
|
-> Result<()>
|
|
|
|
{
|
2016-08-25 15:56:55 +00:00
|
|
|
let new_id = new_id.with_base(self.path().clone());
|
2016-03-17 12:40:06 +00:00
|
|
|
let hsmap = self.entries.write();
|
|
|
|
if hsmap.is_err() {
|
2016-06-27 16:16:43 +00:00
|
|
|
return Err(SE::new(SEK::LockPoisoned, None)).map_err_into(SEK::MoveCallError)
|
2016-03-17 12:40:06 +00:00
|
|
|
}
|
|
|
|
if hsmap.unwrap().contains_key(&new_id) {
|
2016-06-27 16:16:43 +00:00
|
|
|
return Err(SE::new(SEK::EntryAlreadyExists, None)).map_err_into(SEK::MoveCallError)
|
2016-03-17 12:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let old_id = entry.get_location().clone();
|
|
|
|
|
2016-09-05 14:51:37 +00:00
|
|
|
let old_id_as_path = try!(old_id.clone().with_base(self.path().clone()).into_pathbuf());
|
|
|
|
let new_id_as_path = try!(new_id.clone().with_base(self.path().clone()).into_pathbuf());
|
|
|
|
FileAbstraction::copy(&old_id_as_path, &new_id_as_path)
|
2016-04-17 10:30:34 +00:00
|
|
|
.and_then(|_| {
|
|
|
|
if remove_old {
|
2016-09-05 14:51:37 +00:00
|
|
|
FileAbstraction::remove_file(&old_id_as_path)
|
2016-04-17 10:30:34 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
})
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::FileError)
|
2016-06-08 13:06:18 +00:00
|
|
|
.and_then(|_| self.execute_hooks_for_id(self.post_move_aspects.clone(), &new_id)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::PostHookExecuteError)
|
|
|
|
.map_err_into(SEK::HookExecutionError))
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::MoveCallError)
|
2016-02-21 14:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Move an entry without loading
|
|
|
|
pub fn move_by_id(&self, old_id: StoreId, new_id: StoreId) -> Result<()> {
|
2016-03-17 12:39:32 +00:00
|
|
|
|
2016-08-25 15:56:55 +00:00
|
|
|
let new_id = new_id.with_base(self.path().clone());
|
|
|
|
let old_id = old_id.with_base(self.path().clone());
|
2016-03-19 14:06:10 +00:00
|
|
|
|
|
|
|
if let Err(e) = self.execute_hooks_for_id(self.pre_move_aspects.clone(), &old_id) {
|
2016-07-06 16:01:00 +00:00
|
|
|
return Err(e)
|
|
|
|
.map_err_into(SEK::PreHookExecuteError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-07-06 16:01:00 +00:00
|
|
|
.map_err_into(SEK::MoveByIdCallError)
|
2016-03-19 14:06:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:32:04 +00:00
|
|
|
{
|
2016-08-25 06:57:25 +00:00
|
|
|
let hsmap = match self.entries.write() {
|
|
|
|
Err(_) => return Err(SE::new(SEK::LockPoisoned, None)),
|
|
|
|
Ok(m) => m,
|
|
|
|
};
|
|
|
|
|
2016-08-22 14:55:54 +00:00
|
|
|
if hsmap.contains_key(&old_id) {
|
2016-08-01 18:32:04 +00:00
|
|
|
return Err(SE::new(SEK::EntryAlreadyBorrowed, None));
|
|
|
|
} else {
|
2016-09-05 14:51:37 +00:00
|
|
|
let old_id_pb = try!(old_id.clone().with_base(self.path().clone()).into_pathbuf());
|
|
|
|
let new_id_pb = try!(new_id.clone().with_base(self.path().clone()).into_pathbuf());
|
2016-08-25 06:57:25 +00:00
|
|
|
match FileAbstraction::rename(&old_id_pb, &new_id_pb) {
|
2016-08-01 18:32:04 +00:00
|
|
|
Err(e) => return Err(SEK::EntryRenameError.into_error_with_cause(Box::new(e))),
|
2016-08-25 07:30:47 +00:00
|
|
|
Ok(_) => {
|
2016-08-01 18:32:04 +00:00
|
|
|
debug!("Rename worked");
|
2016-07-22 13:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 07:30:47 +00:00
|
|
|
}
|
2016-08-01 18:32:04 +00:00
|
|
|
|
2016-03-17 12:39:32 +00:00
|
|
|
}
|
2016-03-19 14:06:10 +00:00
|
|
|
|
|
|
|
self.execute_hooks_for_id(self.pre_move_aspects.clone(), &new_id)
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::PostHookExecuteError)
|
2016-07-15 22:55:34 +00:00
|
|
|
.map_err_into(SEK::HookExecutionError)
|
2016-06-27 16:16:43 +00:00
|
|
|
.map_err_into(SEK::MoveByIdCallError)
|
2016-02-21 14:34:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 15:50:39 +00:00
|
|
|
/// Gets the path where this store is on the disk
|
|
|
|
pub fn path(&self) -> &PathBuf {
|
|
|
|
&self.location
|
|
|
|
}
|
2016-03-10 17:14:53 +00:00
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
pub fn register_hook(&mut self,
|
|
|
|
position: HookPosition,
|
2016-05-03 21:10:32 +00:00
|
|
|
aspect_name: &str,
|
2016-03-05 17:10:38 +00:00
|
|
|
mut h: Box<Hook>)
|
2016-03-04 19:33:08 +00:00
|
|
|
-> Result<()>
|
2016-02-16 19:42:51 +00:00
|
|
|
{
|
2016-03-04 19:33:08 +00:00
|
|
|
debug!("Registering hook: {:?}", h);
|
|
|
|
debug!(" in position: {:?}", position);
|
|
|
|
debug!(" with aspect: {:?}", aspect_name);
|
|
|
|
|
2016-03-21 18:40:19 +00:00
|
|
|
let guard = match position {
|
2016-05-26 16:40:58 +00:00
|
|
|
HookPosition::StoreUnload => self.store_unload_aspects.clone(),
|
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
HookPosition::PreCreate => self.pre_create_aspects.clone(),
|
|
|
|
HookPosition::PostCreate => self.post_create_aspects.clone(),
|
|
|
|
HookPosition::PreRetrieve => self.pre_retrieve_aspects.clone(),
|
|
|
|
HookPosition::PostRetrieve => self.post_retrieve_aspects.clone(),
|
|
|
|
HookPosition::PreUpdate => self.pre_update_aspects.clone(),
|
|
|
|
HookPosition::PostUpdate => self.post_update_aspects.clone(),
|
|
|
|
HookPosition::PreDelete => self.pre_delete_aspects.clone(),
|
|
|
|
HookPosition::PostDelete => self.post_delete_aspects.clone(),
|
|
|
|
};
|
|
|
|
|
2016-05-14 17:15:51 +00:00
|
|
|
let mut guard = match guard.deref().lock().map_err(|_| SE::new(SEK::LockError, None)) {
|
2016-06-27 16:16:43 +00:00
|
|
|
Err(e) => return Err(SEK::HookRegisterError.into_error_with_cause(Box::new(e))),
|
2016-05-14 17:15:51 +00:00
|
|
|
Ok(g) => g,
|
|
|
|
};
|
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
for mut aspect in guard.deref_mut() {
|
|
|
|
if aspect.name().clone() == aspect_name.clone() {
|
2016-07-15 22:27:54 +00:00
|
|
|
debug!("Trying to find configuration for hook: {:?}", h);
|
2016-03-05 17:10:38 +00:00
|
|
|
self.get_config_for_hook(h.name()).map(|config| h.set_config(config));
|
2016-07-15 22:27:54 +00:00
|
|
|
debug!("Trying to register hook in aspect: {:?} <- {:?}", aspect, h);
|
2016-03-04 19:33:08 +00:00
|
|
|
aspect.register_hook(h);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 12:29:04 +00:00
|
|
|
|
2016-06-27 16:16:43 +00:00
|
|
|
let annfe = SEK::AspectNameNotFoundError.into_error();
|
|
|
|
Err(SEK::HookRegisterError.into_error_with_cause(Box::new(annfe)))
|
2016-02-22 18:24:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 17:10:38 +00:00
|
|
|
fn get_config_for_hook(&self, name: &str) -> Option<&Value> {
|
2016-05-03 21:10:32 +00:00
|
|
|
match self.configuration {
|
|
|
|
Some(Value::Table(ref tabl)) => {
|
2016-07-15 22:28:27 +00:00
|
|
|
debug!("Trying to head 'hooks' section from {:?}", tabl);
|
2016-03-05 17:10:38 +00:00
|
|
|
tabl.get("hooks")
|
|
|
|
.map(|hook_section| {
|
2016-07-15 22:28:27 +00:00
|
|
|
debug!("Found hook section: {:?}", hook_section);
|
|
|
|
debug!("Reading section key: {:?}", name);
|
2016-05-03 21:10:32 +00:00
|
|
|
match *hook_section {
|
|
|
|
Value::Table(ref tabl) => tabl.get(name),
|
2016-03-05 17:10:38 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(None)
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
fn execute_hooks_for_id(&self,
|
|
|
|
aspects: Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
id: &StoreId)
|
2016-04-18 15:25:19 +00:00
|
|
|
-> HookResult<()>
|
2016-02-16 19:42:51 +00:00
|
|
|
{
|
2016-07-14 13:28:50 +00:00
|
|
|
match aspects.lock() {
|
|
|
|
Err(_) => return Err(HookErrorKind::HookExecutionError.into()),
|
|
|
|
Ok(g) => g
|
|
|
|
}.iter().fold_defresult(|aspect| {
|
|
|
|
debug!("[Aspect][exec]: {:?}", aspect);
|
|
|
|
(aspect as &StoreIdAccessor).access(id)
|
|
|
|
}).map_err(Box::new)
|
|
|
|
.map_err(|e| HookErrorKind::HookExecutionError.into_error_with_cause(e))
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:33:08 +00:00
|
|
|
fn execute_hooks_for_mut_file(&self,
|
|
|
|
aspects: Arc<Mutex<Vec<Aspect>>>,
|
|
|
|
fle: &mut FileLockEntry)
|
2016-04-18 15:25:19 +00:00
|
|
|
-> HookResult<()>
|
2016-02-16 19:42:51 +00:00
|
|
|
{
|
2016-07-14 13:28:50 +00:00
|
|
|
match aspects.lock() {
|
|
|
|
Err(_) => return Err(HookErrorKind::HookExecutionError.into()),
|
|
|
|
Ok(g) => g
|
|
|
|
}.iter().fold_defresult(|aspect| {
|
|
|
|
debug!("[Aspect][exec]: {:?}", aspect);
|
|
|
|
aspect.access_mut(fle)
|
|
|
|
}).map_err(Box::new)
|
|
|
|
.map_err(|e| HookErrorKind::HookExecutionError.into_error_with_cause(e))
|
2016-02-16 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 12:29:20 +00:00
|
|
|
impl Debug for Store {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FMTError> {
|
|
|
|
try!(write!(fmt, " --- Store ---\n"));
|
|
|
|
try!(write!(fmt, "\n"));
|
|
|
|
try!(write!(fmt, " - location : {:?}\n", self.location));
|
|
|
|
try!(write!(fmt, " - configuration : {:?}\n", self.configuration));
|
|
|
|
try!(write!(fmt, " - pre_create_aspects : {:?}\n", self.pre_create_aspects ));
|
|
|
|
try!(write!(fmt, " - post_create_aspects : {:?}\n", self.post_create_aspects ));
|
|
|
|
try!(write!(fmt, " - pre_retrieve_aspects : {:?}\n", self.pre_retrieve_aspects ));
|
|
|
|
try!(write!(fmt, " - post_retrieve_aspects : {:?}\n", self.post_retrieve_aspects ));
|
|
|
|
try!(write!(fmt, " - pre_update_aspects : {:?}\n", self.pre_update_aspects ));
|
|
|
|
try!(write!(fmt, " - post_update_aspects : {:?}\n", self.post_update_aspects ));
|
|
|
|
try!(write!(fmt, " - pre_delete_aspects : {:?}\n", self.pre_delete_aspects ));
|
|
|
|
try!(write!(fmt, " - post_delete_aspects : {:?}\n", self.post_delete_aspects ));
|
|
|
|
try!(write!(fmt, "\n"));
|
|
|
|
try!(write!(fmt, "Entries:\n"));
|
|
|
|
try!(write!(fmt, "{:?}", self.entries));
|
|
|
|
try!(write!(fmt, "\n"));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
impl Drop for Store {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock all files on drop
|
|
|
|
*
|
2016-01-23 15:15:34 +00:00
|
|
|
* TODO: Unlock them
|
2016-01-16 18:04:15 +00:00
|
|
|
*/
|
|
|
|
fn drop(&mut self) {
|
2016-08-25 15:08:04 +00:00
|
|
|
match StoreId::new(Some(self.location.clone()), PathBuf::from(".")) {
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
|
|
|
warn!("Cannot construct StoreId for Store to execute hooks!");
|
|
|
|
warn!("Will close Store without executing hooks!");
|
|
|
|
},
|
|
|
|
Ok(store_id) => {
|
|
|
|
if let Err(e) = self.execute_hooks_for_id(self.store_unload_aspects.clone(), &store_id) {
|
|
|
|
debug!("Store-load hooks execution failed. Cannot create store object.");
|
|
|
|
warn!("Store Unload Hook error: {:?}", e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2016-05-26 16:40:58 +00:00
|
|
|
|
2016-01-28 20:06:49 +00:00
|
|
|
debug!("Dropping store");
|
2016-01-16 18:04:15 +00:00
|
|
|
}
|
2016-01-13 20:51:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-16 19:53:38 +00:00
|
|
|
/// A struct that allows you to borrow an Entry
|
2016-01-16 18:04:15 +00:00
|
|
|
pub struct FileLockEntry<'a> {
|
|
|
|
store: &'a Store,
|
2016-01-16 18:32:12 +00:00
|
|
|
entry: Entry,
|
2016-01-16 17:25:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 18:04:15 +00:00
|
|
|
impl<'a> FileLockEntry<'a, > {
|
2016-05-26 16:22:14 +00:00
|
|
|
fn new(store: &'a Store, entry: Entry) -> FileLockEntry<'a> {
|
2016-01-16 17:25:48 +00:00
|
|
|
FileLockEntry {
|
|
|
|
store: store,
|
|
|
|
entry: entry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:26:45 +00:00
|
|
|
impl<'a> Debug for FileLockEntry<'a> {
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FMTError> {
|
|
|
|
write!(fmt, "FileLockEntry(Store = {})", self.store.location.to_str()
|
|
|
|
.unwrap_or("Unknown Path"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 11:13:58 +00:00
|
|
|
impl<'a> Deref for FileLockEntry<'a> {
|
2016-01-16 17:25:48 +00:00
|
|
|
type Target = Entry;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 11:13:58 +00:00
|
|
|
impl<'a> DerefMut for FileLockEntry<'a> {
|
2016-01-16 17:25:48 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:32:12 +00:00
|
|
|
impl<'a> Drop for FileLockEntry<'a> {
|
2016-02-06 23:58:53 +00:00
|
|
|
/// This will silently ignore errors, use `Store::update` if you want to catch the errors
|
2016-01-16 18:32:12 +00:00
|
|
|
fn drop(&mut self) {
|
2016-02-06 23:58:53 +00:00
|
|
|
let _ = self.store._update(self);
|
2016-01-16 18:32:12 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 15:26:02 +00:00
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
/// `EntryContent` type
|
2016-01-23 15:26:02 +00:00
|
|
|
pub type EntryContent = String;
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
/// `EntryHeader`
|
|
|
|
///
|
|
|
|
/// This is basically a wrapper around `toml::Table` which provides convenience to the user of the
|
|
|
|
/// library.
|
2016-01-23 15:26:02 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct EntryHeader {
|
2016-02-11 14:21:26 +00:00
|
|
|
header: Value,
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type EntryResult<V> = RResult<V, ParserError>;
|
|
|
|
|
2016-02-04 10:44:22 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
enum Token {
|
|
|
|
Key(String),
|
|
|
|
Index(usize),
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
/**
|
|
|
|
* Wrapper type around file header (TOML) object
|
|
|
|
*/
|
|
|
|
impl EntryHeader {
|
|
|
|
|
2016-01-23 16:40:39 +00:00
|
|
|
pub fn new() -> EntryHeader {
|
2016-01-23 15:26:02 +00:00
|
|
|
EntryHeader {
|
2016-02-11 14:21:26 +00:00
|
|
|
header: build_default_header()
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 10:50:47 +00:00
|
|
|
pub fn header(&self) -> &Value {
|
|
|
|
&self.header
|
|
|
|
}
|
|
|
|
|
2016-01-23 16:48:45 +00:00
|
|
|
fn from_table(t: Table) -> EntryHeader {
|
|
|
|
EntryHeader {
|
2016-02-11 14:21:26 +00:00
|
|
|
header: Value::Table(t)
|
2016-01-23 16:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
pub fn parse(s: &str) -> EntryResult<EntryHeader> {
|
|
|
|
use toml::Parser;
|
|
|
|
|
|
|
|
let mut parser = Parser::new(s);
|
|
|
|
parser.parse()
|
2016-05-27 08:11:51 +00:00
|
|
|
.ok_or(ParserErrorKind::TOMLParserErrors.into())
|
2016-02-06 17:32:29 +00:00
|
|
|
.and_then(verify_header_consistency)
|
|
|
|
.map(EntryHeader::from_table)
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 17:50:39 +00:00
|
|
|
pub fn verify(&self) -> Result<()> {
|
2016-05-03 21:10:32 +00:00
|
|
|
match self.header {
|
|
|
|
Value::Table(ref t) => verify_header(&t),
|
2016-05-14 17:05:54 +00:00
|
|
|
_ => Err(SE::new(SEK::HeaderTypeFailure, None)),
|
2016-02-11 14:21:26 +00:00
|
|
|
}
|
2016-02-06 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 09:37:43 +00:00
|
|
|
/**
|
|
|
|
* Insert a header field by a string-spec
|
|
|
|
*
|
|
|
|
* ```ignore
|
|
|
|
* insert("something.in.a.field", Boolean(true));
|
|
|
|
* ```
|
|
|
|
*
|
2016-02-05 14:19:12 +00:00
|
|
|
* If an array field was accessed which is _out of bounds_ of the array available, the element
|
|
|
|
* is appended to the array.
|
|
|
|
*
|
2016-01-27 09:37:43 +00:00
|
|
|
* Inserts a Boolean in the section "something" -> "in" -> "a" -> "field"
|
|
|
|
* A JSON equivalent would be
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* something: {
|
|
|
|
* in: {
|
|
|
|
* a: {
|
|
|
|
* field: true
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Returns true if header field was set, false if there is already a value
|
|
|
|
*/
|
|
|
|
pub fn insert(&mut self, spec: &str, v: Value) -> Result<bool> {
|
2016-02-12 20:57:53 +00:00
|
|
|
self.insert_with_sep(spec, '.', v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<bool> {
|
2016-05-14 17:24:01 +00:00
|
|
|
let tokens = match EntryHeader::tokenize(spec, sep) {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(t) => t
|
|
|
|
};
|
2016-02-05 14:19:12 +00:00
|
|
|
|
2016-05-14 17:24:01 +00:00
|
|
|
let destination = match tokens.iter().last() {
|
|
|
|
None => return Err(SE::new(SEK::HeaderPathSyntaxError, None)),
|
|
|
|
Some(d) => d,
|
|
|
|
};
|
2016-02-05 14:19:12 +00:00
|
|
|
|
|
|
|
let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens
|
2016-05-14 17:24:01 +00:00
|
|
|
|
|
|
|
// walk N-1 tokens
|
|
|
|
let value = match EntryHeader::walk_header(&mut self.header, path_to_dest) {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(v) => v
|
|
|
|
};
|
2016-02-05 14:19:12 +00:00
|
|
|
|
|
|
|
// There is already an value at this place
|
|
|
|
if EntryHeader::extract(value, destination).is_ok() {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
match *destination {
|
|
|
|
Token::Key(ref s) => { // if the destination shall be an map key
|
|
|
|
match *value {
|
2016-02-05 14:19:12 +00:00
|
|
|
/*
|
|
|
|
* Put it in there if we have a map
|
|
|
|
*/
|
2016-05-03 21:10:32 +00:00
|
|
|
Value::Table(ref mut t) => {
|
2016-02-05 14:19:12 +00:00
|
|
|
t.insert(s.clone(), v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fail if there is no map here
|
|
|
|
*/
|
2016-05-14 17:05:54 +00:00
|
|
|
_ => return Err(SE::new(SEK::HeaderPathTypeFailure, None)),
|
2016-02-05 14:19:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
Token::Index(i) => { // if the destination shall be an array
|
|
|
|
match *value {
|
2016-02-05 14:19:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Put it in there if we have an array
|
|
|
|
*/
|
2016-05-03 21:10:32 +00:00
|
|
|
Value::Array(ref mut a) => {
|
2016-02-05 14:19:12 +00:00
|
|
|
a.push(v); // push to the end of the array
|
|
|
|
|
|
|
|
// if the index is inside the array, we swap-remove the element at this
|
|
|
|
// index
|
|
|
|
if a.len() < i {
|
|
|
|
a.swap_remove(i);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fail if there is no array here
|
|
|
|
*/
|
2016-05-14 17:05:54 +00:00
|
|
|
_ => return Err(SE::new(SEK::HeaderPathTypeFailure, None)),
|
2016-02-05 14:19:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(true)
|
2016-01-27 09:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a header field by a string-spec
|
|
|
|
*
|
|
|
|
* ```ignore
|
|
|
|
* set("something.in.a.field", Boolean(true));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Sets a Boolean in the section "something" -> "in" -> "a" -> "field"
|
|
|
|
* A JSON equivalent would be
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* something: {
|
|
|
|
* in: {
|
|
|
|
* a: {
|
|
|
|
* field: true
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* If there is already a value at this place, this value will be overridden and the old value
|
|
|
|
* will be returned
|
|
|
|
*/
|
|
|
|
pub fn set(&mut self, spec: &str, v: Value) -> Result<Option<Value>> {
|
2016-02-12 20:56:48 +00:00
|
|
|
self.set_with_sep(spec, '.', v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_with_sep(&mut self, spec: &str, sep: char, v: Value) -> Result<Option<Value>> {
|
2016-05-14 17:26:54 +00:00
|
|
|
let tokens = match EntryHeader::tokenize(spec, sep) {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(t) => t,
|
|
|
|
};
|
2016-02-11 14:15:31 +00:00
|
|
|
debug!("tokens = {:?}", tokens);
|
2016-02-10 14:46:00 +00:00
|
|
|
|
2016-05-14 17:26:54 +00:00
|
|
|
let destination = match tokens.iter().last() {
|
|
|
|
None => return Err(SE::new(SEK::HeaderPathSyntaxError, None)),
|
|
|
|
Some(d) => d
|
|
|
|
};
|
2016-02-11 14:15:31 +00:00
|
|
|
debug!("destination = {:?}", destination);
|
2016-02-10 14:46:00 +00:00
|
|
|
|
|
|
|
let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens
|
2016-05-14 17:26:54 +00:00
|
|
|
// walk N-1 tokens
|
|
|
|
let value = match EntryHeader::walk_header(&mut self.header, path_to_dest) {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(v) => v
|
|
|
|
};
|
2016-02-11 14:15:31 +00:00
|
|
|
debug!("walked value = {:?}", value);
|
2016-02-10 14:46:00 +00:00
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
match *destination {
|
|
|
|
Token::Key(ref s) => { // if the destination shall be an map key->value
|
|
|
|
match *value {
|
2016-02-10 14:46:00 +00:00
|
|
|
/*
|
|
|
|
* Put it in there if we have a map
|
|
|
|
*/
|
2016-05-03 21:10:32 +00:00
|
|
|
Value::Table(ref mut t) => {
|
2016-02-11 14:15:31 +00:00
|
|
|
debug!("Matched Key->Table");
|
2016-02-10 14:46:00 +00:00
|
|
|
return Ok(t.insert(s.clone(), v));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fail if there is no map here
|
|
|
|
*/
|
2016-02-11 14:15:31 +00:00
|
|
|
_ => {
|
|
|
|
debug!("Matched Key->NON-Table");
|
2016-05-14 17:05:54 +00:00
|
|
|
return Err(SE::new(SEK::HeaderPathTypeFailure, None));
|
2016-02-11 14:15:31 +00:00
|
|
|
}
|
2016-02-10 14:46:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
Token::Index(i) => { // if the destination shall be an array
|
|
|
|
match *value {
|
2016-02-10 14:46:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Put it in there if we have an array
|
|
|
|
*/
|
2016-05-03 21:10:32 +00:00
|
|
|
Value::Array(ref mut a) => {
|
2016-02-11 14:15:31 +00:00
|
|
|
debug!("Matched Index->Array");
|
2016-02-10 14:46:00 +00:00
|
|
|
a.push(v); // push to the end of the array
|
|
|
|
|
|
|
|
// if the index is inside the array, we swap-remove the element at this
|
|
|
|
// index
|
2016-02-11 14:15:31 +00:00
|
|
|
if a.len() > i {
|
|
|
|
debug!("Swap-Removing in Array {:?}[{:?}] <- {:?}", a, i, a[a.len()-1]);
|
2016-02-10 14:46:00 +00:00
|
|
|
return Ok(Some(a.swap_remove(i)));
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:15:31 +00:00
|
|
|
debug!("Appended");
|
2016-02-10 14:46:00 +00:00
|
|
|
return Ok(None);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fail if there is no array here
|
|
|
|
*/
|
2016-02-11 14:15:31 +00:00
|
|
|
_ => {
|
|
|
|
debug!("Matched Index->NON-Array");
|
2016-05-14 17:05:54 +00:00
|
|
|
return Err(SE::new(SEK::HeaderPathTypeFailure, None));
|
2016-02-11 14:15:31 +00:00
|
|
|
},
|
2016-02-10 14:46:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
2016-01-27 09:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a header field by a string-spec
|
|
|
|
*
|
|
|
|
* ```ignore
|
|
|
|
* let value = read("something.in.a.field");
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Reads a Value in the section "something" -> "in" -> "a" -> "field"
|
|
|
|
* A JSON equivalent would be
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* something: {
|
|
|
|
* in: {
|
|
|
|
* a: {
|
|
|
|
* field: true
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2016-02-10 15:00:51 +00:00
|
|
|
* If there is no a value at this place, None will be returned. This also holds true for Arrays
|
|
|
|
* which are accessed at an index which is not yet there, even if the accessed index is much
|
|
|
|
* larger than the array length.
|
2016-01-27 09:37:43 +00:00
|
|
|
*/
|
|
|
|
pub fn read(&self, spec: &str) -> Result<Option<Value>> {
|
2016-02-12 20:55:06 +00:00
|
|
|
self.read_with_sep(spec, '.')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_with_sep(&self, spec: &str, splitchr: char) -> Result<Option<Value>> {
|
2016-05-14 17:29:14 +00:00
|
|
|
let tokens = match EntryHeader::tokenize(spec, splitchr) {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(t) => t,
|
|
|
|
};
|
2016-02-10 15:00:51 +00:00
|
|
|
|
2016-02-11 14:21:26 +00:00
|
|
|
let mut header_clone = self.header.clone(); // we clone as READing is simpler this way
|
2016-05-14 17:29:14 +00:00
|
|
|
// walk N-1 tokens
|
|
|
|
match EntryHeader::walk_header(&mut header_clone, tokens) {
|
|
|
|
Err(e) => match e.err_type() {
|
2016-02-10 15:00:51 +00:00
|
|
|
// We cannot find the header key, as there is no path to it
|
2016-05-14 17:05:54 +00:00
|
|
|
SEK::HeaderKeyNotFound => Ok(None),
|
2016-03-01 20:19:15 +00:00
|
|
|
_ => Err(e),
|
2016-05-14 17:29:14 +00:00
|
|
|
},
|
|
|
|
Ok(v) => Ok(Some(v.clone())),
|
2016-02-10 15:00:51 +00:00
|
|
|
}
|
2016-01-27 09:37:43 +00:00
|
|
|
}
|
2016-02-04 10:44:22 +00:00
|
|
|
|
2016-02-12 18:51:05 +00:00
|
|
|
pub fn delete(&mut self, spec: &str) -> Result<Option<Value>> {
|
2016-05-14 17:33:41 +00:00
|
|
|
let tokens = match EntryHeader::tokenize(spec, '.') {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(t) => t
|
|
|
|
};
|
2016-02-12 18:51:05 +00:00
|
|
|
|
2016-05-14 17:33:41 +00:00
|
|
|
let destination = match tokens.iter().last() {
|
|
|
|
None => return Err(SE::new(SEK::HeaderPathSyntaxError, None)),
|
|
|
|
Some(d) => d
|
|
|
|
};
|
2016-02-12 18:51:05 +00:00
|
|
|
debug!("destination = {:?}", destination);
|
|
|
|
|
|
|
|
let path_to_dest = tokens[..(tokens.len() - 1)].into(); // N - 1 tokens
|
2016-05-14 17:33:41 +00:00
|
|
|
// walk N-1 tokens
|
|
|
|
let mut value = match EntryHeader::walk_header(&mut self.header, path_to_dest) {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(v) => v
|
|
|
|
};
|
2016-02-12 18:51:05 +00:00
|
|
|
debug!("walked value = {:?}", value);
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
match *destination {
|
|
|
|
Token::Key(ref s) => { // if the destination shall be an map key->value
|
|
|
|
match *value {
|
|
|
|
Value::Table(ref mut t) => {
|
2016-02-12 18:51:05 +00:00
|
|
|
debug!("Matched Key->Table, removing {:?}", s);
|
|
|
|
return Ok(t.remove(s));
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
debug!("Matched Key->NON-Table");
|
2016-05-14 17:05:54 +00:00
|
|
|
return Err(SE::new(SEK::HeaderPathTypeFailure, None));
|
2016-02-12 18:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
Token::Index(i) => { // if the destination shall be an array
|
|
|
|
match *value {
|
|
|
|
Value::Array(ref mut a) => {
|
2016-02-12 18:51:05 +00:00
|
|
|
// if the index is inside the array, we swap-remove the element at this
|
|
|
|
// index
|
|
|
|
if a.len() > i {
|
|
|
|
debug!("Removing in Array {:?}[{:?}]", a, i);
|
|
|
|
return Ok(Some(a.remove(i)));
|
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
debug!("Matched Index->NON-Array");
|
2016-05-14 17:05:54 +00:00
|
|
|
return Err(SE::new(SEK::HeaderPathTypeFailure, None));
|
2016-02-12 18:51:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2016-02-12 20:50:44 +00:00
|
|
|
fn tokenize(spec: &str, splitchr: char) -> Result<Vec<Token>> {
|
2016-02-04 10:44:22 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2016-02-12 20:50:44 +00:00
|
|
|
spec.split(splitchr)
|
2016-02-04 10:44:22 +00:00
|
|
|
.map(|s| {
|
|
|
|
usize::from_str(s)
|
|
|
|
.map(Token::Index)
|
|
|
|
.or_else(|_| Ok(Token::Key(String::from(s))))
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2016-02-04 12:26:01 +00:00
|
|
|
fn walk_header(v: &mut Value, tokens: Vec<Token>) -> Result<&mut Value> {
|
|
|
|
use std::vec::IntoIter;
|
|
|
|
|
|
|
|
fn walk_iter<'a>(v: Result<&'a mut Value>, i: &mut IntoIter<Token>) -> Result<&'a mut Value> {
|
|
|
|
let next = i.next();
|
|
|
|
v.and_then(move |value| {
|
|
|
|
if let Some(token) = next {
|
2016-02-05 14:12:34 +00:00
|
|
|
walk_iter(EntryHeader::extract(value, &token), i)
|
2016-02-04 12:26:01 +00:00
|
|
|
} else {
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
walk_iter(Ok(v), &mut tokens.into_iter())
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
fn extract_from_table<'a>(v: &'a mut Value, s: &str) -> Result<&'a mut Value> {
|
|
|
|
match *v {
|
|
|
|
Value::Table(ref mut t) => {
|
2016-02-05 14:12:34 +00:00
|
|
|
t.get_mut(&s[..])
|
2016-05-14 17:05:54 +00:00
|
|
|
.ok_or(SE::new(SEK::HeaderKeyNotFound, None))
|
2016-02-05 14:12:34 +00:00
|
|
|
},
|
2016-05-14 17:05:54 +00:00
|
|
|
_ => Err(SE::new(SEK::HeaderPathTypeFailure, None)),
|
2016-02-05 14:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_from_array(v: &mut Value, i: usize) -> Result<&mut Value> {
|
2016-05-03 21:10:32 +00:00
|
|
|
match *v {
|
|
|
|
Value::Array(ref mut a) => {
|
2016-02-05 14:12:34 +00:00
|
|
|
if a.len() < i {
|
2016-05-14 17:05:54 +00:00
|
|
|
Err(SE::new(SEK::HeaderKeyNotFound, None))
|
2016-02-05 14:12:34 +00:00
|
|
|
} else {
|
|
|
|
Ok(&mut a[i])
|
|
|
|
}
|
|
|
|
},
|
2016-05-14 17:05:54 +00:00
|
|
|
_ => Err(SE::new(SEK::HeaderPathTypeFailure, None)),
|
2016-02-05 14:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract<'a>(v: &'a mut Value, token: &Token) -> Result<&'a mut Value> {
|
2016-05-03 21:10:32 +00:00
|
|
|
match *token {
|
|
|
|
Token::Key(ref s) => EntryHeader::extract_from_table(v, s),
|
|
|
|
Token::Index(i) => EntryHeader::extract_from_array(v, i),
|
2016-02-05 14:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 21:02:33 +00:00
|
|
|
impl Into<Table> for EntryHeader {
|
|
|
|
|
|
|
|
fn into(self) -> Table {
|
|
|
|
match self.header {
|
|
|
|
Value::Table(t) => t,
|
|
|
|
_ => panic!("EntryHeader is not a table!"),
|
|
|
|
}
|
2016-02-06 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 21:07:15 +00:00
|
|
|
impl From<Table> for EntryHeader {
|
|
|
|
|
|
|
|
fn from(t: Table) -> EntryHeader {
|
|
|
|
EntryHeader { header: Value::Table(t) }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:21:26 +00:00
|
|
|
fn build_default_header() -> Value { // BTreeMap<String, Value>
|
2016-01-23 15:26:02 +00:00
|
|
|
let mut m = BTreeMap::new();
|
|
|
|
|
|
|
|
m.insert(String::from("imag"), {
|
|
|
|
let mut imag_map = BTreeMap::<String, Value>::new();
|
|
|
|
|
2016-02-04 16:38:20 +00:00
|
|
|
imag_map.insert(String::from("version"), Value::String(String::from(version!())));
|
2016-01-23 15:26:02 +00:00
|
|
|
imag_map.insert(String::from("links"), Value::Array(vec![]));
|
|
|
|
|
|
|
|
Value::Table(imag_map)
|
|
|
|
});
|
|
|
|
|
2016-02-11 14:21:26 +00:00
|
|
|
Value::Table(m)
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
2016-02-06 18:26:00 +00:00
|
|
|
fn verify_header(t: &Table) -> Result<()> {
|
|
|
|
if !has_main_section(t) {
|
2016-05-27 08:11:51 +00:00
|
|
|
Err(SE::from(ParserErrorKind::MissingMainSection.into_error()))
|
2016-02-06 18:26:00 +00:00
|
|
|
} else if !has_imag_version_in_main_section(t) {
|
2016-05-27 08:11:51 +00:00
|
|
|
Err(SE::from(ParserErrorKind::MissingVersionInfo.into_error()))
|
2016-02-06 18:26:00 +00:00
|
|
|
} else if !has_only_tables(t) {
|
|
|
|
debug!("Could not verify that it only has tables in its base table");
|
2016-05-27 08:11:51 +00:00
|
|
|
Err(SE::from(ParserErrorKind::NonTableInBaseTable.into_error()))
|
2016-02-06 18:26:00 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2016-01-23 15:26:02 +00:00
|
|
|
|
|
|
|
fn verify_header_consistency(t: Table) -> EntryResult<Table> {
|
2016-05-14 17:37:46 +00:00
|
|
|
verify_header(&t)
|
2016-05-27 08:11:51 +00:00
|
|
|
.map_err(Box::new)
|
|
|
|
.map_err(|e| ParserErrorKind::HeaderInconsistency.into_error_with_cause(e))
|
2016-05-14 17:37:46 +00:00
|
|
|
.map(|_| t)
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 18:26:00 +00:00
|
|
|
fn has_only_tables(t: &Table) -> bool {
|
2016-02-06 18:39:20 +00:00
|
|
|
debug!("Verifying that table has only tables");
|
2016-05-03 21:10:32 +00:00
|
|
|
t.iter().all(|(_, x)| if let Value::Table(_) = *x { true } else { false })
|
2016-02-06 18:26:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
fn has_main_section(t: &Table) -> bool {
|
|
|
|
t.contains_key("imag") &&
|
|
|
|
match t.get("imag") {
|
|
|
|
Some(&Value::Table(_)) => true,
|
|
|
|
Some(_) => false,
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_imag_version_in_main_section(t: &Table) -> bool {
|
|
|
|
use semver::Version;
|
|
|
|
|
2016-05-03 21:10:32 +00:00
|
|
|
match *t.get("imag").unwrap() {
|
|
|
|
Value::Table(ref sec) => {
|
2016-01-23 15:26:02 +00:00
|
|
|
sec.get("version")
|
|
|
|
.and_then(|v| {
|
2016-05-03 21:10:32 +00:00
|
|
|
match *v {
|
2016-05-14 17:38:22 +00:00
|
|
|
Value::String(ref s) => Some(Version::parse(&s[..]).is_ok()),
|
|
|
|
_ => Some(false),
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
})
|
2016-01-23 16:30:01 +00:00
|
|
|
.unwrap_or(false)
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
2016-05-03 21:10:32 +00:00
|
|
|
_ => false,
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An Entry of the store
|
|
|
|
*
|
|
|
|
* Contains location, header and content part.
|
|
|
|
*/
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Entry {
|
|
|
|
location: StoreId,
|
|
|
|
header: EntryHeader,
|
|
|
|
content: EntryContent,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entry {
|
|
|
|
|
2016-01-24 19:19:09 +00:00
|
|
|
pub fn new(loc: StoreId) -> Entry {
|
2016-01-23 15:26:02 +00:00
|
|
|
Entry {
|
|
|
|
location: loc,
|
2016-01-23 16:48:45 +00:00
|
|
|
header: EntryHeader::new(),
|
2016-01-23 15:26:02 +00:00
|
|
|
content: EntryContent::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 13:58:34 +00:00
|
|
|
pub fn from_reader<S: IntoStoreId>(loc: S, file: &mut Read) -> Result<Entry> {
|
2016-01-24 15:01:37 +00:00
|
|
|
let text = {
|
|
|
|
let mut s = String::new();
|
|
|
|
try!(file.read_to_string(&mut s));
|
|
|
|
s
|
2016-01-23 16:30:01 +00:00
|
|
|
};
|
2016-01-24 15:01:37 +00:00
|
|
|
Self::from_str(loc, &text[..])
|
|
|
|
}
|
2016-01-23 16:30:01 +00:00
|
|
|
|
2016-05-04 10:00:31 +00:00
|
|
|
pub fn from_str<S: IntoStoreId>(loc: S, s: &str) -> Result<Entry> {
|
2016-01-28 20:06:49 +00:00
|
|
|
debug!("Building entry from string");
|
2016-02-20 20:06:47 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref RE: Regex = Regex::new(r"(?smx)
|
|
|
|
^---$
|
|
|
|
(?P<header>.*) # Header
|
|
|
|
^---$\n
|
|
|
|
(?P<content>.*) # Content
|
|
|
|
").unwrap();
|
|
|
|
}
|
|
|
|
|
2016-05-14 16:59:15 +00:00
|
|
|
let matches = match RE.captures(s) {
|
2016-05-14 17:05:54 +00:00
|
|
|
None => return Err(SE::new(SEK::MalformedEntry, None)),
|
2016-05-14 16:59:15 +00:00
|
|
|
Some(s) => s,
|
|
|
|
};
|
2016-01-23 16:30:01 +00:00
|
|
|
|
2016-05-14 16:59:15 +00:00
|
|
|
let header = match matches.name("header") {
|
2016-05-14 17:05:54 +00:00
|
|
|
None => return Err(SE::new(SEK::MalformedEntry, None)),
|
2016-05-14 16:59:15 +00:00
|
|
|
Some(s) => s
|
|
|
|
};
|
2016-01-23 16:30:01 +00:00
|
|
|
|
|
|
|
let content = matches.name("content").unwrap_or("");
|
|
|
|
|
2016-01-28 20:06:49 +00:00
|
|
|
debug!("Header and content found. Yay! Building Entry object now");
|
2016-01-23 16:30:01 +00:00
|
|
|
Ok(Entry {
|
2016-08-25 15:56:55 +00:00
|
|
|
location: try!(loc.into_storeid()),
|
2016-05-14 16:59:15 +00:00
|
|
|
header: try!(EntryHeader::parse(header)),
|
2016-01-23 16:30:01 +00:00
|
|
|
content: content.into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-24 18:55:47 +00:00
|
|
|
pub fn to_str(&self) -> String {
|
2016-09-09 09:48:10 +00:00
|
|
|
format!("---\n{header}---\n{content}",
|
2016-02-11 14:21:26 +00:00
|
|
|
header = ::toml::encode_str(&self.header.header),
|
2016-01-24 18:55:47 +00:00
|
|
|
content = self.content)
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
pub fn get_location(&self) -> &StoreId {
|
|
|
|
&self.location
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_header(&self) -> &EntryHeader {
|
|
|
|
&self.header
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_header_mut(&mut self) -> &mut EntryHeader {
|
|
|
|
&mut self.header
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_content(&self) -> &EntryContent {
|
|
|
|
&self.content
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_content_mut(&mut self) -> &mut EntryContent {
|
|
|
|
&mut self.content
|
|
|
|
}
|
|
|
|
|
2016-02-06 17:50:39 +00:00
|
|
|
pub fn verify(&self) -> Result<()> {
|
|
|
|
self.header.verify()
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 15:27:41 +00:00
|
|
|
mod glob_store_iter {
|
|
|
|
use std::fmt::{Debug, Formatter};
|
|
|
|
use std::fmt::Error as FmtError;
|
2016-08-22 10:22:39 +00:00
|
|
|
use std::path::PathBuf;
|
2016-05-12 15:27:41 +00:00
|
|
|
use glob::Paths;
|
|
|
|
use storeid::StoreId;
|
2016-05-13 12:48:20 +00:00
|
|
|
use storeid::StoreIdIterator;
|
2016-05-12 15:27:41 +00:00
|
|
|
|
2016-08-25 15:10:16 +00:00
|
|
|
use error::StoreErrorKind as SEK;
|
|
|
|
use error::MapErrInto;
|
|
|
|
|
|
|
|
use libimagerror::trace::trace_error;
|
|
|
|
|
2016-05-12 15:27:41 +00:00
|
|
|
pub struct GlobStoreIdIterator {
|
2016-08-22 10:22:39 +00:00
|
|
|
store_path: PathBuf,
|
2016-05-12 15:27:41 +00:00
|
|
|
paths: Paths,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for GlobStoreIdIterator {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
write!(fmt, "GlobStoreIdIterator")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-05-13 12:48:20 +00:00
|
|
|
|
|
|
|
impl Into<StoreIdIterator> for GlobStoreIdIterator {
|
|
|
|
|
|
|
|
fn into(self) -> StoreIdIterator {
|
|
|
|
StoreIdIterator::new(Box::new(self))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-05-12 15:27:41 +00:00
|
|
|
|
|
|
|
impl GlobStoreIdIterator {
|
|
|
|
|
2016-08-22 10:22:39 +00:00
|
|
|
pub fn new(paths: Paths, store_path: PathBuf) -> GlobStoreIdIterator {
|
2016-09-05 16:12:29 +00:00
|
|
|
debug!("Create a GlobStoreIdIterator(store_path = {:?}, /* ... */)", store_path);
|
|
|
|
|
2016-05-12 15:27:41 +00:00
|
|
|
GlobStoreIdIterator {
|
2016-08-22 10:22:39 +00:00
|
|
|
store_path: store_path,
|
2016-05-12 15:27:41 +00:00
|
|
|
paths: paths,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for GlobStoreIdIterator {
|
|
|
|
type Item = StoreId;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<StoreId> {
|
2016-08-22 10:22:39 +00:00
|
|
|
self.paths
|
|
|
|
.next()
|
|
|
|
.and_then(|o| {
|
2016-09-05 16:12:29 +00:00
|
|
|
debug!("GlobStoreIdIterator::next() => {:?}", o);
|
2016-08-25 15:10:16 +00:00
|
|
|
o.map_err_into(SEK::StoreIdHandlingError)
|
2016-09-05 16:22:55 +00:00
|
|
|
.and_then(|p| StoreId::from_full_path(&self.store_path, p))
|
2016-08-25 15:10:16 +00:00
|
|
|
.map_err(|e| {
|
2016-08-22 10:22:39 +00:00
|
|
|
debug!("GlobStoreIdIterator error: {:?}", e);
|
2016-08-25 15:10:16 +00:00
|
|
|
trace_error(&e);
|
|
|
|
}).ok()
|
2016-08-22 10:22:39 +00:00
|
|
|
})
|
2016-05-12 15:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2016-02-11 14:15:31 +00:00
|
|
|
extern crate env_logger;
|
|
|
|
|
2016-01-23 15:26:02 +00:00
|
|
|
use std::collections::BTreeMap;
|
2016-02-05 13:47:06 +00:00
|
|
|
use super::EntryHeader;
|
|
|
|
use super::Token;
|
2016-08-25 16:03:25 +00:00
|
|
|
use storeid::StoreId;
|
2016-01-23 15:26:02 +00:00
|
|
|
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_imag_section() {
|
|
|
|
use super::has_main_section;
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert("imag".into(), Value::Table(BTreeMap::new()));
|
|
|
|
|
|
|
|
assert!(has_main_section(&map));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_imag_invalid_section_type() {
|
|
|
|
use super::has_main_section;
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert("imag".into(), Value::Boolean(false));
|
|
|
|
|
|
|
|
assert!(!has_main_section(&map));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_imag_abscent_main_section() {
|
|
|
|
use super::has_main_section;
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert("not_imag".into(), Value::Boolean(false));
|
|
|
|
|
|
|
|
assert!(!has_main_section(&map));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_main_section_without_version() {
|
|
|
|
use super::has_imag_version_in_main_section;
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert("imag".into(), Value::Table(BTreeMap::new()));
|
|
|
|
|
|
|
|
assert!(!has_imag_version_in_main_section(&map));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_main_section_with_version() {
|
|
|
|
use super::has_imag_version_in_main_section;
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
let mut sub = BTreeMap::new();
|
|
|
|
sub.insert("version".into(), Value::String("0.0.0".into()));
|
|
|
|
map.insert("imag".into(), Value::Table(sub));
|
|
|
|
|
|
|
|
assert!(has_imag_version_in_main_section(&map));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_main_section_with_version_in_wrong_type() {
|
|
|
|
use super::has_imag_version_in_main_section;
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
let mut sub = BTreeMap::new();
|
|
|
|
sub.insert("version".into(), Value::Boolean(false));
|
|
|
|
map.insert("imag".into(), Value::Table(sub));
|
|
|
|
|
|
|
|
assert!(!has_imag_version_in_main_section(&map));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verification_good() {
|
|
|
|
use super::verify_header_consistency;
|
|
|
|
|
|
|
|
let mut header = BTreeMap::new();
|
|
|
|
let sub = {
|
|
|
|
let mut sub = BTreeMap::new();
|
|
|
|
sub.insert("version".into(), Value::String(String::from("0.0.0")));
|
|
|
|
|
|
|
|
Value::Table(sub)
|
|
|
|
};
|
|
|
|
|
|
|
|
header.insert("imag".into(), sub);
|
|
|
|
|
|
|
|
assert!(verify_header_consistency(header).is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verification_invalid_versionstring() {
|
|
|
|
use super::verify_header_consistency;
|
|
|
|
|
|
|
|
let mut header = BTreeMap::new();
|
|
|
|
let sub = {
|
|
|
|
let mut sub = BTreeMap::new();
|
|
|
|
sub.insert("version".into(), Value::String(String::from("000")));
|
|
|
|
|
|
|
|
Value::Table(sub)
|
|
|
|
};
|
|
|
|
|
|
|
|
header.insert("imag".into(), sub);
|
|
|
|
|
|
|
|
assert!(!verify_header_consistency(header).is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verification_current_version() {
|
|
|
|
use super::verify_header_consistency;
|
|
|
|
|
|
|
|
let mut header = BTreeMap::new();
|
|
|
|
let sub = {
|
|
|
|
let mut sub = BTreeMap::new();
|
2016-02-04 16:38:20 +00:00
|
|
|
sub.insert("version".into(), Value::String(String::from(version!())));
|
2016-01-23 15:26:02 +00:00
|
|
|
|
|
|
|
Value::Table(sub)
|
|
|
|
};
|
|
|
|
|
|
|
|
header.insert("imag".into(), sub);
|
|
|
|
|
|
|
|
assert!(verify_header_consistency(header).is_ok());
|
|
|
|
}
|
2016-01-24 15:01:37 +00:00
|
|
|
|
|
|
|
static TEST_ENTRY : &'static str = "---
|
|
|
|
[imag]
|
2016-01-24 18:55:47 +00:00
|
|
|
version = \"0.0.3\"
|
2016-01-24 15:01:37 +00:00
|
|
|
---
|
|
|
|
Hai";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_entry_from_str() {
|
|
|
|
use super::Entry;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
println!("{}", TEST_ENTRY);
|
2016-08-25 16:03:25 +00:00
|
|
|
let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(),
|
2016-01-24 15:01:37 +00:00
|
|
|
TEST_ENTRY).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(entry.content, "Hai");
|
|
|
|
}
|
|
|
|
|
2016-01-24 18:55:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_entry_to_str() {
|
|
|
|
use super::Entry;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
println!("{}", TEST_ENTRY);
|
2016-08-25 16:03:25 +00:00
|
|
|
let entry = Entry::from_str(StoreId::new_baseless(PathBuf::from("test/foo~1.3")).unwrap(),
|
2016-01-24 18:55:47 +00:00
|
|
|
TEST_ENTRY).unwrap();
|
|
|
|
let string = entry.to_str();
|
|
|
|
|
|
|
|
assert_eq!(TEST_ENTRY, string);
|
|
|
|
}
|
2016-01-24 15:01:37 +00:00
|
|
|
|
2016-02-05 13:47:06 +00:00
|
|
|
#[test]
|
|
|
|
fn test_walk_header_simple() {
|
2016-02-12 20:50:44 +00:00
|
|
|
let tokens = EntryHeader::tokenize("a", '.').unwrap();
|
2016-02-05 13:47:06 +00:00
|
|
|
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
|
|
|
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
|
|
|
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
|
|
|
|
|
|
|
let mut header = BTreeMap::new();
|
|
|
|
header.insert(String::from("a"), Value::Integer(1));
|
2016-01-23 15:26:02 +00:00
|
|
|
|
2016-02-05 13:47:06 +00:00
|
|
|
let mut v_header = Value::Table(header);
|
|
|
|
let res = EntryHeader::walk_header(&mut v_header, tokens);
|
|
|
|
assert_eq!(&mut Value::Integer(1), res.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_walk_header_with_array() {
|
2016-02-12 20:50:44 +00:00
|
|
|
let tokens = EntryHeader::tokenize("a.0", '.').unwrap();
|
2016-02-05 13:47:06 +00:00
|
|
|
assert!(tokens.len() == 2, "2 token was expected, {} were parsed", tokens.len());
|
|
|
|
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
|
|
|
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
|
|
|
|
|
|
|
let mut header = BTreeMap::new();
|
|
|
|
let ary = Value::Array(vec![Value::Integer(1)]);
|
|
|
|
header.insert(String::from("a"), ary);
|
2016-01-23 15:26:02 +00:00
|
|
|
|
|
|
|
|
2016-02-05 13:47:06 +00:00
|
|
|
let mut v_header = Value::Table(header);
|
|
|
|
let res = EntryHeader::walk_header(&mut v_header, tokens);
|
|
|
|
assert_eq!(&mut Value::Integer(1), res.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_walk_header_extract_array() {
|
2016-02-12 20:50:44 +00:00
|
|
|
let tokens = EntryHeader::tokenize("a", '.').unwrap();
|
2016-02-05 13:47:06 +00:00
|
|
|
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
|
|
|
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from("a")),
|
|
|
|
"'a' token was expected, {:?} was parsed", tokens.iter().next());
|
|
|
|
|
|
|
|
let mut header = BTreeMap::new();
|
|
|
|
let ary = Value::Array(vec![Value::Integer(1)]);
|
|
|
|
header.insert(String::from("a"), ary);
|
|
|
|
|
|
|
|
let mut v_header = Value::Table(header);
|
|
|
|
let res = EntryHeader::walk_header(&mut v_header, tokens);
|
|
|
|
assert_eq!(&mut Value::Array(vec![Value::Integer(1)]), res.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a big testing header.
|
|
|
|
*
|
|
|
|
* JSON equivalent:
|
|
|
|
*
|
|
|
|
* ```json
|
|
|
|
* {
|
|
|
|
* "a": {
|
|
|
|
* "array": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
|
|
|
|
* },
|
|
|
|
* "b": {
|
|
|
|
* "array": [ "string1", "string2", "string3", "string4" ]
|
|
|
|
* },
|
|
|
|
* "c": {
|
|
|
|
* "array": [ 1, "string2", 3, "string4" ]
|
|
|
|
* },
|
|
|
|
* "d": {
|
|
|
|
* "array": [
|
|
|
|
* {
|
|
|
|
* "d1": 1
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* "d2": 2
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* "d3": 3
|
|
|
|
* },
|
|
|
|
* ],
|
|
|
|
*
|
|
|
|
* "something": "else",
|
|
|
|
*
|
|
|
|
* "and": {
|
|
|
|
* "something": {
|
|
|
|
* "totally": "different"
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The sections "a", "b", "c", "d" are created in the respective helper functions
|
|
|
|
* create_header_section_a, create_header_section_b, create_header_section_c and
|
|
|
|
* create_header_section_d.
|
|
|
|
*
|
|
|
|
* These functions can also be used for testing.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fn create_header() -> Value {
|
|
|
|
let a = create_header_section_a();
|
|
|
|
let b = create_header_section_b();
|
|
|
|
let c = create_header_section_c();
|
|
|
|
let d = create_header_section_d();
|
|
|
|
|
|
|
|
let mut header = BTreeMap::new();
|
|
|
|
header.insert(String::from("a"), a);
|
|
|
|
header.insert(String::from("b"), b);
|
|
|
|
header.insert(String::from("c"), c);
|
|
|
|
header.insert(String::from("d"), d);
|
|
|
|
|
|
|
|
Value::Table(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_header_section_a() -> Value {
|
|
|
|
// 0..10 is exclusive 10
|
|
|
|
let a_ary = Value::Array((0..10).map(|x| Value::Integer(x)).collect());
|
|
|
|
|
|
|
|
let mut a_obj = BTreeMap::new();
|
|
|
|
a_obj.insert(String::from("array"), a_ary);
|
|
|
|
Value::Table(a_obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_header_section_b() -> Value {
|
|
|
|
let b_ary = Value::Array((0..9)
|
|
|
|
.map(|x| Value::String(format!("string{}", x)))
|
|
|
|
.collect());
|
|
|
|
|
|
|
|
let mut b_obj = BTreeMap::new();
|
|
|
|
b_obj.insert(String::from("array"), b_ary);
|
|
|
|
Value::Table(b_obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_header_section_c() -> Value {
|
|
|
|
let c_ary = Value::Array(
|
|
|
|
vec![
|
|
|
|
Value::Integer(1),
|
|
|
|
Value::String(String::from("string2")),
|
|
|
|
Value::Integer(3),
|
|
|
|
Value::String(String::from("string4"))
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mut c_obj = BTreeMap::new();
|
|
|
|
c_obj.insert(String::from("array"), c_ary);
|
|
|
|
Value::Table(c_obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_header_section_d() -> Value {
|
|
|
|
let d_ary = Value::Array(
|
|
|
|
vec![
|
|
|
|
{
|
|
|
|
let mut tab = BTreeMap::new();
|
|
|
|
tab.insert(String::from("d1"), Value::Integer(1));
|
|
|
|
tab
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let mut tab = BTreeMap::new();
|
|
|
|
tab.insert(String::from("d2"), Value::Integer(2));
|
|
|
|
tab
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let mut tab = BTreeMap::new();
|
|
|
|
tab.insert(String::from("d3"), Value::Integer(3));
|
|
|
|
tab
|
|
|
|
},
|
|
|
|
].into_iter().map(Value::Table).collect());
|
|
|
|
|
|
|
|
let and_obj = Value::Table({
|
|
|
|
let mut tab = BTreeMap::new();
|
|
|
|
let something_tab = Value::Table({
|
|
|
|
let mut tab = BTreeMap::new();
|
|
|
|
tab.insert(String::from("totally"), Value::String(String::from("different")));
|
|
|
|
tab
|
|
|
|
});
|
|
|
|
tab.insert(String::from("something"), something_tab);
|
|
|
|
tab
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut d_obj = BTreeMap::new();
|
|
|
|
d_obj.insert(String::from("array"), d_ary);
|
|
|
|
d_obj.insert(String::from("something"), Value::String(String::from("else")));
|
|
|
|
d_obj.insert(String::from("and"), and_obj);
|
|
|
|
Value::Table(d_obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_walk_header_big_a() {
|
|
|
|
test_walk_header_extract_section("a", &create_header_section_a());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_walk_header_big_b() {
|
|
|
|
test_walk_header_extract_section("b", &create_header_section_b());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_walk_header_big_c() {
|
|
|
|
test_walk_header_extract_section("c", &create_header_section_c());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_walk_header_big_d() {
|
|
|
|
test_walk_header_extract_section("d", &create_header_section_d());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_walk_header_extract_section(secname: &str, expected: &Value) {
|
2016-02-12 20:50:44 +00:00
|
|
|
let tokens = EntryHeader::tokenize(secname, '.').unwrap();
|
2016-02-05 13:47:06 +00:00
|
|
|
assert!(tokens.len() == 1, "1 token was expected, {} were parsed", tokens.len());
|
|
|
|
assert!(tokens.iter().next().unwrap() == &Token::Key(String::from(secname)),
|
|
|
|
"'{}' token was expected, {:?} was parsed", secname, tokens.iter().next());
|
|
|
|
|
|
|
|
let mut header = create_header();
|
|
|
|
let res = EntryHeader::walk_header(&mut header, tokens);
|
|
|
|
assert_eq!(expected, res.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_walk_header_extract_numbers() {
|
|
|
|
test_extract_number("a", 0, 0);
|
|
|
|
test_extract_number("a", 1, 1);
|
|
|
|
test_extract_number("a", 2, 2);
|
|
|
|
test_extract_number("a", 3, 3);
|
|
|
|
test_extract_number("a", 4, 4);
|
|
|
|
test_extract_number("a", 5, 5);
|
|
|
|
test_extract_number("a", 6, 6);
|
|
|
|
test_extract_number("a", 7, 7);
|
|
|
|
test_extract_number("a", 8, 8);
|
|
|
|
test_extract_number("a", 9, 9);
|
|
|
|
|
|
|
|
test_extract_number("c", 0, 1);
|
|
|
|
test_extract_number("c", 2, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_extract_number(sec: &str, idx: usize, exp: i64) {
|
2016-02-12 20:50:44 +00:00
|
|
|
let tokens = EntryHeader::tokenize(&format!("{}.array.{}", sec, idx)[..], '.').unwrap();
|
2016-02-05 13:47:06 +00:00
|
|
|
assert!(tokens.len() == 3, "3 token was expected, {} were parsed", tokens.len());
|
|
|
|
{
|
|
|
|
let mut iter = tokens.iter();
|
|
|
|
|
|
|
|
let tok = iter.next().unwrap();
|
|
|
|
let exp = Token::Key(String::from(sec));
|
|
|
|
assert!(tok == &exp, "'{}' token was expected, {:?} was parsed", sec, tok);
|
2016-01-23 15:26:02 +00:00
|
|
|
|
2016-02-05 13:47:06 +00:00
|
|
|
let tok = iter.next().unwrap();
|
|
|
|
let exp = Token::Key(String::from("array"));
|
|
|
|
assert!(tok == &exp, "'array' token was expected, {:?} was parsed", tok);
|
|
|
|
|
|
|
|
let tok = iter.next().unwrap();
|
|
|
|
let exp = Token::Index(idx);
|
|
|
|
assert!(tok == &exp, "'{}' token was expected, {:?} was parsed", idx, tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut header = create_header();
|
|
|
|
let res = EntryHeader::walk_header(&mut header, tokens);
|
|
|
|
assert_eq!(&mut Value::Integer(exp), res.unwrap());
|
|
|
|
}
|
|
|
|
|
2016-02-10 16:07:58 +00:00
|
|
|
#[test]
|
|
|
|
fn test_header_read() {
|
|
|
|
let v = create_header();
|
|
|
|
let h = match v {
|
|
|
|
Value::Table(t) => EntryHeader::from_table(t),
|
|
|
|
_ => panic!("create_header() doesn't return a table!"),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Array(_))) = h.read("a.array") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.1") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.9") { true } else { false });
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("c") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Array(_))) = h.read("c.array") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::String(_))) = h.read("c.array.1") { true } else { false });
|
|
|
|
assert!(if let Ok(None) = h.read("c.array.9") { true } else { false });
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.0.d1") { true } else { false });
|
|
|
|
assert!(if let Ok(None) = h.read("d.array.0.d2") { true } else { false });
|
|
|
|
assert!(if let Ok(None) = h.read("d.array.0.d3") { true } else { false });
|
|
|
|
|
|
|
|
assert!(if let Ok(None) = h.read("d.array.1.d1") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.1.d2") { true } else { false });
|
|
|
|
assert!(if let Ok(None) = h.read("d.array.1.d3") { true } else { false });
|
|
|
|
|
|
|
|
assert!(if let Ok(None) = h.read("d.array.2.d1") { true } else { false });
|
|
|
|
assert!(if let Ok(None) = h.read("d.array.2.d2") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("d.array.2.d3") { true } else { false });
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::String(_))) = h.read("d.something") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("d.and") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("d.and.something") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::String(_))) = h.read("d.and.something.totally") { true } else { false });
|
|
|
|
}
|
2016-02-11 14:22:23 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header_set_override() {
|
|
|
|
let _ = env_logger::init();
|
|
|
|
let v = create_header();
|
|
|
|
let mut h = match v {
|
|
|
|
Value::Table(t) => EntryHeader::from_table(t),
|
|
|
|
_ => panic!("create_header() doesn't return a table!"),
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("Testing index 0");
|
|
|
|
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
|
|
|
|
|
|
|
println!("Altering index 0");
|
|
|
|
assert_eq!(h.set("a.array.0", Value::Integer(42)).unwrap().unwrap(), Value::Integer(0));
|
|
|
|
|
|
|
|
println!("Values now: {:?}", h);
|
|
|
|
|
|
|
|
println!("Testing all indexes");
|
|
|
|
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(42));
|
|
|
|
assert_eq!(h.read("a.array.1").unwrap().unwrap(), Value::Integer(1));
|
|
|
|
assert_eq!(h.read("a.array.2").unwrap().unwrap(), Value::Integer(2));
|
|
|
|
assert_eq!(h.read("a.array.3").unwrap().unwrap(), Value::Integer(3));
|
|
|
|
assert_eq!(h.read("a.array.4").unwrap().unwrap(), Value::Integer(4));
|
|
|
|
assert_eq!(h.read("a.array.5").unwrap().unwrap(), Value::Integer(5));
|
|
|
|
assert_eq!(h.read("a.array.6").unwrap().unwrap(), Value::Integer(6));
|
|
|
|
assert_eq!(h.read("a.array.7").unwrap().unwrap(), Value::Integer(7));
|
|
|
|
assert_eq!(h.read("a.array.8").unwrap().unwrap(), Value::Integer(8));
|
|
|
|
assert_eq!(h.read("a.array.9").unwrap().unwrap(), Value::Integer(9));
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:28:35 +00:00
|
|
|
#[test]
|
|
|
|
fn test_header_set_new() {
|
|
|
|
let _ = env_logger::init();
|
|
|
|
let v = create_header();
|
|
|
|
let mut h = match v {
|
|
|
|
Value::Table(t) => EntryHeader::from_table(t),
|
|
|
|
_ => panic!("create_header() doesn't return a table!"),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(h.read("a.foo").is_ok());
|
|
|
|
assert!(h.read("a.foo").unwrap().is_none());
|
|
|
|
|
|
|
|
{
|
|
|
|
let v = h.set("a.foo", Value::Integer(42));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert!(v.unwrap().is_none());
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.foo") { true } else { false });
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let v = h.set("new", Value::Table(BTreeMap::new()));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert!(v.unwrap().is_none());
|
|
|
|
|
|
|
|
let v = h.set("new.subset", Value::Table(BTreeMap::new()));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert!(v.unwrap().is_none());
|
|
|
|
|
|
|
|
let v = h.set("new.subset.dest", Value::Integer(1337));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert!(v.unwrap().is_none());
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("new") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("new.subset") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("new.subset.dest") { true } else { false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:38:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header_insert_override() {
|
|
|
|
let _ = env_logger::init();
|
|
|
|
let v = create_header();
|
|
|
|
let mut h = match v {
|
|
|
|
Value::Table(t) => EntryHeader::from_table(t),
|
|
|
|
_ => panic!("create_header() doesn't return a table!"),
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("Testing index 0");
|
|
|
|
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
|
|
|
|
|
|
|
println!("Altering index 0");
|
|
|
|
assert_eq!(h.insert("a.array.0", Value::Integer(42)).unwrap(), false);
|
|
|
|
println!("...should have failed");
|
|
|
|
|
|
|
|
println!("Testing all indexes");
|
|
|
|
assert_eq!(h.read("a.array.0").unwrap().unwrap(), Value::Integer(0));
|
|
|
|
assert_eq!(h.read("a.array.1").unwrap().unwrap(), Value::Integer(1));
|
|
|
|
assert_eq!(h.read("a.array.2").unwrap().unwrap(), Value::Integer(2));
|
|
|
|
assert_eq!(h.read("a.array.3").unwrap().unwrap(), Value::Integer(3));
|
|
|
|
assert_eq!(h.read("a.array.4").unwrap().unwrap(), Value::Integer(4));
|
|
|
|
assert_eq!(h.read("a.array.5").unwrap().unwrap(), Value::Integer(5));
|
|
|
|
assert_eq!(h.read("a.array.6").unwrap().unwrap(), Value::Integer(6));
|
|
|
|
assert_eq!(h.read("a.array.7").unwrap().unwrap(), Value::Integer(7));
|
|
|
|
assert_eq!(h.read("a.array.8").unwrap().unwrap(), Value::Integer(8));
|
|
|
|
assert_eq!(h.read("a.array.9").unwrap().unwrap(), Value::Integer(9));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_header_insert_new() {
|
|
|
|
let _ = env_logger::init();
|
|
|
|
let v = create_header();
|
|
|
|
let mut h = match v {
|
|
|
|
Value::Table(t) => EntryHeader::from_table(t),
|
|
|
|
_ => panic!("create_header() doesn't return a table!"),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(h.read("a.foo").is_ok());
|
|
|
|
assert!(h.read("a.foo").unwrap().is_none());
|
|
|
|
|
|
|
|
{
|
|
|
|
let v = h.insert("a.foo", Value::Integer(42));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert_eq!(v.unwrap(), true);
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.foo") { true } else { false });
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let v = h.insert("new", Value::Table(BTreeMap::new()));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert_eq!(v.unwrap(), true);
|
|
|
|
|
|
|
|
let v = h.insert("new.subset", Value::Table(BTreeMap::new()));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert_eq!(v.unwrap(), true);
|
|
|
|
|
|
|
|
let v = h.insert("new.subset.dest", Value::Integer(1337));
|
|
|
|
assert!(v.is_ok());
|
|
|
|
assert_eq!(v.unwrap(), true);
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("new") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("new.subset") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("new.subset.dest") { true } else { false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:57:23 +00:00
|
|
|
#[test]
|
|
|
|
fn test_header_delete() {
|
|
|
|
let _ = env_logger::init();
|
|
|
|
let v = create_header();
|
|
|
|
let mut h = match v {
|
|
|
|
Value::Table(t) => EntryHeader::from_table(t),
|
|
|
|
_ => panic!("create_header() doesn't return a table!"),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.read("a") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Array(_))) = h.read("a.array") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.1") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(_))) = h.read("a.array.9") { true } else { false });
|
|
|
|
|
|
|
|
assert!(if let Ok(Some(Value::Integer(1))) = h.delete("a.array.1") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Integer(9))) = h.delete("a.array.8") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Array(_))) = h.delete("a.array") { true } else { false });
|
|
|
|
assert!(if let Ok(Some(Value::Table(_))) = h.delete("a") { true } else { false });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:47:06 +00:00
|
|
|
}
|
2016-01-23 15:26:02 +00:00
|
|
|
|
2016-09-05 13:01:14 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod store_tests {
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use super::Store;
|
|
|
|
|
2016-09-07 10:47:52 +00:00
|
|
|
pub fn get_store() -> Store {
|
2016-09-05 13:01:14 +00:00
|
|
|
Store::new(PathBuf::from("/"), None).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_store_instantiation() {
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
assert_eq!(store.location, PathBuf::from("/"));
|
|
|
|
assert!(store.entries.read().unwrap().is_empty());
|
|
|
|
|
|
|
|
assert!(store.store_unload_aspects.lock().unwrap().is_empty());
|
|
|
|
|
|
|
|
assert!(store.pre_create_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.post_create_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.pre_retrieve_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.post_retrieve_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.pre_update_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.post_update_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.pre_delete_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.post_delete_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.pre_move_aspects.lock().unwrap().is_empty());
|
|
|
|
assert!(store.post_move_aspects.lock().unwrap().is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_store_create() {
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
for n in 1..100 {
|
|
|
|
let s = format!("test-{}", n);
|
|
|
|
let entry = store.create(PathBuf::from(s.clone())).unwrap();
|
|
|
|
assert!(entry.verify().is_ok());
|
|
|
|
let loc = entry.get_location().clone().into_pathbuf().unwrap();
|
|
|
|
assert!(loc.starts_with("/"));
|
|
|
|
assert!(loc.ends_with(s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-09-20 14:43:32 +00:00
|
|
|
fn test_store_get_create_get_delete_get() {
|
2016-09-05 13:01:14 +00:00
|
|
|
let store = get_store();
|
|
|
|
|
2016-09-20 14:43:32 +00:00
|
|
|
for n in 1..100 {
|
|
|
|
let res = store.get(PathBuf::from(format!("test-{}", n)));
|
|
|
|
assert!(match res { Ok(None) => true, _ => false, })
|
|
|
|
}
|
|
|
|
|
2016-09-05 13:01:14 +00:00
|
|
|
for n in 1..100 {
|
|
|
|
let s = format!("test-{}", n);
|
|
|
|
let entry = store.create(PathBuf::from(s.clone())).unwrap();
|
2016-09-20 14:43:32 +00:00
|
|
|
|
2016-09-05 13:01:14 +00:00
|
|
|
assert!(entry.verify().is_ok());
|
2016-09-20 14:43:32 +00:00
|
|
|
|
2016-09-05 13:01:14 +00:00
|
|
|
let loc = entry.get_location().clone().into_pathbuf().unwrap();
|
2016-09-20 14:43:32 +00:00
|
|
|
|
2016-09-05 13:01:14 +00:00
|
|
|
assert!(loc.starts_with("/"));
|
|
|
|
assert!(loc.ends_with(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
for n in 1..100 {
|
2016-09-20 14:43:32 +00:00
|
|
|
let res = store.get(PathBuf::from(format!("test-{}", n)));
|
|
|
|
assert!(match res { Ok(Some(_)) => true, _ => false, })
|
2016-09-05 13:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for n in 1..100 {
|
2016-09-20 14:43:32 +00:00
|
|
|
assert!(store.delete(PathBuf::from(format!("test-{}", n))).is_ok())
|
|
|
|
}
|
|
|
|
|
|
|
|
for n in 1..100 {
|
|
|
|
let res = store.get(PathBuf::from(format!("test-{}", n)));
|
|
|
|
assert!(match res { Ok(None) => true, _ => false, })
|
2016-09-05 13:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_store_create_twice() {
|
|
|
|
use error::StoreErrorKind as SEK;
|
|
|
|
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
for n in 1..100 {
|
|
|
|
let s = format!("test-{}", n % 50);
|
|
|
|
store.create(PathBuf::from(s.clone()))
|
|
|
|
.map_err(|e| assert!(is_match!(e.err_type(), SEK::CreateCallError) && n >= 50))
|
|
|
|
.ok()
|
|
|
|
.map(|entry| {
|
|
|
|
assert!(entry.verify().is_ok());
|
|
|
|
let loc = entry.get_location().clone().into_pathbuf().unwrap();
|
|
|
|
assert!(loc.starts_with("/"));
|
|
|
|
assert!(loc.ends_with(s));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 13:20:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_store_create_in_hm() {
|
|
|
|
use storeid::StoreId;
|
|
|
|
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
for n in 1..100 {
|
|
|
|
let pb = StoreId::new_baseless(PathBuf::from(format!("test-{}", n))).unwrap();
|
|
|
|
|
|
|
|
assert!(store.entries.read().unwrap().get(&pb).is_none());
|
|
|
|
assert!(store.create(pb.clone()).is_ok());
|
|
|
|
|
|
|
|
let pb = pb.with_base(store.path().clone());
|
|
|
|
assert!(store.entries.read().unwrap().get(&pb).is_some());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 13:22:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_store_retrieve_in_hm() {
|
|
|
|
use storeid::StoreId;
|
|
|
|
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
for n in 1..100 {
|
|
|
|
let pb = StoreId::new_baseless(PathBuf::from(format!("test-{}", n))).unwrap();
|
|
|
|
|
|
|
|
assert!(store.entries.read().unwrap().get(&pb).is_none());
|
|
|
|
assert!(store.retrieve(pb.clone()).is_ok());
|
|
|
|
|
|
|
|
let pb = pb.with_base(store.path().clone());
|
|
|
|
assert!(store.entries.read().unwrap().get(&pb).is_some());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 09:01:56 +00:00
|
|
|
#[test]
|
|
|
|
fn test_get_none() {
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
for n in 1..100 {
|
|
|
|
match store.get(PathBuf::from(format!("test-{}", n))) {
|
|
|
|
Ok(None) => assert!(true),
|
|
|
|
_ => assert!(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 09:03:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delete_none() {
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
for n in 1..100 {
|
|
|
|
match store.delete(PathBuf::from(format!("test-{}", n))) {
|
|
|
|
Err(_) => assert!(true),
|
|
|
|
_ => assert!(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 09:18:49 +00:00
|
|
|
|
|
|
|
// Disabled because we cannot test this by now, as we rely on glob() in
|
|
|
|
// Store::retieve_for_module(), which accesses the filesystem and tests run in-memory, so there
|
|
|
|
// are no files on the filesystem in this test after Store::create().
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_retrieve_for_module() {
|
|
|
|
// let pathes = vec![
|
|
|
|
// "foo/1", "foo/2", "foo/3", "foo/4", "foo/5",
|
|
|
|
// "bar/1", "bar/2", "bar/3", "bar/4", "bar/5",
|
|
|
|
// "bla/1", "bla/2", "bla/3", "bla/4", "bla/5",
|
|
|
|
// "boo/1", "boo/2", "boo/3", "boo/4", "boo/5",
|
|
|
|
// "glu/1", "glu/2", "glu/3", "glu/4", "glu/5",
|
|
|
|
// ];
|
|
|
|
|
|
|
|
// fn test(store: &Store, modulename: &str) {
|
|
|
|
// use std::path::Component;
|
|
|
|
// use storeid::StoreId;
|
|
|
|
|
|
|
|
// let retrieved = store.retrieve_for_module(modulename);
|
|
|
|
// assert!(retrieved.is_ok());
|
|
|
|
// let v : Vec<StoreId> = retrieved.unwrap().collect();
|
|
|
|
// println!("v = {:?}", v);
|
|
|
|
// assert!(v.len() == 5);
|
|
|
|
|
|
|
|
// let retrieved = store.retrieve_for_module(modulename);
|
|
|
|
// assert!(retrieved.is_ok());
|
|
|
|
|
|
|
|
// assert!(retrieved.unwrap().all(|e| {
|
|
|
|
// let first = e.components().next();
|
|
|
|
// assert!(first.is_some());
|
|
|
|
// match first.unwrap() {
|
|
|
|
// Component::Normal(s) => s == modulename,
|
|
|
|
// _ => false,
|
|
|
|
// }
|
|
|
|
// }))
|
|
|
|
// }
|
|
|
|
|
|
|
|
// let store = get_store();
|
|
|
|
// for path in pathes {
|
|
|
|
// assert!(store.create(PathBuf::from(path)).is_ok());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// test(&store, "foo");
|
|
|
|
// test(&store, "bar");
|
|
|
|
// test(&store, "bla");
|
|
|
|
// test(&store, "boo");
|
|
|
|
// test(&store, "glu");
|
|
|
|
// }
|
|
|
|
|
2016-09-05 13:01:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 10:47:52 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod store_hook_tests {
|
|
|
|
|
|
|
|
mod succeeding_hook {
|
|
|
|
use hook::Hook;
|
|
|
|
use hook::accessor::HookDataAccessor;
|
|
|
|
use hook::accessor::HookDataAccessorProvider;
|
|
|
|
use hook::position::HookPosition;
|
|
|
|
|
|
|
|
use self::accessor::SucceedingHookAccessor as DHA;
|
|
|
|
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SucceedingHook {
|
|
|
|
position: HookPosition,
|
|
|
|
accessor: DHA,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SucceedingHook {
|
|
|
|
|
|
|
|
pub fn new(pos: HookPosition) -> SucceedingHook {
|
|
|
|
SucceedingHook { position: pos.clone(), accessor: DHA::new(pos) }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hook for SucceedingHook {
|
|
|
|
fn name(&self) -> &'static str { "testhook_succeeding" }
|
|
|
|
fn set_config(&mut self, _: &Value) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HookDataAccessorProvider for SucceedingHook {
|
|
|
|
|
|
|
|
fn accessor(&self) -> HookDataAccessor {
|
|
|
|
use hook::position::HookPosition as HP;
|
|
|
|
use hook::accessor::HookDataAccessor as HDA;
|
|
|
|
|
|
|
|
match self.position {
|
|
|
|
HP::StoreUnload |
|
|
|
|
HP::PreCreate |
|
|
|
|
HP::PreRetrieve |
|
|
|
|
HP::PreDelete |
|
|
|
|
HP::PostDelete => HDA::StoreIdAccess(&self.accessor),
|
|
|
|
HP::PostCreate |
|
|
|
|
HP::PostRetrieve |
|
|
|
|
HP::PreUpdate |
|
|
|
|
HP::PostUpdate => HDA::MutableAccess(&self.accessor),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod accessor {
|
|
|
|
use hook::result::HookResult;
|
|
|
|
use hook::accessor::MutableHookDataAccessor;
|
|
|
|
use hook::accessor::NonMutableHookDataAccessor;
|
|
|
|
use hook::accessor::StoreIdAccessor;
|
|
|
|
use hook::position::HookPosition;
|
|
|
|
use store::FileLockEntry;
|
|
|
|
use storeid::StoreId;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SucceedingHookAccessor(HookPosition);
|
|
|
|
|
|
|
|
impl SucceedingHookAccessor {
|
|
|
|
|
|
|
|
pub fn new(position: HookPosition) -> SucceedingHookAccessor {
|
|
|
|
SucceedingHookAccessor(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StoreIdAccessor for SucceedingHookAccessor {
|
|
|
|
|
|
|
|
fn access(&self, id: &StoreId) -> HookResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MutableHookDataAccessor for SucceedingHookAccessor {
|
|
|
|
|
|
|
|
fn access_mut(&self, fle: &mut FileLockEntry) -> HookResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NonMutableHookDataAccessor for SucceedingHookAccessor {
|
|
|
|
|
|
|
|
fn access(&self, fle: &FileLockEntry) -> HookResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:49:50 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use hook::position::HookPosition as HP;
|
|
|
|
use storeid::StoreId;
|
|
|
|
use store::Store;
|
|
|
|
|
|
|
|
use self::succeeding_hook::SucceedingHook;
|
|
|
|
|
|
|
|
fn get_store_with_config() -> Store {
|
|
|
|
use toml::Parser;
|
|
|
|
|
|
|
|
let cfg = Parser::new(mini_config()).parse().unwrap();
|
|
|
|
println!("Config parsed: {:?}", cfg);
|
|
|
|
Store::new(PathBuf::from("/"), Some(cfg.get("store").cloned().unwrap())).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mini_config() -> &'static str {
|
|
|
|
r#"
|
|
|
|
[store]
|
|
|
|
store-unload-hook-aspects = [ "test" ]
|
|
|
|
pre-create-hook-aspects = [ "test" ]
|
|
|
|
post-create-hook-aspects = [ "test" ]
|
|
|
|
pre-move-hook-aspects = [ "test" ]
|
|
|
|
post-move-hook-aspects = [ "test" ]
|
|
|
|
pre-retrieve-hook-aspects = [ "test" ]
|
|
|
|
post-retrieve-hook-aspects = [ "test" ]
|
|
|
|
pre-update-hook-aspects = [ "test" ]
|
|
|
|
post-update-hook-aspects = [ "test" ]
|
|
|
|
pre-delete-hook-aspects = [ "test" ]
|
|
|
|
post-delete-hook-aspects = [ "test" ]
|
|
|
|
|
|
|
|
[store.aspects.test]
|
|
|
|
parallel = false
|
|
|
|
mutable_hooks = true
|
|
|
|
|
|
|
|
[store.hooks.testhook_succeeding]
|
|
|
|
aspect = "test"
|
|
|
|
"#
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pre_create() {
|
|
|
|
let mut store = get_store_with_config();
|
|
|
|
let pos = HP::PreCreate;
|
|
|
|
let hook = SucceedingHook::new(pos.clone());
|
|
|
|
|
|
|
|
assert!(store.register_hook(pos, "test", Box::new(hook)).map_err(|e| println!("{:?}", e)).is_ok());
|
|
|
|
|
|
|
|
let pb = StoreId::new_baseless(PathBuf::from("test")).unwrap();
|
|
|
|
assert!(store.create(pb.clone()).is_ok());
|
|
|
|
}
|
|
|
|
|
2016-09-07 10:47:52 +00:00
|
|
|
}
|
|
|
|
|