2016-10-13 13:12:52 +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::Store;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::storeid::StoreId;
|
2016-10-13 14:27:01 +00:00
|
|
|
use libimagnotes::note::Note;
|
2016-10-13 13:12:52 +00:00
|
|
|
|
|
|
|
use result::Result;
|
|
|
|
use error::AnnotationErrorKind as AEK;
|
|
|
|
|
|
|
|
use module_path::ModuleEntryPath;
|
|
|
|
|
|
|
|
pub trait Annotateable {
|
|
|
|
|
|
|
|
/// Add an annotation to `Self`, that is a `FileLockEntry` which is linked to `Self` (link as in
|
|
|
|
/// libimagentrylink).
|
|
|
|
///
|
|
|
|
/// This `Annotation` is stored in the Store itself.
|
|
|
|
fn annotate(&self, store: &Store) -> Result<Annotation> {
|
2016-10-13 14:27:01 +00:00
|
|
|
self.annotate_with_path_generator(store, DefaultAnnotationNameGenerator::new())
|
2016-10-13 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add an annotation to `Self`, that is a `FileLockEntry` which is linked to `Self` (link as in
|
|
|
|
/// libimagentrylink).
|
|
|
|
///
|
|
|
|
/// This `Annotation` is stored in the Store itself.
|
|
|
|
///
|
2016-10-13 14:27:01 +00:00
|
|
|
/// The `pg` is a AnnotationNameGenerator object which is used to generate a StoreId
|
|
|
|
fn annotate_with_path_generator(&self, store: &Store, pg: &AnnotationNameGenerator) -> Result<Annotation>;
|
2016-10-13 13:12:52 +00:00
|
|
|
|
2016-10-13 14:05:39 +00:00
|
|
|
/// List annotations of a Annotateable
|
|
|
|
///
|
2016-10-13 14:27:01 +00:00
|
|
|
/// This lists only annotations that are generated via the `DefaultAnnotationNameGenerator`
|
2016-10-13 14:05:39 +00:00
|
|
|
fn annotations(&self) -> Result<Vec<StoreId>>;
|
|
|
|
|
2016-10-13 14:11:10 +00:00
|
|
|
/// Remove an annotation by its ID
|
|
|
|
fn remove_annotation(&mut self, ann_id: &str) -> Result<()>;
|
|
|
|
|
|
|
|
/// Remove an annotation and remove the annotation object from the store, if there's no more
|
|
|
|
/// link to it.
|
|
|
|
fn remove_annotation_with_gc(&mut self, ann_id: &str, store: &Store) -> Result<()>;
|
|
|
|
|
2016-10-13 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:27:01 +00:00
|
|
|
/// A AnnotationNameGenerator generates a unique path for the annotation to be generated.
|
|
|
|
pub trait AnnotationNameGenerator {
|
|
|
|
fn generate_annotation_path(&self) -> Result<String>;
|
2016-10-13 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:27:01 +00:00
|
|
|
/// The DefaultAnnotationNameGenerator generates unique StoreIds for Annotations, where the
|
2016-10-13 13:12:52 +00:00
|
|
|
/// collection the annotations are stored in is `/annotation/`.
|
2016-10-13 14:27:01 +00:00
|
|
|
pub struct DefaultAnnotationNameGenerator;
|
2016-10-13 13:12:52 +00:00
|
|
|
|
2016-10-13 14:27:01 +00:00
|
|
|
impl AnnotationNameGenerator for DefaultAnnotationNameGenerator {
|
2016-10-13 13:12:52 +00:00
|
|
|
|
2016-10-13 14:27:01 +00:00
|
|
|
fn generate_annotation_path(&self) -> Result<String> {
|
|
|
|
Ok(format!("{}/{}", MODULE_ENTRY_PATH_NAME, Uuid::new_v4()))
|
2016-10-13 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-13 14:27:01 +00:00
|
|
|
pub struct Annotation<'a>(Note<'a>);
|
2016-10-13 13:12:52 +00:00
|
|
|
|
|
|
|
impl Annotateable for FileLockEntry {
|
|
|
|
|
2016-10-13 14:27:01 +00:00
|
|
|
fn annotate_with_path_generator(&self, store: &Store, pg: &AnnotationNameGenerator)
|
2016-10-13 13:12:52 +00:00
|
|
|
-> Result<Annotation>
|
|
|
|
{
|
2016-10-13 13:57:16 +00:00
|
|
|
pb.generate_annotation_path()
|
2016-10-13 14:27:01 +00:00
|
|
|
.and_then(|name| Note::new(store, name, String::new()).map_err_into(AEK::StoreWriteError))
|
2016-10-13 13:57:16 +00:00
|
|
|
.and_then(|mut fle| {
|
|
|
|
self.add_internal_link(&mut fle)
|
|
|
|
.map_err_into(AEK::LinkingError)
|
|
|
|
.map(|_| fle)
|
|
|
|
})
|
|
|
|
.map(Annotation)
|
2016-10-13 13:12:52 +00:00
|
|
|
}
|
2016-10-13 14:05:39 +00:00
|
|
|
|
|
|
|
/// Get the annotations of a FileLockEntry
|
|
|
|
///
|
|
|
|
/// Returns the pathes to the annotations, not the annotations itself.
|
|
|
|
fn annotations(&self) -> Result<Vec<StoreId>> {
|
2016-10-13 14:27:01 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref pb : PathBuf = PathBuf::from(format!("{}/{}",
|
|
|
|
libimagnotes::MODULE_ENTRY_PATH_NAME,
|
|
|
|
MODULE_ENTRY_PATH_NAME));
|
|
|
|
};
|
|
|
|
|
2016-10-13 14:05:39 +00:00
|
|
|
self.get_internal_links()
|
|
|
|
.map_err_into(AEK::LinkError)
|
2016-10-13 14:27:01 +00:00
|
|
|
.map(|v| v.iter_into().filter(|id| id.local().starts_with(pb)).collect())
|
2016-10-13 14:05:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 14:11:10 +00:00
|
|
|
/// Remove an annotation by its ID
|
|
|
|
fn remove_annotation(&mut self, ann_id: &str) -> Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove an annotation and remove the annotation object from the store, if there's no more
|
|
|
|
/// link to it.
|
|
|
|
fn remove_annotation_with_gc(&mut self, ann_id: &str, store: &Store) -> Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2016-10-13 13:12:52 +00:00
|
|
|
}
|