diff --git a/lib/entry/libimagentrycategory/src/entry.rs b/lib/entry/libimagentrycategory/src/entry.rs index c6f82e04..624b5fa1 100644 --- a/lib/entry/libimagentrycategory/src/entry.rs +++ b/lib/entry/libimagentrycategory/src/entry.rs @@ -41,6 +41,8 @@ pub trait EntryCategory { fn has_category(&self) -> Result; + fn remove_category(&mut self) -> Result<()>; + } impl EntryCategory for Entry { @@ -82,4 +84,20 @@ impl EntryCategory for Entry { .map(|x| x.is_some()) } + /// Remove the category setting + /// + /// # Warning + /// + /// This does _only_ remove the category setting in the header. This does _not_ remove the + /// internal link to the category entry, nor does it remove the category from the store. + fn remove_category(&mut self) -> Result<()> { + use toml_query::delete::TomlValueDeleteExt; + + self.get_header_mut() + .delete("category.value") + .chain_err(|| CEK::HeaderWriteError) + .map(|_| ()) + } + + } diff --git a/lib/entry/libimagentrycategory/src/store.rs b/lib/entry/libimagentrycategory/src/store.rs index 8179010d..1058c7bb 100644 --- a/lib/entry/libimagentrycategory/src/store.rs +++ b/lib/entry/libimagentrycategory/src/store.rs @@ -82,9 +82,26 @@ impl CategoryStore for Store { } /// Delete a category + /// + /// Automatically removes all category settings from entries which are linked to this category. fn delete_category(&self, name: &str) -> Result<()> { + use libimagentrylink::internal::InternalLinker; + use category::Category; + trace!("Deleting category: '{}'", name); let sid = mk_category_storeid(self.path().clone(), name)?; + + { + let mut category = self.get(sid.clone())? + .ok_or_else(|| CEK::CategoryDoesNotExist) + .map_err(CE::from_kind)?; + + for entry in category.get_entries(self)? { + let mut entry = entry?; + let _ = category.remove_internal_link(&mut entry)?; + } + } + self.delete(sid).map_err(CE::from) }