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

81 lines
2.7 KiB
Rust
Raw Normal View History

//
// 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
//
2017-02-23 12:22:57 +00:00
use std::path::PathBuf;
use toml::Value;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
2017-02-23 12:22:57 +00:00
use libimagstore::store::Store;
use libimagentrylink::internal::InternalLinker;
use libimagerror::into::IntoError;
2017-08-26 15:53:08 +00:00
use toml_query::read::TomlValueReadExt;
use toml_query::insert::TomlValueInsertExt;
use result::Result;
use error::AnnotationErrorKind as AEK;
2017-02-23 12:22:57 +00:00
use error::MapErrInto;
pub trait Annotateable {
/// Add an annotation to `Self`, that is a `FileLockEntry` which is linked to `Self` (link as in
/// libimagentrylink).
///
2017-02-23 12:22:57 +00:00
/// 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>>;
2016-10-13 14:11:10 +00:00
2017-06-26 17:28:36 +00:00
/// Check whether an entry is an annotation
fn is_annotation(&self) -> Result<bool>;
}
2017-02-23 12:22:57 +00:00
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))
2017-08-26 15:53:08 +00:00
.map_err_into(AEK::HeaderWriteError)
.map(|_| anno)
2017-02-23 12:22:57 +00:00
})
.and_then(|mut anno| {
anno.add_internal_link(self)
.map_err_into(AEK::LinkingError)
2017-02-23 12:22:57 +00:00
.map(|_| anno)
})
2016-10-13 14:11:10 +00:00
}
2017-06-26 17:28:36 +00:00
fn is_annotation(&self) -> Result<bool> {
self.get_header()
.read("annotation.is_annotation")
.map_err_into(AEK::StoreReadError)
.and_then(|res| match res {
2017-08-26 15:53:08 +00:00
Some(&Value::Boolean(b)) => Ok(b),
None => Ok(false),
_ => Err(AEK::HeaderTypeError.into_error()),
2017-06-26 17:28:36 +00:00
})
}
}
2017-02-23 12:22:57 +00:00