imag/lib/entry/libimagentryannotation/src/annotateable.rs

105 lines
3.7 KiB
Rust
Raw Normal View History

//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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
//
2017-02-23 12:22:57 +00:00
use toml::Value;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
2017-02-23 12:22:57 +00:00
use libimagstore::store::Store;
2017-10-12 16:23:52 +00:00
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator;
2017-02-23 12:22:57 +00:00
use libimagentrylink::internal::InternalLinker;
use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
use toml_query::read::TomlValueReadTypeExt;
2017-08-26 15:53:08 +00:00
use toml_query::insert::TomlValueInsertExt;
use error::Result;
use error::AnnotationErrorKind as AEK;
use error::ResultExt;
2017-10-12 16:23:52 +00:00
use iter::*;
2017-10-12 16:23:52 +00:00
pub trait Annotateable {
2017-02-23 12:22:57 +00:00
fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>>;
2017-10-12 16:23:52 +00:00
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>>;
2017-06-26 17:28:36 +00:00
fn is_annotation(&self) -> Result<bool>;
}
provide_kindflag_path!(IsAnnotation, "annotation.is_annotation");
2017-02-23 12:22:57 +00:00
impl Annotateable for Entry {
2017-10-12 16:23:52 +00:00
/// Annotate an entry, returns the new entry which is used to annotate
2017-02-23 12:22:57 +00:00
fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>> {
2017-10-12 16:23:52 +00:00
use module_path::ModuleEntryPath;
store.retrieve(ModuleEntryPath::new(ann_name).into_storeid()?)
2017-10-12 16:23:52 +00:00
.map_err(From::from)
2017-02-23 12:22:57 +00:00
.and_then(|mut anno| {
2017-10-12 16:23:52 +00:00
{
let _ = anno.set_isflag::<IsAnnotation>()?;
let _ = anno
.get_header_mut()
.insert("annotation.name", Value::String(String::from(ann_name)))?;
2017-10-12 16:23:52 +00:00
}
Ok(anno)
2017-02-23 12:22:57 +00:00
})
.and_then(|mut anno| {
anno.add_internal_link(self)
.chain_err(|| AEK::LinkingError)
2017-02-23 12:22:57 +00:00
.map(|_| anno)
})
2016-10-13 14:11:10 +00:00
}
2017-10-12 16:23:52 +00:00
/// 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 = annotation?;
let name = match anno.get_header().read_string("annotation.name")? {
Some(ref name) => name.clone(),
None => continue,
2017-10-12 16:23:52 +00:00
};
if name == ann_name {
let _ = self.remove_internal_link(&mut anno)?;
return Ok(Some(anno));
2017-10-12 16:23:52 +00:00
}
}
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))
}
2017-06-26 17:28:36 +00:00
fn is_annotation(&self) -> Result<bool> {
self.is::<IsAnnotation>().map_err(From::from)
2017-06-26 17:28:36 +00:00
}
}
2017-02-23 12:22:57 +00:00