2017-02-23 13:11:32 +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 libimagstore::store::Entry;
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagentrylink::internal::InternalLinker;
|
2017-09-15 10:52:03 +00:00
|
|
|
use libimagnotes::notestore::NoteStore;
|
|
|
|
use libimagnotes::iter::NoteIterator;
|
2017-02-23 13:11:32 +00:00
|
|
|
use libimagstore::storeid::StoreIdIterator;
|
|
|
|
|
2017-09-03 19:39:32 +00:00
|
|
|
use error::Result;
|
2017-02-23 13:11:32 +00:00
|
|
|
use error::AnnotationErrorKind as AEK;
|
2017-09-03 13:50:54 +00:00
|
|
|
use error::ResultExt;
|
2017-02-23 13:11:32 +00:00
|
|
|
|
|
|
|
use self::iter::*;
|
|
|
|
|
|
|
|
pub trait AnnotationFetcher<'a> {
|
|
|
|
|
|
|
|
fn all_annotations(&'a self) -> Result<AnnotationIter<'a>>;
|
|
|
|
|
|
|
|
fn annotations_for_entry(&'a self, entry: &Entry) -> Result<AnnotationIter<'a>>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AnnotationFetcher<'a> for Store {
|
|
|
|
|
|
|
|
/// Wrapper around `Note::all_notes()` of `libimagnotes` which filters out normal notes and
|
|
|
|
/// leaves only annotations in the iterator.
|
|
|
|
fn all_annotations(&'a self) -> Result<AnnotationIter<'a>> {
|
2017-09-15 10:52:03 +00:00
|
|
|
NoteStore::all_notes(self)
|
|
|
|
.map(|iter| AnnotationIter::new(iter, self))
|
2017-09-03 13:50:54 +00:00
|
|
|
.chain_err(|| AEK::StoreReadError)
|
2017-02-23 13:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get all annotations (in an iterator) for an entry
|
|
|
|
///
|
|
|
|
/// Internally, this fetches the links of the entry, fetches all the entries behind the links
|
|
|
|
/// and filters them for annotations.
|
|
|
|
///
|
|
|
|
/// This might result in some heavy IO operations if a lot of stuff is linked to a single
|
|
|
|
/// entry, but should normally be not that heavy.
|
|
|
|
fn annotations_for_entry(&'a self, entry: &Entry) -> Result<AnnotationIter<'a>> {
|
|
|
|
entry.get_internal_links()
|
2017-09-03 13:50:54 +00:00
|
|
|
.chain_err(|| AEK::StoreReadError)
|
2017-04-21 15:54:03 +00:00
|
|
|
.map(|iter| StoreIdIterator::new(Box::new(iter.map(|e| e.get_store_id().clone()))))
|
2017-09-15 10:52:03 +00:00
|
|
|
.map(NoteIterator::new)
|
|
|
|
.map(|i| AnnotationIter::new(i, self))
|
2017-02-23 13:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod iter {
|
|
|
|
use toml::Value;
|
|
|
|
|
2017-08-26 15:53:08 +00:00
|
|
|
use toml_query::read::TomlValueReadExt;
|
|
|
|
|
2017-09-15 10:52:03 +00:00
|
|
|
use libimagnotes::iter::NoteIterator;
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2017-02-23 13:11:32 +00:00
|
|
|
|
2017-09-03 19:39:32 +00:00
|
|
|
use error::Result;
|
2017-02-23 13:11:32 +00:00
|
|
|
use error::AnnotationErrorKind as AEK;
|
2017-09-03 19:28:36 +00:00
|
|
|
use error::AnnotationError as AE;
|
2017-09-03 13:50:54 +00:00
|
|
|
use error::ResultExt;
|
2017-02-23 13:11:32 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2017-09-15 10:52:03 +00:00
|
|
|
pub struct AnnotationIter<'a>(NoteIterator, &'a Store);
|
2017-02-23 13:11:32 +00:00
|
|
|
|
|
|
|
impl<'a> AnnotationIter<'a> {
|
|
|
|
|
2017-09-15 10:52:03 +00:00
|
|
|
pub fn new(noteiter: NoteIterator, store: &'a Store) -> AnnotationIter<'a> {
|
|
|
|
AnnotationIter(noteiter, store)
|
2017-02-23 13:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for AnnotationIter<'a> {
|
2017-09-15 10:52:03 +00:00
|
|
|
type Item = Result<FileLockEntry<'a>>;
|
2017-02-23 13:11:32 +00:00
|
|
|
|
2017-09-15 10:52:03 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2017-02-23 13:11:32 +00:00
|
|
|
loop {
|
2017-09-15 10:52:03 +00:00
|
|
|
match self.0.next().map(|id| self.1.get(id)) {
|
|
|
|
Some(Ok(Some(note))) => {
|
2017-08-26 15:53:08 +00:00
|
|
|
match note.get_header().read("annotation.is_annotation") {
|
2017-02-23 13:11:32 +00:00
|
|
|
Ok(None) => continue, // not an annotation
|
2017-08-26 15:53:08 +00:00
|
|
|
Ok(Some(&Value::Boolean(true))) => return Some(Ok(note)),
|
2017-09-03 19:28:36 +00:00
|
|
|
Ok(Some(_)) => return Some(Err(AE::from_kind(AEK::HeaderTypeError))),
|
2017-09-03 13:50:54 +00:00
|
|
|
Err(e) => return Some(Err(e).chain_err(|| AEK::HeaderReadError)),
|
2017-02-23 13:11:32 +00:00
|
|
|
}
|
|
|
|
},
|
2017-09-15 10:52:03 +00:00
|
|
|
Some(Ok(None)) => continue,
|
|
|
|
Some(Err(e)) => return Some(Err(e).chain_err(|| AEK::StoreReadError)),
|
|
|
|
None => return None, // iterator consumed
|
2017-02-23 13:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|