2017-05-21 13:03:26 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
|
|
|
use toml_query::read::TomlValueReadExt;
|
2018-01-12 15:32:18 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2017-05-21 13:03:26 +00:00
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
use libimagstore::store::Entry;
|
|
|
|
|
|
|
|
use error::CategoryErrorKind as CEK;
|
2017-09-03 19:28:36 +00:00
|
|
|
use error::CategoryError as CE;
|
2017-09-03 13:55:20 +00:00
|
|
|
use error::ResultExt;
|
2017-09-03 19:39:32 +00:00
|
|
|
use error::Result;
|
2017-05-31 17:21:44 +00:00
|
|
|
use register::CategoryRegister;
|
2017-05-21 13:03:26 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
|
|
|
|
pub struct Category(String);
|
|
|
|
|
|
|
|
impl From<String> for Category {
|
|
|
|
|
|
|
|
fn from(s: String) -> Category {
|
|
|
|
Category(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<String> for Category {
|
|
|
|
fn into(self) -> String {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait EntryCategory {
|
|
|
|
|
|
|
|
fn set_category(&mut self, s: Category) -> Result<()>;
|
2017-05-31 17:21:44 +00:00
|
|
|
|
|
|
|
fn set_category_checked(&mut self, register: &CategoryRegister, s: Category) -> Result<()>;
|
|
|
|
|
2017-05-21 13:03:26 +00:00
|
|
|
fn get_category(&self) -> Result<Option<Category>>;
|
|
|
|
|
|
|
|
fn has_category(&self) -> Result<bool>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EntryCategory for Entry {
|
|
|
|
|
|
|
|
fn set_category(&mut self, s: Category) -> Result<()> {
|
|
|
|
self.get_header_mut()
|
|
|
|
.insert(&String::from("category.value"), Value::String(s.into()))
|
2017-09-03 13:55:20 +00:00
|
|
|
.chain_err(|| CEK::HeaderWriteError)
|
2017-05-21 13:03:26 +00:00
|
|
|
.map(|_| ())
|
|
|
|
}
|
2017-05-31 17:21:44 +00:00
|
|
|
|
|
|
|
/// 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 {
|
2017-09-03 19:28:36 +00:00
|
|
|
Err(CE::from_kind(CEK::CategoryDoesNotExist))
|
2017-05-31 17:21:44 +00:00
|
|
|
})
|
|
|
|
}
|
2017-05-21 13:03:26 +00:00
|
|
|
|
|
|
|
fn get_category(&self) -> Result<Option<Category>> {
|
2018-01-04 22:09:30 +00:00
|
|
|
self.get_header()
|
2018-01-12 15:32:18 +00:00
|
|
|
.read_string("category.value")
|
2018-01-04 22:09:30 +00:00
|
|
|
.chain_err(|| CEK::HeaderReadError)
|
2018-01-12 15:32:18 +00:00
|
|
|
.and_then(|o| o.map(Category::from).ok_or(CE::from_kind(CEK::TypeError)))
|
|
|
|
.map(Some)
|
2017-05-21 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn has_category(&self) -> Result<bool> {
|
2018-01-12 15:32:18 +00:00
|
|
|
self.get_header().read("category.value")
|
2017-09-03 13:55:20 +00:00
|
|
|
.chain_err(|| CEK::HeaderReadError)
|
2018-01-12 15:32:18 +00:00
|
|
|
.map(|x| x.is_some())
|
2017-05-21 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|