Redefine hook trait system

This commit is contained in:
Matthias Beyer 2016-02-16 20:16:45 +01:00
parent 001c80b72d
commit 17b1d6c390
2 changed files with 65 additions and 32 deletions

View file

@ -1,64 +1,75 @@
use storeid::StoreId;
use store::FileLockEntry;
use self::error::HookError;
pub type HookResult = Result<(), HookError>;
pub trait Hook : Eq + PartialEq + Debug + Display {
fn dependencies(&self) -> Vec<Box<Hook>>;
}
pub trait StoreIdHook : Hook {
fn execute(&self, &StoreId) -> HookResult;
}
pub trait FileLockHook : Hook {
fn execute(&self, &StoreId, &FileLockEntry) -> HookResult;
}
pub type HookResult<T> = Result<T, HookError>;
pub mod read {
use super::FileLockHook;
use super::StoreIdHook;
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
pub trait PreReadHook : StoreIdHook {
pub trait PreReadHook {
fn pre_read(&self, &StoreId) -> HookResult<()>;
}
pub trait PostReadHook : FileLockHook {
pub trait PostReadHook {
fn post_read<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
}
}
pub mod create {
use super::FileLockHook;
use super::StoreIdHook;
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
pub trait PreCreateHook : StoreIdHook {
pub trait PreCreateHook {
fn pre_create(&self, &StoreId) -> HookResult<()>;
}
pub trait PostCreateHook : FileLockHook {
pub trait PostCreateHook {
fn post_create<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
}
}
pub mod retrieve {
use super::FileLockHook;
use super::StoreIdHook;
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
pub trait PreRetrieveHook : StoreIdHook {
pub trait PreRetrieveHook {
fn pre_retrieve(&self, &StoreId) -> HookResult<()>;
}
pub trait PostRetrieveHook : FileLockHook {
pub trait PostRetrieveHook {
fn post_retrieve<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
}
}
pub mod update {
use store::FileLockEntry;
use super::HookResult;
pub trait PreUpdateHook {
fn pre_update(&self, &FileLockEntry) -> HookResult<()>;
}
pub trait PostUpdateHook {
fn post_update(&self, &FileLockEntry) -> HookResult<()>;
}
}
pub mod delete {
use super::StoreIdHook;
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
pub trait PreDeleteHook : StoreIdHook {
pub trait PreDeleteHook {
fn pre_delete(&self, &StoreId) -> HookResult<()>;
}
pub trait PostDeleteHook : StoreIdHook {
pub trait PostDeleteHook {
fn post_delete(&self, &StoreId) -> HookResult<()>;
}
}

View file

@ -21,7 +21,6 @@ use error::{StoreError, StoreErrorKind};
use storeid::{StoreId, StoreIdIterator};
use lazyfile::LazyFile;
use hook::Hook;
use hook::read::{PreReadHook, PostReadHook};
use hook::create::{PreCreateHook, PostCreateHook};
use hook::retrieve::{PreRetrieveHook, PostRetrieveHook};
@ -109,6 +108,8 @@ pub struct Store {
post_create_hooks : Arc<Mutex<Vec<Box<PostCreateHook>>>>,
pre_retrieve_hooks : Arc<Mutex<Vec<Box<PreRetrieveHook>>>>,
post_retrieve_hooks : Arc<Mutex<Vec<Box<PostRetrieveHook>>>>,
pre_update_hooks : Arc<Mutex<Vec<Box<PreUpdateHook>>>>,
post_update_hooks : Arc<Mutex<Vec<Box<PostUpdateHook>>>>,
pre_delete_hooks : Arc<Mutex<Vec<Box<PreDeleteHook>>>>,
post_delete_hooks : Arc<Mutex<Vec<Box<PostDeleteHook>>>>,
@ -153,6 +154,8 @@ impl Store {
post_create_hooks : Arc::new(Mutex::new(vec![])),
pre_retrieve_hooks : Arc::new(Mutex::new(vec![])),
post_retrieve_hooks : Arc::new(Mutex::new(vec![])),
pre_update_hooks : Arc::new(Mutex::new(vec![])),
post_update_hooks : Arc::new(Mutex::new(vec![])),
pre_delete_hooks : Arc::new(Mutex::new(vec![])),
post_delete_hooks : Arc::new(Mutex::new(vec![])),
entries: Arc::new(RwLock::new(HashMap::new())),
@ -336,6 +339,25 @@ impl Store {
.map(|mut guard| guard.deref_mut().push(h))
}
pub fn register_pre_update_hook(&self, h: Box<PreUpdateHook>) -> Result<()> {
self.pre_update_hooks
.deref()
.lock()
.map_err(|_| StoreError::new(StoreErrorKind::HookRegisterError, None))
// TODO: cause: Some(Box::new(e))
.map(|mut guard| guard.deref_mut().push(h))
}
pub fn register_post_update_hook(&self, h: Box<PostUpdateHook>) -> Result<()> {
self.post_update_hooks
.deref()
.lock()
.map_err(|_| StoreError::new(StoreErrorKind::HookRegisterError, None))
// TODO: cause: Some(Box::new(e))
.map(|mut guard| guard.deref_mut().push(h))
}
pub fn register_pre_delete_hook(&self, h: Box<PreDeleteHook>) -> Result<()> {
self.pre_delete_hooks
.deref()