Rewrite CategoryRegister as CategoryStore

This commit is contained in:
Matthias Beyer 2018-04-26 11:29:23 +02:00
parent 5f305ef7a7
commit ab15d89e64
2 changed files with 12 additions and 28 deletions

View file

@ -50,7 +50,7 @@ extern crate libimagstore;
pub mod category; pub mod category;
pub mod entry; pub mod entry;
pub mod error; pub mod error;
pub mod register; pub mod store;
module_entry_path_mod!("category"); module_entry_path_mod!("category");

View file

@ -39,7 +39,7 @@ pub const CATEGORY_REGISTER_NAME_FIELD_PATH : &'static str = "category.register.
/// Extension on the Store to make it a register for categories /// Extension on the Store to make it a register for categories
/// ///
/// The register writes files to the /// The register writes files to the
pub trait CategoryRegister { pub trait CategoryStore {
fn category_exists(&self, name: &str) -> Result<bool>; fn category_exists(&self, name: &str) -> Result<bool>;
@ -53,7 +53,7 @@ pub trait CategoryRegister {
} }
impl CategoryRegister for Store { impl CategoryStore for Store {
/// Check whether a category exists /// Check whether a category exists
fn category_exists(&self, name: &str) -> Result<bool> { fn category_exists(&self, name: &str) -> Result<bool> {
@ -64,39 +64,23 @@ impl CategoryRegister for Store {
/// Create a category /// Create a category
/// ///
/// Fails if the category already exists (returns false then) /// Fails if the category already exists (returns false then)
fn create_category(&self, name: &str) -> Result<bool> { fn create_category(&self, name: &str) -> Result<FileLockEntry<'a>> {
use libimagstore::error::StoreErrorKind as SEK; let sid = mk_category_storeid(self.path().clone(), name)?;
let mut entry = self.create(sid)?;
let sid = mk_category_storeid(self.path().clone(), name)?; entry.set_isflag::<IsCategory>()?;
let _ = entry
.get_header_mut()
.insert(CATEGORY_REGISTER_NAME_FIELD_PATH, Value::String(String::from(name)))?;
match self.create(sid) { Ok(entry)
Ok(mut entry) => {
let val = Value::String(String::from(name));
entry.get_header_mut()
.insert(CATEGORY_REGISTER_NAME_FIELD_PATH, val)
.map(|opt| if opt.is_none() {
debug!("Setting category header worked")
} else {
warn!("Setting category header replaced existing value: {:?}", opt);
})
.map(|_| true)
.chain_err(|| CEK::HeaderWriteError)
.chain_err(|| CEK::StoreWriteError)
}
Err(store_error) => if is_match!(store_error.kind(), &SEK::EntryAlreadyExists(_)) {
Ok(false)
} else {
Err(store_error).chain_err(|| CEK::StoreWriteError)
}
}
} }
/// Delete a category /// Delete a category
fn delete_category(&self, name: &str) -> Result<()> { fn delete_category(&self, name: &str) -> Result<()> {
let sid = mk_category_storeid(self.path().clone(), name)?; let sid = mk_category_storeid(self.path().clone(), name)?;
self.delete(sid).map_err(CE::from)
self.delete(sid).chain_err(|| CEK::StoreWriteError)
} }
/// Get all category names /// Get all category names