Make hooks configurable

This introduces the Configurable trait and abstracts the
dependency-traits for each trait over a generic "Hook" trait, which
requires Configurable and Debug to be implemented, but has no own
functions.

Configuring is done _without_ the Runtime object, so we can be sure that
the hooks do not have access to the store object.
This commit is contained in:
Matthias Beyer 2016-02-17 16:33:41 +01:00
parent d9a4512821
commit d7733aa13a

View file

@ -1,84 +1,90 @@
use std::fmt::Debug;
use toml::Value;
use self::error::HookError; use self::error::HookError;
pub type HookResult<T> = Result<T, HookError>; pub type HookResult<T> = Result<T, HookError>;
pub mod read { pub trait Configureable {
use std::fmt::Debug; fn set_config(&mut self, cfg: Value);
}
pub trait Hook : Configureable + Debug {
}
pub mod read {
use storeid::StoreId; use storeid::StoreId;
use store::FileLockEntry; use store::FileLockEntry;
use super::HookResult; use super::HookResult;
use super::Hook;
pub trait PreReadHook : Debug { pub trait PreReadHook : Hook {
fn pre_read(&self, &StoreId) -> HookResult<()>; fn pre_read(&self, &StoreId) -> HookResult<()>;
} }
pub trait PostReadHook : Debug { pub trait PostReadHook : Hook {
fn post_read<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>; fn post_read<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
} }
} }
pub mod create { pub mod create {
use std::fmt::Debug;
use storeid::StoreId; use storeid::StoreId;
use store::FileLockEntry; use store::FileLockEntry;
use super::HookResult; use super::HookResult;
use super::Hook;
pub trait PreCreateHook : Debug { pub trait PreCreateHook : Hook {
fn pre_create(&self, &StoreId) -> HookResult<()>; fn pre_create(&self, &StoreId) -> HookResult<()>;
} }
pub trait PostCreateHook : Debug { pub trait PostCreateHook : Hook {
fn post_create<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>; fn post_create<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
} }
} }
pub mod retrieve { pub mod retrieve {
use std::fmt::Debug;
use storeid::StoreId; use storeid::StoreId;
use store::FileLockEntry; use store::FileLockEntry;
use super::HookResult; use super::HookResult;
use super::Hook;
pub trait PreRetrieveHook : Debug { pub trait PreRetrieveHook : Hook {
fn pre_retrieve(&self, &StoreId) -> HookResult<()>; fn pre_retrieve(&self, &StoreId) -> HookResult<()>;
} }
pub trait PostRetrieveHook : Debug { pub trait PostRetrieveHook : Hook {
fn post_retrieve<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>; fn post_retrieve<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
} }
} }
pub mod update { pub mod update {
use std::fmt::Debug;
use store::FileLockEntry; use store::FileLockEntry;
use super::HookResult; use super::HookResult;
use super::Hook;
pub trait PreUpdateHook : Debug { pub trait PreUpdateHook : Hook {
fn pre_update(&self, &FileLockEntry) -> HookResult<()>; fn pre_update(&self, &FileLockEntry) -> HookResult<()>;
} }
pub trait PostUpdateHook : Debug { pub trait PostUpdateHook : Hook {
fn post_update(&self, &FileLockEntry) -> HookResult<()>; fn post_update(&self, &FileLockEntry) -> HookResult<()>;
} }
} }
pub mod delete { pub mod delete {
use std::fmt::Debug;
use storeid::StoreId; use storeid::StoreId;
use store::FileLockEntry; use store::FileLockEntry;
use super::HookResult; use super::HookResult;
use super::Hook;
pub trait PreDeleteHook : Debug { pub trait PreDeleteHook : Hook {
fn pre_delete(&self, &StoreId) -> HookResult<()>; fn pre_delete(&self, &StoreId) -> HookResult<()>;
} }
pub trait PostDeleteHook : Debug { pub trait PostDeleteHook : Hook {
fn post_delete(&self, &StoreId) -> HookResult<()>; fn post_delete(&self, &StoreId) -> HookResult<()>;
} }
} }