diff --git a/libimagstore/src/hook.rs b/libimagstore/src/hook.rs index f8738f82..3028cc98 100644 --- a/libimagstore/src/hook.rs +++ b/libimagstore/src/hook.rs @@ -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>; -} - -pub trait StoreIdHook : Hook { - fn execute(&self, &StoreId) -> HookResult; -} - -pub trait FileLockHook : Hook { - fn execute(&self, &StoreId, &FileLockEntry) -> HookResult; -} +pub type HookResult = Result; 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>; } } 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>; } } 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>; + } +} + +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<()>; } } diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index dfb3efb2..d6c5a9a4 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -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>>>, pre_retrieve_hooks : Arc>>>, post_retrieve_hooks : Arc>>>, + pre_update_hooks : Arc>>>, + post_update_hooks : Arc>>>, pre_delete_hooks : Arc>>>, post_delete_hooks : Arc>>>, @@ -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) -> 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) -> 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) -> Result<()> { self.pre_delete_hooks .deref()