diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-02-05 19:58:08 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-02-10 00:58:21 +0100 |
commit | ed01f8b463322b89fbe9abcfa528f9be74a077b8 (patch) | |
tree | 83a1ded45afe8bd030b6b09a361c6ddb5dd6d2f2 | |
parent | b010d69e62eb051ac088a683cc2087e3dbb0e2e4 (diff) |
Make annotations unnamed (automatically UUID named)
It makes no sense to name annotations, a user only cares about whether
there are annotations or not, or their contents. A name has no meaning
in this context.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/entry/libimagentryannotation/Cargo.toml | 3 | ||||
-rw-r--r-- | lib/entry/libimagentryannotation/src/annotateable.rs | 37 | ||||
-rw-r--r-- | lib/entry/libimagentryannotation/src/annotation_fetcher.rs | 16 | ||||
-rw-r--r-- | lib/entry/libimagentryannotation/src/lib.rs | 4 |
4 files changed, 29 insertions, 31 deletions
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<FileLockEntry<'a>>; + fn annotate<'a>(&mut self, store: &'a Store) -> Result<FileLockEntry<'a>>; fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<Option<FileLockEntry<'a>>>; fn annotations<'a>(&self, store: &'a Store) -> Result<AnnotationIter<'a>>; fn is_annotation(&self) -> Result<bool>; @@ -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<FileLockEntry<'a>> { - use module_path::ModuleEntryPath; - store.retrieve(ModuleEntryPath::new(ann_name).into_storeid()?) + fn annotate<'a>(&mut self, store: &'a Store) -> Result<FileLockEntry<'a>> { + 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::<IsAnnotation>()?; @@ -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<Option<FileLockEntry<'a>>> { - 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<AnnotationIter<'a>>; +pub trait AnnotationFetcher { + fn all_annotations(&self) -> Result<StoreIdIterator>; } -impl<'a> AnnotationFetcher<'a> for Store { - - fn all_annotations(&'a self) -> Result<AnnotationIter<'a>> { - Ok(AnnotationIter::new(self.entries()?.without_store(), self)) +impl<'a> AnnotationFetcher for Store { + fn all_annotations(&self) -> Result<StoreIdIterator> { + 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; |