Add functionality to remove categories

This commit is contained in:
Matthias Beyer 2018-05-04 11:02:46 +02:00
parent 9c36fc8ac0
commit 2e2bce77a0
2 changed files with 35 additions and 0 deletions

View file

@ -41,6 +41,8 @@ pub trait EntryCategory {
fn has_category(&self) -> Result<bool>;
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(|_| ())
}
}

View file

@ -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)
}