From c3088faa4c31ae64aaddeb83e805200be8935a32 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 May 2017 19:21:44 +0200 Subject: [PATCH] Add "checked" variant of set_category() --- libimagentrycategory/src/category.rs | 16 ++++++++++++++++ libimagentrycategory/src/error.rs | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/libimagentrycategory/src/category.rs b/libimagentrycategory/src/category.rs index b47282a0..89763913 100644 --- a/libimagentrycategory/src/category.rs +++ b/libimagentrycategory/src/category.rs @@ -28,6 +28,7 @@ use libimagerror::into::IntoError; use error::CategoryErrorKind as CEK; use error::MapErrInto; use result::Result; +use register::CategoryRegister; #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub struct Category(String); @@ -49,6 +50,9 @@ impl Into for Category { pub trait EntryCategory { fn set_category(&mut self, s: Category) -> Result<()>; + + fn set_category_checked(&mut self, register: &CategoryRegister, s: Category) -> Result<()>; + fn get_category(&self) -> Result>; fn has_category(&self) -> Result; @@ -64,6 +68,18 @@ impl EntryCategory for Entry { .map(|_| ()) } + /// Check whether a category exists before setting it. + /// + /// This function should be used by default over EntryCategory::set_category()! + fn set_category_checked(&mut self, register: &CategoryRegister, s: Category) -> Result<()> { + register.category_exists(&s.0) + .and_then(|bl| if bl { + self.set_category(s) + } else { + Err(CEK::CategoryDoesNotExist.into_error()) + }) + } + fn get_category(&self) -> Result> { match self.get_header().read(&String::from("category.value")) { Err(res) => match res.kind() { diff --git a/libimagentrycategory/src/error.rs b/libimagentrycategory/src/error.rs index 64aebc18..72824337 100644 --- a/libimagentrycategory/src/error.rs +++ b/libimagentrycategory/src/error.rs @@ -24,7 +24,9 @@ generate_error_module!( StoreIdHandlingError => "StoreId handling error", HeaderReadError => "Header read error", HeaderWriteError => "Header write error", - TypeError => "Found wrong type in header" + TypeError => "Found wrong type in header", + + CategoryDoesNotExist => "Category does not exist" ); );