From fa9e8e81923280b4e65e66651dd19a21247b8302 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 4 Mar 2016 16:43:01 +0100 Subject: [PATCH] Split hook/mod.rs into several files --- libimagstore/src/hook/accessor.rs | 21 +++ libimagstore/src/hook/create.rs | 13 ++ libimagstore/src/hook/delete.rs | 14 ++ libimagstore/src/hook/error.rs | 159 ++++++++++++++++++ libimagstore/src/hook/mod.rs | 269 +----------------------------- libimagstore/src/hook/read.rs | 13 ++ libimagstore/src/hook/result.rs | 3 + libimagstore/src/hook/retrieve.rs | 13 ++ libimagstore/src/hook/update.rs | 12 ++ libimagstore/src/store.rs | 6 +- 10 files changed, 260 insertions(+), 263 deletions(-) create mode 100644 libimagstore/src/hook/accessor.rs create mode 100644 libimagstore/src/hook/create.rs create mode 100644 libimagstore/src/hook/delete.rs create mode 100644 libimagstore/src/hook/error.rs create mode 100644 libimagstore/src/hook/read.rs create mode 100644 libimagstore/src/hook/result.rs create mode 100644 libimagstore/src/hook/retrieve.rs create mode 100644 libimagstore/src/hook/update.rs diff --git a/libimagstore/src/hook/accessor.rs b/libimagstore/src/hook/accessor.rs new file mode 100644 index 00000000..b3c1f0f1 --- /dev/null +++ b/libimagstore/src/hook/accessor.rs @@ -0,0 +1,21 @@ +use hook::result::HookResult; +use store::FileLockEntry; + +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), + NonMutableAccess(Box), +} + +pub trait HookDataAccessorProvider { + fn accessor(&self) -> Box; +} + + diff --git a/libimagstore/src/hook/create.rs b/libimagstore/src/hook/create.rs new file mode 100644 index 00000000..f43dd224 --- /dev/null +++ b/libimagstore/src/hook/create.rs @@ -0,0 +1,13 @@ +use storeid::StoreId; +use store::FileLockEntry; +use hook::accessor::HookDataAccessorProvider; +use hook::result::HookResult; +use hook::Hook; + +pub trait PreCreateHook : Hook { + fn pre_create(&self, &StoreId) -> HookResult<()>; +} + +pub trait PostCreateHook : Hook + HookDataAccessorProvider { +} + diff --git a/libimagstore/src/hook/delete.rs b/libimagstore/src/hook/delete.rs new file mode 100644 index 00000000..53d57cef --- /dev/null +++ b/libimagstore/src/hook/delete.rs @@ -0,0 +1,14 @@ +use storeid::StoreId; +use store::FileLockEntry; +use hook::accessor::HookDataAccessorProvider; +use hook::result::HookResult; +use hook::Hook; + +pub trait PreDeleteHook : Hook { + fn pre_delete(&self, &StoreId) -> HookResult<()>; +} + +pub trait PostDeleteHook : Hook { + fn post_delete(&self, &StoreId) -> HookResult<()>; +} + diff --git a/libimagstore/src/hook/error.rs b/libimagstore/src/hook/error.rs new file mode 100644 index 00000000..daa8af23 --- /dev/null +++ b/libimagstore/src/hook/error.rs @@ -0,0 +1,159 @@ +use std::error::Error; +use std::fmt::Error as FmtError; +use std::clone::Clone; +use std::fmt::{Display, Formatter}; +use std::convert::Into; + +/** + * Kind of error + */ +#[derive(Clone, Copy, Debug)] +pub enum HookErrorKind { + Pre(PreHookErrorKind), + Post(PostHookErrorKind) +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum PreHookErrorKind { + // ... +} + +impl Into for PreHookErrorKind { + fn into(self) -> HookErrorKind { + HookErrorKind::Pre(self) + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum PostHookErrorKind { + // ... +} + +impl Into for PostHookErrorKind { + fn into(self) -> HookErrorKind { + HookErrorKind::Post(self) + } +} + +pub trait IntoHookError { + fn into_hookerror(self) -> HookError; + fn into_hookerror_with_cause(self, cause: Box) -> HookError; +} + +impl Into for HookErrorKind { + + fn into(self) -> HookError { + HookError::new(self, None) + } + +} + +impl Into for (HookErrorKind, Box) { + + fn into(self) -> HookError { + HookError::new(self.0, Some(self.1)) + } + +} + +impl Into for PreHookErrorKind { + + fn into(self) -> HookError { + HookError::new(HookErrorKind::Pre(self), None) + } + +} + +impl Into for (PreHookErrorKind, Box) { + + fn into(self) -> HookError { + HookError::new(HookErrorKind::Pre(self.0), Some(self.1)) + } + +} + +impl Into for PostHookErrorKind { + + fn into(self) -> HookError { + HookError::new(HookErrorKind::Post(self), None) + } + +} + +impl Into for (PostHookErrorKind, Box) { + + fn into(self) -> HookError { + HookError::new(HookErrorKind::Post(self.0), Some(self.1)) + } + +} + +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>, +} + +impl HookError { + + /** + * Build a new HookError from an HookErrorKind, optionally with cause + */ + pub fn new(errtype: HookErrorKind, cause: Option>) + -> 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) + } + +} + + diff --git a/libimagstore/src/hook/mod.rs b/libimagstore/src/hook/mod.rs index 6999e012..415f4ec4 100644 --- a/libimagstore/src/hook/mod.rs +++ b/libimagstore/src/hook/mod.rs @@ -5,267 +5,16 @@ use toml::Value; use self::error::HookError; use store::FileLockEntry; -pub type HookResult = Result; +pub mod accessor; +pub mod create; +pub mod delete; +pub mod error; +pub mod read; +pub mod result; +pub mod retrieve; +pub mod update; -pub trait Configureable { +pub trait Hook : Debug + Send + Sync { fn set_config(&mut self, cfg: Value); } -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), - NonMutableAccess(Box), -} - -pub trait HookDataAccessorProvider { - fn accessor(&self) -> Box; -} - -pub trait Hook : Configureable + Debug + Send + Sync { -} - -pub mod read { - use storeid::StoreId; - use store::FileLockEntry; - use super::HookDataAccessorProvider; - use super::HookResult; - use super::Hook; - - pub trait PreReadHook : Hook { - fn pre_read(&self, &StoreId) -> HookResult<()>; - } - - pub trait PostReadHook : Hook + HookDataAccessorProvider { - } - -} - -pub mod create { - use storeid::StoreId; - use store::FileLockEntry; - use super::HookDataAccessorProvider; - use super::HookResult; - use super::Hook; - - pub trait PreCreateHook : Hook { - fn pre_create(&self, &StoreId) -> HookResult<()>; - } - - pub trait PostCreateHook : Hook + HookDataAccessorProvider { - } - -} - -pub mod retrieve { - use storeid::StoreId; - use store::FileLockEntry; - use super::HookDataAccessorProvider; - use super::HookResult; - use super::Hook; - - pub trait PreRetrieveHook : Hook { - fn pre_retrieve(&self, &StoreId) -> HookResult<()>; - } - - pub trait PostRetrieveHook : Hook + HookDataAccessorProvider { - } -} - -pub mod update { - use store::FileLockEntry; - use super::HookDataAccessorProvider; - use super::HookResult; - use super::Hook; - - pub trait PreUpdateHook : Hook { - fn pre_update(&self, &FileLockEntry) -> HookResult<()>; - } - - pub trait PostUpdateHook : Hook + HookDataAccessorProvider { - } -} - -pub mod delete { - use storeid::StoreId; - use store::FileLockEntry; - use super::HookDataAccessorProvider; - use super::HookResult; - use super::Hook; - - pub trait PreDeleteHook : Hook { - fn pre_delete(&self, &StoreId) -> HookResult<()>; - } - - pub trait PostDeleteHook : Hook { - fn post_delete(&self, &StoreId) -> HookResult<()>; - } -} - -pub mod error { - use std::error::Error; - use std::fmt::Error as FmtError; - use std::clone::Clone; - use std::fmt::{Display, Formatter}; - use std::convert::Into; - - /** - * Kind of error - */ - #[derive(Clone, Copy, Debug)] - pub enum HookErrorKind { - Pre(PreHookErrorKind), - Post(PostHookErrorKind) - } - - #[derive(Clone, Copy, Debug, Eq, PartialEq)] - pub enum PreHookErrorKind { - // ... - } - - impl Into for PreHookErrorKind { - fn into(self) -> HookErrorKind { - HookErrorKind::Pre(self) - } - } - - #[derive(Clone, Copy, Debug, Eq, PartialEq)] - pub enum PostHookErrorKind { - // ... - } - - impl Into for PostHookErrorKind { - fn into(self) -> HookErrorKind { - HookErrorKind::Post(self) - } - } - - pub trait IntoHookError { - fn into_hookerror(self) -> HookError; - fn into_hookerror_with_cause(self, cause: Box) -> HookError; - } - - impl Into for HookErrorKind { - - fn into(self) -> HookError { - HookError::new(self, None) - } - - } - - impl Into for (HookErrorKind, Box) { - - fn into(self) -> HookError { - HookError::new(self.0, Some(self.1)) - } - - } - - impl Into for PreHookErrorKind { - - fn into(self) -> HookError { - HookError::new(HookErrorKind::Pre(self), None) - } - - } - - impl Into for (PreHookErrorKind, Box) { - - fn into(self) -> HookError { - HookError::new(HookErrorKind::Pre(self.0), Some(self.1)) - } - - } - - impl Into for PostHookErrorKind { - - fn into(self) -> HookError { - HookError::new(HookErrorKind::Post(self), None) - } - - } - - impl Into for (PostHookErrorKind, Box) { - - fn into(self) -> HookError { - HookError::new(HookErrorKind::Post(self.0), Some(self.1)) - } - - } - - 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>, - } - - impl HookError { - - /** - * Build a new HookError from an HookErrorKind, optionally with cause - */ - pub fn new(errtype: HookErrorKind, cause: Option>) - -> 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) - } - - } - -} - diff --git a/libimagstore/src/hook/read.rs b/libimagstore/src/hook/read.rs new file mode 100644 index 00000000..f7a338c5 --- /dev/null +++ b/libimagstore/src/hook/read.rs @@ -0,0 +1,13 @@ +use storeid::StoreId; +use store::FileLockEntry; +use hook::accessor::HookDataAccessorProvider; +use hook::result::HookResult; +use hook::Hook; + +pub trait PreReadHook : Hook { + fn pre_read(&self, &StoreId) -> HookResult<()>; +} + +pub trait PostReadHook : Hook + HookDataAccessorProvider { +} + diff --git a/libimagstore/src/hook/result.rs b/libimagstore/src/hook/result.rs new file mode 100644 index 00000000..11766624 --- /dev/null +++ b/libimagstore/src/hook/result.rs @@ -0,0 +1,3 @@ +use hook::error::HookError; + +pub type HookResult = Result; diff --git a/libimagstore/src/hook/retrieve.rs b/libimagstore/src/hook/retrieve.rs new file mode 100644 index 00000000..db238ff0 --- /dev/null +++ b/libimagstore/src/hook/retrieve.rs @@ -0,0 +1,13 @@ +use storeid::StoreId; +use store::FileLockEntry; +use hook::accessor::HookDataAccessorProvider; +use hook::result::HookResult; +use hook::Hook; + +pub trait PreRetrieveHook : Hook { + fn pre_retrieve(&self, &StoreId) -> HookResult<()>; +} + +pub trait PostRetrieveHook : Hook + HookDataAccessorProvider { +} + diff --git a/libimagstore/src/hook/update.rs b/libimagstore/src/hook/update.rs new file mode 100644 index 00000000..21687222 --- /dev/null +++ b/libimagstore/src/hook/update.rs @@ -0,0 +1,12 @@ +use store::FileLockEntry; +use hook::accessor::HookDataAccessorProvider; +use hook::result::HookResult; +use hook::Hook; + +pub trait PreUpdateHook : Hook { + fn pre_update(&self, &FileLockEntry) -> HookResult<()>; +} + +pub trait PostUpdateHook : Hook + HookDataAccessorProvider { +} + diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index ceaa3172..3090ad6a 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -23,8 +23,8 @@ use error::{StoreError, StoreErrorKind}; use storeid::{StoreId, StoreIdIterator}; use lazyfile::LazyFile; -use hook::HookResult; -use hook::{ MutableHookDataAccessor, +use hook::result::HookResult; +use hook::accessor::{ MutableHookDataAccessor, NonMutableHookDataAccessor, HookDataAccessor, HookDataAccessorProvider}; @@ -458,7 +458,7 @@ impl Store { use std::thread; use std::thread::JoinHandle; - use hook::HookDataAccessor as HDA; + use hook::accessor::HookDataAccessor as HDA; use error::StoreError as SE; use error::StoreErrorKind as SEK;