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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::path::Path;
use std::path::PathBuf;
use std::fmt::Debug;
use failure::Fallible as Result;
use toml::Value;
use toml_query::insert::TomlValueInsertExt;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::storeid::StoreId;
use libimagstore::iter::Entries;
use libimagentryref::hasher::default::DefaultHasher;
use libimagentryref::reference::Config;
use libimagentryref::reference::RefFassade;
use libimagentryref::reference::Ref;
use libimagentryref::reference::MutRef;
use libimagentryutil::isa::Is;
use crate::mid::MessageId;
use crate::mail::Mail;
use crate::mail::IsMail;
use crate::hasher::MailHasher;
use crate::util::get_message_id_for_mailfile;
pub trait MailStore<'a> {
fn create_mail_from_path<P, CollName>(&'a self, p: P, collection_name: CollName, config: &Config)
-> Result<FileLockEntry<'a>>
where P: AsRef<Path> + Debug,
CollName: AsRef<str> + Debug;
fn get_mail_from_path<P>(&'a self, p: P)
-> Result<Option<FileLockEntry<'a>>>
where P: AsRef<Path> + Debug;
fn retrieve_mail_from_path<P, CollName>(&'a self, p: P, collection_name: CollName, config: &Config)
-> Result<FileLockEntry<'a>>
where P: AsRef<Path> + Debug,
CollName: AsRef<str> + Debug;
fn get_mail(&'a self, mid: MessageId) -> Result<Option<FileLockEntry<'a>>>;
fn all_mails(&'a self) -> Result<Entries<'a>>;
}
impl<'a> MailStore<'a> for Store {
fn create_mail_from_path<P, CollName>(&'a self, p: P, collection_name: CollName, config: &Config)
-> Result<FileLockEntry<'a>>
where P: AsRef<Path> + Debug,
CollName: AsRef<str> + Debug
{
let message_id = get_message_id_for_mailfile(p.as_ref())?;
let new_sid = crate::module_path::new_id(message_id.clone())?;
let mut entry = self.create(new_sid)?;
entry
.as_ref_with_hasher_mut::<MailHasher>()
.make_ref(p, collection_name, config, false)?;
let _ = entry
.get_header_mut()
.insert("mail.message-id", Value::String(message_id))?;
entry.set_isflag::<IsMail>()?;
Ok(entry)
}
fn get_mail_from_path<P>(&'a self, p: P)
-> Result<Option<FileLockEntry<'a>>>
where P: AsRef<Path> + Debug
{
let message_id = get_message_id_for_mailfile(p.as_ref())?;
let new_sid = crate::module_path::new_id(message_id.clone())?;
match self.get(new_sid)? {
Some(mut entry) => {
if !entry.is_ref()? {
return Err(format_err!("{} is not a ref", entry.get_location()))
}
if p.as_ref().ends_with(entry.as_ref_with_hasher::<MailHasher>().get_relative_path()?) {
return Err(format_err!("{} is not a ref to {:?}",
entry.get_location(),
p.as_ref().display()))
}
let _ = entry.get_header_mut().insert("mail.message-id", Value::String(message_id))?;
Ok(Some(entry))
},
None => Ok(None),
}
}
fn retrieve_mail_from_path<P, CollName>(&'a self, p: P, collection_name: CollName, config: &Config)
-> Result<FileLockEntry<'a>>
where P: AsRef<Path> + Debug,
CollName: AsRef<str> + Debug
{
let message_id = get_message_id_for_mailfile(&p)?;
let new_sid = crate::module_path::new_id(message_id.clone())?;
let mut entry = self.retrieve(new_sid)?;
let _ = entry
.get_header_mut()
.insert("mail.message-id", Value::String(message_id))?;
entry
.as_ref_with_hasher_mut::<DefaultHasher>()
.make_ref(p, collection_name, config, false)?;
entry.set_isflag::<IsMail>()?;
Ok(entry)
}
fn get_mail(&'a self, mid: MessageId) -> Result<Option<FileLockEntry<'a>>> {
let mid_s : String = mid.into();
self.get(StoreId::new(PathBuf::from(mid_s))?)
.and_then(|oe| match oe {
Some(e) => if e.is_mail()? {
Ok(Some(e))
} else {
Err(format_err!("{} is not a mail entry", e.get_location()))
},
None => Ok(None)
})
}
fn all_mails(&'a self) -> Result<Entries<'a>> {
self.entries()?.in_collection("mail")
}
}