2018-04-26 09:29:09 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2018-04-26 09:29:09 +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 libimagentryutil::isa::Is;
|
|
|
|
use libimagentryutil::isa::IsKindHeaderPathProvider;
|
2018-04-26 10:04:18 +00:00
|
|
|
use libimagstore::store::Entry;
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::storeid::StoreIdIterator;
|
2019-06-21 19:38:50 +00:00
|
|
|
use libimagentrylink::linkable::Linkable;
|
2018-04-26 09:29:09 +00:00
|
|
|
|
2018-04-26 10:04:18 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
|
|
|
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Fallible as Result;
|
2019-05-17 22:14:34 +00:00
|
|
|
use failure::ResultExt;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::store::CATEGORY_REGISTER_NAME_FIELD_PATH;
|
|
|
|
use crate::iter::CategoryEntryIterator;
|
2018-04-26 09:29:09 +00:00
|
|
|
|
|
|
|
provide_kindflag_path!(pub IsCategory, "category.is_category");
|
|
|
|
|
|
|
|
pub trait Category {
|
2018-04-26 10:04:18 +00:00
|
|
|
fn is_category(&self) -> Result<bool>;
|
|
|
|
fn get_name(&self) -> Result<String>;
|
|
|
|
fn get_entries<'a>(&self, store: &'a Store) -> Result<CategoryEntryIterator<'a>>;
|
2018-04-26 09:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Category for Entry {
|
|
|
|
fn is_category(&self) -> Result<bool> {
|
2018-10-30 17:40:51 +00:00
|
|
|
self.is::<IsCategory>()
|
2018-04-26 09:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_name(&self) -> Result<String> {
|
2018-04-26 10:04:18 +00:00
|
|
|
trace!("Getting category name of '{:?}'", self.get_location());
|
|
|
|
self.get_header()
|
|
|
|
.read_string(CATEGORY_REGISTER_NAME_FIELD_PATH)
|
2019-05-17 22:14:34 +00:00
|
|
|
.context(format_err!("Failed to read header at '{}'", CATEGORY_REGISTER_NAME_FIELD_PATH))
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)?
|
2019-08-27 08:48:33 +00:00
|
|
|
.ok_or_else(|| err_msg("Category name missing"))
|
2018-04-26 09:29:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 10:04:18 +00:00
|
|
|
fn get_entries<'a>(&self, store: &'a Store) -> Result<CategoryEntryIterator<'a>> {
|
|
|
|
trace!("Getting linked entries for category '{:?}'", self.get_location());
|
2019-06-21 19:33:22 +00:00
|
|
|
let sit = self.links()?.map(|l| l.get_store_id().clone()).map(Ok);
|
2018-04-26 10:04:18 +00:00
|
|
|
let sit = StoreIdIterator::new(Box::new(sit));
|
|
|
|
let name = self.get_name()?;
|
|
|
|
Ok(CategoryEntryIterator::new(store, sit, name))
|
2018-04-26 09:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|