1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::path::PathBuf;
use toml::Value;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::toml_ext::TomlValueExt;
use libimagentrylink::internal::InternalLinker;
use libimagerror::into::IntoError;
use result::Result;
use error::AnnotationErrorKind as AEK;
use error::MapErrInto;
pub trait Annotateable {
fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>>;
fn is_annotation(&self) -> Result<bool>;
}
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))
.map_err_into(AEK::StoreWriteError)
.and_then(|succeeded| {
if succeeded {
Ok(anno)
} else {
Err(AEK::HeaderWriteError.into_error())
}
})
})
.and_then(|mut anno| {
anno.add_internal_link(self)
.map_err_into(AEK::LinkingError)
.map(|_| anno)
})
}
fn is_annotation(&self) -> Result<bool> {
self.get_header()
.read("annotation.is_annotation")
.map_err_into(AEK::StoreReadError)
.and_then(|res| match res {
Some(Value::Boolean(b)) => Ok(b),
None => Ok(false),
_ => Err(AEK::HeaderTypeError.into_error()),
})
}
}