imag/libimagstore/src/hook.rs

170 lines
3.5 KiB
Rust
Raw Normal View History

2016-02-15 20:27:51 +00:00
use self::error::HookError;
2016-02-16 19:16:45 +00:00
pub type HookResult<T> = Result<T, HookError>;
2016-02-15 20:27:51 +00:00
pub mod read {
2016-02-17 15:15:11 +00:00
use std::fmt::Debug;
2016-02-16 19:16:45 +00:00
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
2016-02-15 20:27:51 +00:00
2016-02-17 15:15:11 +00:00
pub trait PreReadHook : Debug {
2016-02-16 19:16:45 +00:00
fn pre_read(&self, &StoreId) -> HookResult<()>;
2016-02-15 20:27:51 +00:00
}
2016-02-17 15:15:11 +00:00
pub trait PostReadHook : Debug {
2016-02-16 19:16:45 +00:00
fn post_read<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
2016-02-15 20:27:51 +00:00
}
}
pub mod create {
2016-02-17 15:15:11 +00:00
use std::fmt::Debug;
2016-02-16 19:16:45 +00:00
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
2016-02-15 20:27:51 +00:00
2016-02-17 15:15:11 +00:00
pub trait PreCreateHook : Debug {
2016-02-16 19:16:45 +00:00
fn pre_create(&self, &StoreId) -> HookResult<()>;
2016-02-15 20:27:51 +00:00
}
2016-02-17 15:15:11 +00:00
pub trait PostCreateHook : Debug {
2016-02-16 19:16:45 +00:00
fn post_create<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
2016-02-15 20:27:51 +00:00
}
}
pub mod retrieve {
2016-02-17 15:15:11 +00:00
use std::fmt::Debug;
2016-02-16 19:16:45 +00:00
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
2016-02-17 15:15:11 +00:00
pub trait PreRetrieveHook : Debug {
2016-02-16 19:16:45 +00:00
fn pre_retrieve(&self, &StoreId) -> HookResult<()>;
}
2016-02-17 15:15:11 +00:00
pub trait PostRetrieveHook : Debug {
2016-02-16 19:16:45 +00:00
fn post_retrieve<'a>(&self, FileLockEntry<'a>) -> HookResult<FileLockEntry<'a>>;
}
}
pub mod update {
2016-02-17 15:15:11 +00:00
use std::fmt::Debug;
2016-02-16 19:16:45 +00:00
use store::FileLockEntry;
use super::HookResult;
2016-02-15 20:27:51 +00:00
2016-02-17 15:15:11 +00:00
pub trait PreUpdateHook : Debug {
2016-02-16 19:16:45 +00:00
fn pre_update(&self, &FileLockEntry) -> HookResult<()>;
2016-02-15 20:27:51 +00:00
}
2016-02-17 15:15:11 +00:00
pub trait PostUpdateHook : Debug {
2016-02-16 19:16:45 +00:00
fn post_update(&self, &FileLockEntry) -> HookResult<()>;
2016-02-15 20:27:51 +00:00
}
}
pub mod delete {
2016-02-17 15:15:11 +00:00
use std::fmt::Debug;
2016-02-16 19:16:45 +00:00
use storeid::StoreId;
use store::FileLockEntry;
use super::HookResult;
2016-02-15 20:27:51 +00:00
2016-02-17 15:15:11 +00:00
pub trait PreDeleteHook : Debug {
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:15:11 +00:00
pub trait PostDeleteHook : Debug {
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};
/**
* Kind of error
*/
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HookErrorKind {
// ...
}
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)
}
}
}