diff --git a/lib/entry/libimagentryannotation/Cargo.toml b/lib/entry/libimagentryannotation/Cargo.toml index a677a6b3..166eef92 100644 --- a/lib/entry/libimagentryannotation/Cargo.toml +++ b/lib/entry/libimagentryannotation/Cargo.toml @@ -25,6 +25,9 @@ toml = "0.4" toml-query = "0.8" failure = "0.1" failure_derive = "0.1" +uuid = { version = "0.7", features = ["v4"] } +log = "0.4.0" + libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" } diff --git a/lib/entry/libimagentryannotation/src/annotateable.rs b/lib/entry/libimagentryannotation/src/annotateable.rs index 45da1dca..fdc12821 100644 --- a/lib/entry/libimagentryannotation/src/annotateable.rs +++ b/lib/entry/libimagentryannotation/src/annotateable.rs @@ -18,6 +18,7 @@ // use toml::Value; +use uuid::Uuid; use libimagstore::store::Entry; use libimagstore::store::FileLockEntry; @@ -28,7 +29,6 @@ use libimagentrylink::internal::InternalLinker; use libimagentryutil::isa::Is; use libimagentryutil::isa::IsKindHeaderPathProvider; -use toml_query::read::TomlValueReadTypeExt; use toml_query::insert::TomlValueInsertExt; use failure::Fallible as Result; @@ -37,9 +37,10 @@ use failure::Error; use failure::err_msg; use iter::*; +use module_path::ModuleEntryPath; pub trait Annotateable { - fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result>; + fn annotate<'a>(&mut self, store: &'a Store) -> Result>; fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result>>; fn annotations<'a>(&self, store: &'a Store) -> Result>; fn is_annotation(&self) -> Result; @@ -50,9 +51,11 @@ provide_kindflag_path!(IsAnnotation, "annotation.is_annotation"); 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> { - use module_path::ModuleEntryPath; - store.retrieve(ModuleEntryPath::new(ann_name).into_storeid()?) + fn annotate<'a>(&mut self, store: &'a Store) -> Result> { + let ann_name = Uuid::new_v4().to_hyphenated().to_string(); + debug!("Creating annotation with name = {}", ann_name); + + store.retrieve(ModuleEntryPath::new(ann_name.clone()).into_storeid()?) .and_then(|mut anno| { { let _ = anno.set_isflag::()?; @@ -70,23 +73,17 @@ 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 + // Removes the annotation `ann_name` from the current entry. + // Fails if there's no such annotation entry or if the link to that annotation entry does not + // exist. fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result>> { - for annotation in self.annotations(store)? { - let mut anno = annotation?; - let name = match anno.get_header().read_string("annotation.name")? { - Some(ref name) => name.clone(), - None => continue, - }; - - if name == ann_name { - let _ = self.remove_internal_link(&mut anno)?; - return Ok(Some(anno)); - } + if let Some(mut annotation) = store.get(ModuleEntryPath::new(ann_name).into_storeid()?)? { + let _ = self.remove_internal_link(&mut annotation)?; + Ok(Some(annotation)) + } else { + // error: annotation does not exist + Err(format_err!("Annotation '{}' does not exist", ann_name)).map_err(Error::from) } - - Ok(None) } /// Get all annotations of an entry diff --git a/lib/entry/libimagentryannotation/src/annotation_fetcher.rs b/lib/entry/libimagentryannotation/src/annotation_fetcher.rs index ac903807..dd6c2222 100644 --- a/lib/entry/libimagentryannotation/src/annotation_fetcher.rs +++ b/lib/entry/libimagentryannotation/src/annotation_fetcher.rs @@ -18,21 +18,17 @@ // use libimagstore::store::Store; +use libimagstore::storeid::StoreIdIterator; use failure::Fallible as Result; -use iter::*; - -pub trait AnnotationFetcher<'a> { - - fn all_annotations(&'a self) -> Result>; +pub trait AnnotationFetcher { + fn all_annotations(&self) -> Result; } -impl<'a> AnnotationFetcher<'a> for Store { - - fn all_annotations(&'a self) -> Result> { - Ok(AnnotationIter::new(self.entries()?.without_store(), self)) +impl<'a> AnnotationFetcher for Store { + fn all_annotations(&self) -> Result { + self.entries().map(|iter| iter.in_collection("annotation").without_store()) } - } diff --git a/lib/entry/libimagentryannotation/src/lib.rs b/lib/entry/libimagentryannotation/src/lib.rs index 24de090b..e3be82b9 100644 --- a/lib/entry/libimagentryannotation/src/lib.rs +++ b/lib/entry/libimagentryannotation/src/lib.rs @@ -39,7 +39,9 @@ extern crate toml; extern crate toml_query; -extern crate failure; +#[macro_use] extern crate failure; +#[macro_use] extern crate log; +extern crate uuid; #[macro_use] extern crate libimagstore; extern crate libimagerror;