use failure::Fallible as Result;
use failure::ResultExt;
use failure::Error;
use toml::Value;
use toml_query::read::TomlValueReadTypeExt;
use toml_query::insert::TomlValueInsertExt;
use toml_query::delete::TomlValueDeleteExt;
pub trait Is {
fn is<T: IsKindHeaderPathProvider>(&self) -> Result<bool>;
fn set_isflag<T: IsKindHeaderPathProvider>(&mut self) -> Result<()>;
fn remove_isflag<T: IsKindHeaderPathProvider>(&mut self) -> Result<()>;
}
impl Is for ::libimagstore::store::Entry {
fn is<T: IsKindHeaderPathProvider>(&self) -> Result<bool> {
let field = T::kindflag_header_location();
match self
.get_header()
.read_bool(field)
.context(format_err!("Failed reading header '{}' in '{}'", field, self.get_location()))
.map_err(Error::from)?
{
Some(b) => Ok(b),
None => Ok(false),
}
}
fn set_isflag<T: IsKindHeaderPathProvider>(&mut self) -> Result<()> {
self.get_header_mut()
.insert(T::kindflag_header_location(), Value::Boolean(true))
.context(format_err!("Failed inserting header '{}' in '{}'", T::kindflag_header_location(), self.get_location()))
.map_err(Error::from)
.map(|_| ())
}
fn remove_isflag<T: IsKindHeaderPathProvider>(&mut self) -> Result<()> {
trace!("Trying to remove: {}", T::kindflag_header_location());
self.get_header_mut()
.delete(T::kindflag_header_location())
.context(format_err!("Failed deleting header '{}' in '{}'", T::kindflag_header_location(), self.get_location()))
.map_err(Error::from)
.map(|_| ())
}
}
pub trait IsKindHeaderPathProvider {
fn kindflag_header_location() -> &'static str;
}
#[macro_export]
macro_rules! provide_kindflag_path {
(pub $entry_header_path_provider_type:ident, $path:expr) => {
pub struct $entry_header_path_provider_type;
provide_kindflag_path!(impl for $entry_header_path_provider_type, $path);
};
($entry_header_path_provider_type:ident, $path:expr) => {
struct $entry_header_path_provider_type;
provide_kindflag_path!(impl for $entry_header_path_provider_type, $path);
};
(impl for $entry_header_path_provider_type:ident, $path:expr) => {
impl IsKindHeaderPathProvider for $entry_header_path_provider_type {
fn kindflag_header_location() -> &'static str {
$path
}
}
};
}