summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-02-23 14:29:44 +0100
committerGitHub <noreply@github.com>2017-02-23 14:29:44 +0100
commite33e5d287b961ff79d638d3369ea39b9e8c063a0 (patch)
treeb21826857de956eff80383afd772c833e71ee7ea
parent54655b9bce1ea4367396af5de0c2aa7376a05cea (diff)
parent5034540595b974305701eca2a1c7153f7af4e8ec (diff)
downloadimag-e33e5d287b961ff79d638d3369ea39b9e8c063a0.zip
imag-e33e5d287b961ff79d638d3369ea39b9e8c063a0.tar.gz
Merge pull request #801 from matthiasbeyer/libimagannotation/init
libimagannotation: init
-rw-r--r--Cargo.toml1
-rw-r--r--libimagannotation/Cargo.toml25
-rw-r--r--libimagannotation/src/annotateable.rs70
-rw-r--r--libimagannotation/src/annotation_fetcher.rs116
-rw-r--r--libimagannotation/src/error.rs35
-rw-r--r--libimagannotation/src/lib.rs33
-rw-r--r--libimagannotation/src/result.rs26
7 files changed, 306 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 6a2adae..e203357 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ members = [
"imag-store",
"imag-tag",
"imag-view",
+ "libimagannotation",
"libimagentryedit",
"libimagentryfilter",
"libimagentrylink",
diff --git a/libimagannotation/Cargo.toml b/libimagannotation/Cargo.toml
new file mode 100644
index 0000000..4d15b2d
--- /dev/null
+++ b/libimagannotation/Cargo.toml
@@ -0,0 +1,25 @@
+[package]
+name = "libimagannotation"
+version = "0.2.0"
+authors = ["Matthias Beyer <mail@beyermatthias.de>"]
+
+[dependencies]
+uuid = { version = "0.3.1", features = ["v4"] }
+lazy_static = "0.1.15"
+toml = "0.2.*"
+
+[dependencies.libimagstore]
+path = "../libimagstore"
+
+[dependencies.libimagnotes]
+path = "../libimagnotes"
+
+[dependencies.libimagerror]
+path = "../libimagerror"
+
+[dependencies.libimagentrylink]
+path = "../libimagentrylink"
+
+[dependencies.libimagutil]
+path = "../libimagutil"
+
diff --git a/libimagannotation/src/annotateable.rs b/libimagannotation/src/annotateable.rs
new file mode 100644
index 0000000..698773c
--- /dev/null
+++ b/libimagannotation/src/annotateable.rs
@@ -0,0 +1,70 @@
+//
+// 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 std::path::PathBuf;
+
+use toml::Value;
+
+use libimagstore::store::Entry;
+use libimagstore::store::FileLockEntry;
+use libimagstore::store::Store;
+use libimagstore::toml_ext::TomlValueExt;
+use libimagentrylink::internal::InternalLinker;
+use libimagerror::into::IntoError;
+
+use result::Result;
+use error::AnnotationErrorKind as AEK;
+use error::MapErrInto;
+
+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>>;
+
+}
+
+impl Annotateable for Entry {
+
+ fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>> {
+ store.retrieve(PathBuf::from(ann_name))
+ .map_err_into(AEK::StoreWriteError)
+ .and_then(|mut anno| {
+ anno.get_header_mut()
+ .insert("annotation.is_annotation", Value::Boolean(true))
+ .map_err_into(AEK::StoreWriteError)
+ .and_then(|succeeded| {
+ if succeeded {
+ Ok(anno)
+ } else {
+ Err(AEK::HeaderWriteError.into_error())
+ }
+ })
+ })
+ .and_then(|mut anno| {
+ anno.add_internal_link(self)
+ .map_err_into(AEK::LinkingError)
+ .map(|_| anno)
+ })
+ }
+
+}
+
diff --git a/libimagannotation/src/annotation_fetcher.rs b/libimagannotation/src/annotation_fetcher.rs
new file mode 100644
index 0000000..6c5ac29
--- /dev/null
+++ b/libimagannotation/src/annotation_fetcher.rs
@@ -0,0 +1,116 @@
+//
+// 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;
+use libimagnotes::note::Note;
+use libimagnotes::note::NoteIterator;
+use libimagstore::storeid::StoreIdIterator;
+
+use result::Result;
+use error::AnnotationErrorKind as AEK;
+use error::MapErrInto;
+
+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>> {
+ Note::all_notes(self)
+ .map(|iter| AnnotationIter::new(iter))
+ .map_err_into(AEK::StoreReadError)
+ }
+
+ /// 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(AEK::StoreReadError)
+ .map(Box::new)
+ .map(|iter| StoreIdIterator::new(iter))
+ .map(|iter| NoteIterator::new(self, iter))
+ .map(|iter| AnnotationIter::new(iter))
+ }
+
+}
+
+pub mod iter {
+ use toml::Value;
+
+ use libimagstore::toml_ext::TomlValueExt;
+ use libimagerror::into::IntoError;
+ use libimagnotes::note::Note;
+ use libimagnotes::note::NoteIterator;
+
+ use result::Result;
+ use error::AnnotationErrorKind as AEK;
+ use error::MapErrInto;
+
+ #[derive(Debug)]
+ pub struct AnnotationIter<'a>(NoteIterator<'a>);
+
+ impl<'a> AnnotationIter<'a> {
+
+ pub fn new(noteiter: NoteIterator<'a>) -> AnnotationIter<'a> {
+ AnnotationIter(noteiter)
+ }
+
+ }
+
+ impl<'a> Iterator for AnnotationIter<'a> {
+ type Item = Result<Note<'a>>;
+
+ fn next(&mut self) -> Option<Result<Note<'a>>> {
+ loop {
+ match self.0.next() {
+ Some(Ok(note)) => {
+ let hdr = note.get_header().read("annotation.is_annotation");
+ match hdr {
+ Ok(None) => continue, // not an annotation
+ Ok(Some(Value::Boolean(true))) => return Some(Ok(note)),
+ Ok(Some(_)) => return Some(Err(AEK::HeaderTypeError.into_error())),
+ Err(e) => return Some(Err(e).map_err_into(AEK::HeaderReadError)),
+ }
+ },
+ Some(Err(e)) => return Some(Err(e).map_err_into(AEK::StoreReadError)),
+ None => return None, // iterator consumed
+ }
+ }
+ }
+
+ }
+
+}
+
diff --git a/libimagannotation/src/error.rs b/libimagannotation/src/error.rs
new file mode 100644
index 0000000..25fc004
--- /dev/null
+++ b/libimagannotation/src/error.rs
@@ -0,0 +1,35 @@
+//
+// 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
+//
+
+generate_error_module!(
+ generate_error_types!(AnnotationError, AnnotationErrorKind,
+ StoreReadError => "Store read error",
+ StoreWriteError => "Store write error",
+
+ LinkingError => "Error while linking",
+ HeaderWriteError => "Couldn't write Header for annotation",
+ HeaderReadError => "Couldn't read Header of Entry",
+ HeaderTypeError => "Header field has unexpected type"
+ );
+);
+
+pub use self::error::AnnotationError;
+pub use self::error::AnnotationErrorKind;
+pub use self::error::MapErrInto;
+
diff --git a/libimagannotation/src/lib.rs b/libimagannotation/src/lib.rs
new file mode 100644
index 0000000..0cb4e77
--- /dev/null
+++ b/libimagannotation/src/lib.rs
@@ -0,0 +1,33 @@
+//
+// 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
+//
+
+extern crate uuid;
+extern crate toml;
+
+#[macro_use] extern crate libimagerror;
+#[macro_use] extern crate libimagstore;
+extern crate libimagentrylink;
+extern crate libimagnotes;
+extern crate libimagutil;
+
+pub mod annotateable;
+pub mod annotation_fetcher;
+pub mod error;
+pub mod result;
+
diff --git a/libimagannotation/src/result.rs b/libimagannotation/src/result.rs
new file mode 100644
index 0000000..292fc0e
--- /dev/null
+++ b/libimagannotation/src/result.rs
@@ -0,0 +1,26 @@
+//
+// 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 std::result::Result as RResult;
+
+use error::AnnotationError;
+
+pub type Result<T> = RResult<T, AnnotationError>;
+
+