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
154
155
156
157
158
159
use std::cmp::Ordering;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagerror::trace::trace_error;
use libimagentryutil::isa::Is;
use chrono::offset::Local;
use chrono::Datelike;
use itertools::Itertools;
use chrono::naive::NaiveDateTime;
use chrono::Timelike;
use failure::Fallible as Result;
use failure::Error;
use entry::IsDiaryEntry;
use diaryid::DiaryId;
use diaryid::FromStoreId;
use iter::DiaryEntryIterator;
use iter::DiaryNameIterator;
pub trait Diary {
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry>;
fn new_entry_now(&self, diary_name: &str) -> Result<FileLockEntry>;
fn new_entry_at(&self, diary_name: &str, ndt: &NaiveDateTime) -> Result<FileLockEntry>;
fn entries(&self, diary_name: &str) -> Result<DiaryEntryIterator>;
fn get_youngest_entry_id(&self, diary_name: &str) -> Option<Result<DiaryId>>;
fn diary_names(&self) -> Result<DiaryNameIterator>;
}
impl Diary for Store {
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry> {
let dt = Local::now();
let ndt = dt.naive_local();
let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0, 0);
let mut entry = self.retrieve(id)?;
let _ = entry.set_isflag::<IsDiaryEntry>()?;
Ok(entry)
}
fn new_entry_now(&self, diary_name: &str) -> Result<FileLockEntry> {
let dt = Local::now();
let ndt = dt.naive_local();
self.new_entry_at(diary_name, &ndt)
}
fn new_entry_at(&self, diary_name: &str, ndt: &NaiveDateTime) -> Result<FileLockEntry> {
let id = DiaryId::new(String::from(diary_name),
ndt.year(),
ndt.month(),
ndt.day(),
ndt.hour(),
ndt.minute(),
ndt.second());
let mut entry = self.retrieve(id)?;
let _ = entry.set_isflag::<IsDiaryEntry>()?;
Ok(entry)
}
fn entries(&self, diary_name: &str) -> Result<DiaryEntryIterator> {
debug!("Building iterator for module 'diary' with diary name = '{}'", diary_name);
Store::entries(self)
.map(|iter| DiaryEntryIterator::new(String::from(diary_name), iter.without_store()))
}
fn get_youngest_entry_id(&self, diary_name: &str) -> Option<Result<DiaryId>> {
match Diary::entries(self, diary_name) {
Err(e) => Some(Err(e)),
Ok(entries) => {
let mut sorted_entries = vec![];
for entry in entries {
let entry = match entry {
Ok(e) => DiaryId::from_storeid(&e),
Err(e) => return Some(Err(e)),
};
sorted_entries.push(entry);
}
sorted_entries.into_iter().sorted_by(|a, b| {
match (a, b) {
(&Ok(ref a), &Ok(ref b)) => {
let a : NaiveDateTime = a.clone().into();
let b : NaiveDateTime = b.clone().into();
a.cmp(&b)
},
(&Ok(_), &Err(ref e)) => {
trace_error(e);
Ordering::Less
},
(&Err(ref e), &Ok(_)) => {
trace_error(e);
Ordering::Greater
},
(&Err(ref e1), &Err(ref e2)) => {
trace_error(e1);
trace_error(e2);
Ordering::Equal
},
}
})
.into_iter()
.rev()
.next()
}
}
}
fn diary_names(&self) -> Result<DiaryNameIterator> {
self.entries()
.map(|it| DiaryNameIterator::new(it.without_store()))
.map_err(Error::from)
}
}