2017-05-21 13:03:26 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-21 13:03:26 +00:00
|
|
|
//
|
|
|
|
// 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;
|
2018-04-26 10:04:18 +00:00
|
|
|
use libimagentrylink::internal::InternalLinker;
|
2018-10-30 17:40:51 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2017-05-21 13:03:26 +00:00
|
|
|
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2018-04-26 10:04:18 +00:00
|
|
|
use store::CategoryStore;
|
2017-05-21 13:03:26 +00:00
|
|
|
|
|
|
|
pub trait EntryCategory {
|
|
|
|
|
2018-04-26 10:04:18 +00:00
|
|
|
fn set_category(&mut self, s: &str) -> Result<()>;
|
2017-05-31 17:21:44 +00:00
|
|
|
|
2018-04-26 10:04:18 +00:00
|
|
|
fn set_category_checked(&mut self, register: &CategoryStore, s: &str) -> Result<()>;
|
2017-05-31 17:21:44 +00:00
|
|
|
|
2018-04-26 10:04:18 +00:00
|
|
|
fn get_category(&self) -> Result<String>;
|
2017-05-21 13:03:26 +00:00
|
|
|
|
|
|
|
fn has_category(&self) -> Result<bool>;
|
|
|
|
|
2018-05-04 09:02:46 +00:00
|
|
|
fn remove_category(&mut self) -> Result<()>;
|
|
|
|
|
2017-05-21 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EntryCategory for Entry {
|
|
|
|
|
2018-04-26 10:04:18 +00:00
|
|
|
fn set_category(&mut self, s: &str) -> Result<()> {
|
|
|
|
trace!("Setting category '{}' UNCHECKED", s);
|
2017-05-21 13:03:26 +00:00
|
|
|
self.get_header_mut()
|
2018-04-26 10:04:18 +00:00
|
|
|
.insert(&String::from("category.value"), Value::String(s.to_string()))
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderWriteError)
|
|
|
|
.map_err(Error::from)
|
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()!
|
2018-04-26 10:04:18 +00:00
|
|
|
fn set_category_checked(&mut self, register: &CategoryStore, s: &str) -> Result<()> {
|
|
|
|
trace!("Setting category '{}' checked", s);
|
2018-04-26 09:20:09 +00:00
|
|
|
let mut category = register
|
2018-04-26 10:04:18 +00:00
|
|
|
.get_category_by_name(s)?
|
2018-10-30 17:40:51 +00:00
|
|
|
.ok_or_else(|| Error::from(err_msg("Category does not exist")))?;
|
2018-04-26 09:20:09 +00:00
|
|
|
|
|
|
|
let _ = self.set_category(s)?;
|
|
|
|
let _ = self.add_internal_link(&mut category)?;
|
|
|
|
|
|
|
|
Ok(())
|
2017-05-31 17:21:44 +00:00
|
|
|
}
|
2017-05-21 13:03:26 +00:00
|
|
|
|
2018-04-26 10:04:18 +00:00
|
|
|
fn get_category(&self) -> Result<String> {
|
|
|
|
trace!("Getting category from '{}'", self.get_location());
|
2018-01-04 22:09:30 +00:00
|
|
|
self.get_header()
|
2018-04-26 10:04:18 +00:00
|
|
|
.read_string("category.value")?
|
2018-10-30 17:40:51 +00:00
|
|
|
.ok_or_else(|| Error::from(err_msg("Category name missing")))
|
2017-05-21 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn has_category(&self) -> Result<bool> {
|
2018-04-26 10:04:18 +00:00
|
|
|
trace!("Has category? '{}'", self.get_location());
|
2018-10-30 17:40:51 +00:00
|
|
|
self.get_header()
|
|
|
|
.read("category.value")
|
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from)
|
2018-01-12 15:32:18 +00:00
|
|
|
.map(|x| x.is_some())
|
2017-05-21 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 09:02:46 +00:00
|
|
|
/// 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")
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.context(EM::EntryHeaderWriteError)
|
|
|
|
.map_err(Error::from)
|
2018-05-04 09:02:46 +00:00
|
|
|
.map(|_| ())
|
|
|
|
}
|
|
|
|
|
2017-05-21 13:03:26 +00:00
|
|
|
}
|