Redefine hook trait system
This commit is contained in:
parent
001c80b72d
commit
17b1d6c390
2 changed files with 65 additions and 32 deletions
|
@ -1,64 +1,75 @@
|
||||||
use storeid::StoreId;
|
|
||||||
use store::FileLockEntry;
|
|
||||||
|
|
||||||
use self::error::HookError;
|
use self::error::HookError;
|
||||||
|
|
||||||
pub type HookResult = Result<(), HookError>;
|
pub type HookResult<T> = Result<T, 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 mod read {
|
pub mod read {
|
||||||
use super::FileLockHook;
|
use storeid::StoreId;
|
||||||
use super::StoreIdHook;
|
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 {
|
pub mod create {
|
||||||
use super::FileLockHook;
|
use storeid::StoreId;
|
||||||
use super::StoreIdHook;
|
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 {
|
pub mod retrieve {
|
||||||
use super::FileLockHook;
|
use storeid::StoreId;
|
||||||
use super::StoreIdHook;
|
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 {
|
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<()>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ use error::{StoreError, StoreErrorKind};
|
||||||
use storeid::{StoreId, StoreIdIterator};
|
use storeid::{StoreId, StoreIdIterator};
|
||||||
use lazyfile::LazyFile;
|
use lazyfile::LazyFile;
|
||||||
|
|
||||||
use hook::Hook;
|
|
||||||
use hook::read::{PreReadHook, PostReadHook};
|
use hook::read::{PreReadHook, PostReadHook};
|
||||||
use hook::create::{PreCreateHook, PostCreateHook};
|
use hook::create::{PreCreateHook, PostCreateHook};
|
||||||
use hook::retrieve::{PreRetrieveHook, PostRetrieveHook};
|
use hook::retrieve::{PreRetrieveHook, PostRetrieveHook};
|
||||||
|
@ -109,6 +108,8 @@ pub struct Store {
|
||||||
post_create_hooks : Arc<Mutex<Vec<Box<PostCreateHook>>>>,
|
post_create_hooks : Arc<Mutex<Vec<Box<PostCreateHook>>>>,
|
||||||
pre_retrieve_hooks : Arc<Mutex<Vec<Box<PreRetrieveHook>>>>,
|
pre_retrieve_hooks : Arc<Mutex<Vec<Box<PreRetrieveHook>>>>,
|
||||||
post_retrieve_hooks : Arc<Mutex<Vec<Box<PostRetrieveHook>>>>,
|
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>>>>,
|
pre_delete_hooks : Arc<Mutex<Vec<Box<PreDeleteHook>>>>,
|
||||||
post_delete_hooks : Arc<Mutex<Vec<Box<PostDeleteHook>>>>,
|
post_delete_hooks : Arc<Mutex<Vec<Box<PostDeleteHook>>>>,
|
||||||
|
|
||||||
|
@ -153,6 +154,8 @@ impl Store {
|
||||||
post_create_hooks : Arc::new(Mutex::new(vec![])),
|
post_create_hooks : Arc::new(Mutex::new(vec![])),
|
||||||
pre_retrieve_hooks : Arc::new(Mutex::new(vec![])),
|
pre_retrieve_hooks : Arc::new(Mutex::new(vec![])),
|
||||||
post_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![])),
|
pre_delete_hooks : Arc::new(Mutex::new(vec![])),
|
||||||
post_delete_hooks : Arc::new(Mutex::new(vec![])),
|
post_delete_hooks : Arc::new(Mutex::new(vec![])),
|
||||||
entries: Arc::new(RwLock::new(HashMap::new())),
|
entries: Arc::new(RwLock::new(HashMap::new())),
|
||||||
|
@ -336,6 +339,25 @@ impl Store {
|
||||||
.map(|mut guard| guard.deref_mut().push(h))
|
.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<()> {
|
pub fn register_pre_delete_hook(&self, h: Box<PreDeleteHook>) -> Result<()> {
|
||||||
self.pre_delete_hooks
|
self.pre_delete_hooks
|
||||||
.deref()
|
.deref()
|
||||||
|
|
Loading…
Reference in a new issue