1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
use libimagstore::store::Entry;
use libimagstore::store::Store;
use libimagstore::storeid::StoreIdIterator;
use libimagentrylink::internal::InternalLinker;
use toml_query::read::TomlValueReadTypeExt;
use failure::Fallible as Result;
use failure::Error;
use failure::err_msg;
use store::CATEGORY_REGISTER_NAME_FIELD_PATH;
use iter::CategoryEntryIterator;
provide_kindflag_path!(pub IsCategory, "category.is_category");
pub trait Category {
fn is_category(&self) -> Result<bool>;
fn get_name(&self) -> Result<String>;
fn get_entries<'a>(&self, store: &'a Store) -> Result<CategoryEntryIterator<'a>>;
}
impl Category for Entry {
fn is_category(&self) -> Result<bool> {
self.is::<IsCategory>()
}
fn get_name(&self) -> Result<String> {
trace!("Getting category name of '{:?}'", self.get_location());
self.get_header()
.read_string(CATEGORY_REGISTER_NAME_FIELD_PATH)
.map_err(Error::from)?
.ok_or_else(|| Error::from(err_msg("Category name missing")))
}
fn get_entries<'a>(&self, store: &'a Store) -> Result<CategoryEntryIterator<'a>> {
trace!("Getting linked entries for category '{:?}'", self.get_location());
let sit = self.get_internal_links()?.map(|l| l.get_store_id().clone()).map(Ok);
let sit = StoreIdIterator::new(Box::new(sit));
let name = self.get_name()?;
Ok(CategoryEntryIterator::new(store, sit, name))
}
}