use toml::Value;
use libimagstore::hook::Hook;
use libimagstore::hook::error::HookErrorKind as HEK;
use libimagstore::hook::accessor::HookDataAccessor as HDA;
use libimagstore::hook::accessor::HookDataAccessorProvider;
use libimagstore::hook::accessor::NonMutableHookDataAccessor;
use libimagstore::hook::result::HookResult;
use libimagstore::store::FileLockEntry;
use libimagentrylink::internal::InternalLinker;
mod error {
generate_error_imports!();
generate_error_types!(NoLinksLeftCheckerHookError, NoLinksLeftCheckerHookErrorKind,
ReadInternalLinksError => "Error while reading internal links of entry",
LinksLeft => "The entry has links and therefor cannot be deleted."
);
}
use self::error::NoLinksLeftCheckerHookErrorKind as NLLCHEK;
#[derive(Debug, Clone)]
pub struct DenyDeletionOfLinkedEntriesHook {
abort: bool
}
impl DenyDeletionOfLinkedEntriesHook {
pub fn new() -> DenyDeletionOfLinkedEntriesHook {
DenyDeletionOfLinkedEntriesHook {
abort: true }
}
}
impl Hook for DenyDeletionOfLinkedEntriesHook {
fn name(&self) -> &'static str {
"stdhook_linked_entries_cannot_be_deleted"
}
fn set_config(&mut self, v: &Value) {
self.abort = match v.lookup("aborting") {
Some(&Value::Boolean(b)) => b,
Some(_) => {
warn!("Configuration error, 'aborting' must be a Boolean (true|false).");
warn!("Assuming 'true' now.");
true
},
None => {
warn!("No key 'aborting' - Assuming 'true'");
true
},
};
}
}
impl HookDataAccessorProvider for DenyDeletionOfLinkedEntriesHook {
fn accessor(&self) -> HDA {
HDA::NonMutableAccess(self)
}
}
impl NonMutableHookDataAccessor for DenyDeletionOfLinkedEntriesHook {
fn access(&self, fle: &FileLockEntry) -> HookResult<()> {
use libimagerror::into::IntoError;
use self::error::MapErrInto;
debug!("[NO LINKS LEFT CHECKER HOOK] {:?}", fle.get_location());
let n = try!(fle
.get_internal_links()
.map(|i| i.count())
.map_err_into(NLLCHEK::ReadInternalLinksError)
.map_err(Box::new)
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e)));
if n > 0 {
Err(NLLCHEK::LinksLeft.into_error())
.map_err(Box::new)
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e))
} else {
Ok(())
}
}
}