2016-02-17 15:33:41 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
use toml::Value;
|
|
|
|
|
2016-02-15 20:27:51 +00:00
|
|
|
use self::error::HookError;
|
2016-02-22 18:23:49 +00:00
|
|
|
use store::FileLockEntry;
|
2016-02-15 20:27:51 +00:00
|
|
|
|
2016-02-16 19:16:45 +00:00
|
|
|
pub type HookResult<T> = Result<T, HookError>;
|
2016-02-15 20:27:51 +00:00
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub trait Configureable {
|
|
|
|
fn set_config(&mut self, cfg: Value);
|
|
|
|
}
|
|
|
|
|
2016-02-22 18:23:49 +00:00
|
|
|
pub trait MutableHookDataAccessor : Send + Sync {
|
|
|
|
fn access_mut(&self, &mut FileLockEntry) -> HookResult<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait NonMutableHookDataAccessor : Send + Sync {
|
|
|
|
fn access(&self, &FileLockEntry) -> HookResult<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum HookDataAccessor {
|
|
|
|
MutableAccess(Box<MutableHookDataAccessor>),
|
|
|
|
NonMutableAccess(Box<NonMutableHookDataAccessor>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait HookDataAccessorProvider {
|
|
|
|
fn accessor(&self) -> Box<HookDataAccessor>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Hook : Configureable + Debug + Send + Sync {
|
2016-02-17 15:33:41 +00:00
|
|
|
}
|
2016-02-17 15:15:11 +00:00
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub mod read {
|
2016-02-16 19:16:45 +00:00
|
|
|
use storeid::StoreId;
|
|
|
|
use store::FileLockEntry;
|
2016-02-22 18:23:49 +00:00
|
|
|
use super::HookDataAccessorProvider;
|
2016-02-16 19:16:45 +00:00
|
|
|
use super::HookResult;
|
2016-02-17 15:33:41 +00:00
|
|
|
use super::Hook;
|
2016-02-15 20:27:51 +00:00
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub trait PreReadHook : Hook {
|
2016-02-16 19:16:45 +00:00
|
|
|
fn pre_read(&self, &StoreId) -> HookResult<()>;
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 18:23:49 +00:00
|
|
|
pub trait PostReadHook : Hook + HookDataAccessorProvider {
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod create {
|
2016-02-16 19:16:45 +00:00
|
|
|
use storeid::StoreId;
|
|
|
|
use store::FileLockEntry;
|
2016-02-22 18:23:49 +00:00
|
|
|
use super::HookDataAccessorProvider;
|
2016-02-16 19:16:45 +00:00
|
|
|
use super::HookResult;
|
2016-02-17 15:33:41 +00:00
|
|
|
use super::Hook;
|
2016-02-15 20:27:51 +00:00
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub trait PreCreateHook : Hook {
|
2016-02-16 19:16:45 +00:00
|
|
|
fn pre_create(&self, &StoreId) -> HookResult<()>;
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 18:23:49 +00:00
|
|
|
pub trait PostCreateHook : Hook + HookDataAccessorProvider {
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod retrieve {
|
2016-02-16 19:16:45 +00:00
|
|
|
use storeid::StoreId;
|
|
|
|
use store::FileLockEntry;
|
2016-02-22 18:23:49 +00:00
|
|
|
use super::HookDataAccessorProvider;
|
2016-02-16 19:16:45 +00:00
|
|
|
use super::HookResult;
|
2016-02-17 15:33:41 +00:00
|
|
|
use super::Hook;
|
2016-02-16 19:16:45 +00:00
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub trait PreRetrieveHook : Hook {
|
2016-02-16 19:16:45 +00:00
|
|
|
fn pre_retrieve(&self, &StoreId) -> HookResult<()>;
|
|
|
|
}
|
|
|
|
|
2016-02-22 18:23:49 +00:00
|
|
|
pub trait PostRetrieveHook : Hook + HookDataAccessorProvider {
|
2016-02-16 19:16:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod update {
|
|
|
|
use store::FileLockEntry;
|
2016-02-22 18:23:49 +00:00
|
|
|
use super::HookDataAccessorProvider;
|
2016-02-16 19:16:45 +00:00
|
|
|
use super::HookResult;
|
2016-02-17 15:33:41 +00:00
|
|
|
use super::Hook;
|
2016-02-15 20:27:51 +00:00
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub trait PreUpdateHook : Hook {
|
2016-02-16 19:16:45 +00:00
|
|
|
fn pre_update(&self, &FileLockEntry) -> HookResult<()>;
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 18:23:49 +00:00
|
|
|
pub trait PostUpdateHook : Hook + HookDataAccessorProvider {
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod delete {
|
2016-02-16 19:16:45 +00:00
|
|
|
use storeid::StoreId;
|
|
|
|
use store::FileLockEntry;
|
2016-02-22 18:23:49 +00:00
|
|
|
use super::HookDataAccessorProvider;
|
2016-02-16 19:16:45 +00:00
|
|
|
use super::HookResult;
|
2016-02-17 15:33:41 +00:00
|
|
|
use super::Hook;
|
2016-02-15 20:27:51 +00:00
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub trait PreDeleteHook : Hook {
|
2016-02-16 19:16:45 +00:00
|
|
|
fn pre_delete(&self, &StoreId) -> HookResult<()>;
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 15:33:41 +00:00
|
|
|
pub trait PostDeleteHook : Hook {
|
2016-02-16 19:16:45 +00:00
|
|
|
fn post_delete(&self, &StoreId) -> HookResult<()>;
|
2016-02-15 20:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod error {
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::Error as FmtError;
|
|
|
|
use std::clone::Clone;
|
|
|
|
use std::fmt::{Display, Formatter};
|
2016-02-17 18:17:51 +00:00
|
|
|
use std::convert::Into;
|
2016-02-15 20:27:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Kind of error
|
|
|
|
*/
|
2016-02-17 18:17:51 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2016-02-15 20:27:51 +00:00
|
|
|
pub enum HookErrorKind {
|
2016-02-17 18:17:51 +00:00
|
|
|
Pre(PreHookErrorKind),
|
|
|
|
Post(PostHookErrorKind)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum PreHookErrorKind {
|
2016-02-15 20:27:51 +00:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:17:51 +00:00
|
|
|
impl Into<HookErrorKind> for PreHookErrorKind {
|
|
|
|
fn into(self) -> HookErrorKind {
|
|
|
|
HookErrorKind::Pre(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum PostHookErrorKind {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HookErrorKind> for PostHookErrorKind {
|
|
|
|
fn into(self) -> HookErrorKind {
|
|
|
|
HookErrorKind::Post(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:22:08 +00:00
|
|
|
pub trait IntoHookError {
|
|
|
|
fn into_hookerror(self) -> HookError;
|
|
|
|
fn into_hookerror_with_cause(self, cause: Box<Error>) -> HookError;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HookError> for HookErrorKind {
|
|
|
|
|
|
|
|
fn into(self) -> HookError {
|
|
|
|
HookError::new(self, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HookError> for (HookErrorKind, Box<Error>) {
|
|
|
|
|
|
|
|
fn into(self) -> HookError {
|
|
|
|
HookError::new(self.0, Some(self.1))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HookError> for PreHookErrorKind {
|
|
|
|
|
|
|
|
fn into(self) -> HookError {
|
|
|
|
HookError::new(HookErrorKind::Pre(self), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HookError> for (PreHookErrorKind, Box<Error>) {
|
|
|
|
|
|
|
|
fn into(self) -> HookError {
|
|
|
|
HookError::new(HookErrorKind::Pre(self.0), Some(self.1))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HookError> for PostHookErrorKind {
|
|
|
|
|
|
|
|
fn into(self) -> HookError {
|
|
|
|
HookError::new(HookErrorKind::Post(self), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HookError> for (PostHookErrorKind, Box<Error>) {
|
|
|
|
|
|
|
|
fn into(self) -> HookError {
|
|
|
|
HookError::new(HookErrorKind::Post(self.0), Some(self.1))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-15 20:27:51 +00:00
|
|
|
fn hook_error_type_as_str(e: &HookErrorKind) -> &'static str {
|
|
|
|
match e {
|
|
|
|
_ => "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for HookErrorKind {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
try!(write!(fmt, "{}", hook_error_type_as_str(self)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error type
|
|
|
|
*/
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct HookError {
|
|
|
|
err_type: HookErrorKind,
|
|
|
|
cause: Option<Box<Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HookError {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a new HookError from an HookErrorKind, optionally with cause
|
|
|
|
*/
|
|
|
|
pub fn new(errtype: HookErrorKind, cause: Option<Box<Error>>)
|
|
|
|
-> HookError
|
|
|
|
{
|
|
|
|
HookError {
|
|
|
|
err_type: errtype,
|
|
|
|
cause: cause,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the error type of this HookError
|
|
|
|
*/
|
|
|
|
pub fn err_type(&self) -> HookErrorKind {
|
|
|
|
self.err_type.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for HookError {
|
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
|
|
|
try!(write!(fmt, "[{}]", hook_error_type_as_str(&self.err_type.clone())));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for HookError {
|
|
|
|
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
hook_error_type_as_str(&self.err_type.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
self.cause.as_ref().map(|e| &**e)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|