Rewrite API

This commit is contained in:
Matthias Beyer 2017-10-12 18:23:52 +02:00
parent ec855fefc7
commit 7978e71bde
5 changed files with 120 additions and 89 deletions

View File

@ -17,13 +17,13 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::path::PathBuf;
use toml::Value; use toml::Value;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator;
use libimagentrylink::internal::InternalLinker; use libimagentrylink::internal::InternalLinker;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
@ -34,29 +34,29 @@ use error::AnnotationErrorKind as AEK;
use error::AnnotationError as AE; use error::AnnotationError as AE;
use error::ResultExt; use error::ResultExt;
use iter::*;
pub trait Annotateable { pub trait Annotateable {
/// Add an annotation to `Self`, that is a `FileLockEntry` which is linked to `Self` (link as in
/// libimagentrylink).
///
/// A new annotation also has the field `annotation.is_annotation` set to `true`.
fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>>; fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>>;
fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<Option<FileLockEntry<'a>>>;
/// Check whether an entry is an annotation fn annotations<'a>(&self, store: &'a Store) -> Result<AnnotationIter<'a>>;
fn is_annotation(&self) -> Result<bool>; fn is_annotation(&self) -> Result<bool>;
} }
impl Annotateable for Entry { impl Annotateable for Entry {
/// Annotate an entry, returns the new entry which is used to annotate
fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>> { fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>> {
store.retrieve(PathBuf::from(ann_name)) use module_path::ModuleEntryPath;
.chain_err(|| AEK::StoreWriteError) store.retrieve(try!(ModuleEntryPath::new(ann_name).into_storeid()))
.map_err(From::from)
.and_then(|mut anno| { .and_then(|mut anno| {
anno.get_header_mut() {
.insert("annotation.is_annotation", Value::Boolean(true)) let header = anno.get_header_mut();
.chain_err(|| AEK::HeaderWriteError) try!(header.insert("annotation.is_annotation", Value::Boolean(true)));
.map(|_| anno) try!(header.insert("annotation.name", Value::String(String::from(ann_name))));
}
Ok(anno)
}) })
.and_then(|mut anno| { .and_then(|mut anno| {
anno.add_internal_link(self) anno.add_internal_link(self)
@ -65,10 +65,39 @@ impl Annotateable for Entry {
}) })
} }
/// Checks the current entry for all annotations and removes the one where the name is
/// `ann_name`, which is then returned
fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<Option<FileLockEntry<'a>>> {
for annotation in self.annotations(store)? {
let mut anno = try!(annotation);
let name = match anno.get_header().read("annotation.name")? {
None => continue,
Some(val) => match *val {
Value::String(ref name) => name.clone(),
_ => return Err(AE::from_kind(AEK::HeaderTypeError)),
},
};
if name == ann_name {
let _ = try!(self.remove_internal_link(&mut anno));
}
}
Ok(None)
}
/// Get all annotations of an entry
fn annotations<'a>(&self, store: &'a Store) -> Result<AnnotationIter<'a>> {
self.get_internal_links()
.map_err(From::from)
.map(|iter| StoreIdIterator::new(Box::new(iter.map(|e| e.get_store_id().clone()))))
.map(|i| AnnotationIter::new(i, store))
}
fn is_annotation(&self) -> Result<bool> { fn is_annotation(&self) -> Result<bool> {
self.get_header() self.get_header()
.read("annotation.is_annotation") .read("annotation.is_annotation")
.chain_err(|| AEK::StoreReadError) .map_err(From::from)
.and_then(|res| match res { .and_then(|res| match res {
Some(&Value::Boolean(b)) => Ok(b), Some(&Value::Boolean(b)) => Ok(b),
None => Ok(false), None => Ok(false),

View File

@ -17,22 +17,15 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use libimagstore::store::Entry;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagentrylink::internal::InternalLinker;
use libimagstore::storeid::StoreIdIterator;
use error::Result; use error::Result;
use error::ResultExt; use iter::*;
use self::iter::*;
pub trait AnnotationFetcher<'a> { pub trait AnnotationFetcher<'a> {
fn all_annotations(&'a self) -> Result<AnnotationIter<'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 { impl<'a> AnnotationFetcher<'a> for Store {
@ -43,68 +36,5 @@ impl<'a> AnnotationFetcher<'a> for Store {
.map_err(Into::into) .map_err(Into::into)
} }
/// 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()
.map_err(Into::into)
.map(|iter| StoreIdIterator::new(Box::new(iter.map(|e| e.get_store_id().clone()))))
.map(|i| AnnotationIter::new(i, self))
}
}
pub mod iter {
use toml::Value;
use toml_query::read::TomlValueReadExt;
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator;
use error::Result;
use error::AnnotationErrorKind as AEK;
use error::AnnotationError as AE;
use error::ResultExt;
#[derive(Debug)]
pub struct AnnotationIter<'a>(StoreIdIterator, &'a Store);
impl<'a> AnnotationIter<'a> {
pub fn new(iter: StoreIdIterator, store: &'a Store) -> AnnotationIter<'a> {
AnnotationIter(iter, store)
}
}
impl<'a> Iterator for AnnotationIter<'a> {
type Item = Result<FileLockEntry<'a>>;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.0.next().map(|id| self.1.get(id)) {
Some(Ok(Some(entry))) => {
match entry.get_header().read("annotation.is_annotation") {
Ok(None) => continue, // not an annotation
Ok(Some(&Value::Boolean(true))) => return Some(Ok(entry)),
Ok(Some(_)) => return Some(Err(AE::from_kind(AEK::HeaderTypeError))),
Err(e) => return Some(Err(e).chain_err(|| AEK::HeaderReadError)),
}
},
Some(Ok(None)) => continue,
Some(Err(e)) => return Some(Err(e).chain_err(|| AEK::StoreReadError)),
None => return None, // iterator consumed
}
}
}
}
} }

View File

@ -27,6 +27,10 @@ error_chain! {
LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind); LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind);
} }
foreign_links {
TomlQueryError(::toml_query::error::Error);
}
errors { errors {
StoreReadError { StoreReadError {
description("Store read error") description("Store read error")

View File

@ -0,0 +1,65 @@
//
// 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::Value;
use toml_query::read::TomlValueReadExt;
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator;
use error::Result;
use error::AnnotationErrorKind as AEK;
use error::AnnotationError as AE;
use error::ResultExt;
#[derive(Debug)]
pub struct AnnotationIter<'a>(StoreIdIterator, &'a Store);
impl<'a> AnnotationIter<'a> {
pub fn new(iter: StoreIdIterator, store: &'a Store) -> AnnotationIter<'a> {
AnnotationIter(iter, store)
}
}
impl<'a> Iterator for AnnotationIter<'a> {
type Item = Result<FileLockEntry<'a>>;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.0.next().map(|id| self.1.get(id)) {
Some(Ok(Some(entry))) => {
match entry.get_header().read("annotation.is_annotation") {
Ok(None) => continue, // not an annotation
Ok(Some(&Value::Boolean(true))) => return Some(Ok(entry)),
Ok(Some(_)) => return Some(Err(AE::from_kind(AEK::HeaderTypeError))),
Err(e) => return Some(Err(e).chain_err(|| AEK::HeaderReadError)),
}
},
Some(Ok(None)) => continue,
Some(Err(e)) => return Some(Err(e).chain_err(|| AEK::StoreReadError)),
None => return None, // iterator consumed
}
}
}
}

View File

@ -39,11 +39,14 @@ extern crate toml;
extern crate toml_query; extern crate toml_query;
#[macro_use] extern crate error_chain; #[macro_use] extern crate error_chain;
#[macro_use] extern crate libimagstore;
extern crate libimagerror; extern crate libimagerror;
extern crate libimagstore;
extern crate libimagentrylink; extern crate libimagentrylink;
module_entry_path_mod!("annotations");
pub mod annotateable; pub mod annotateable;
pub mod annotation_fetcher; pub mod annotation_fetcher;
pub mod error; pub mod error;
pub mod iter;