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
use chrono::NaiveDateTime as NDT;
use toml::Value;
use toml_query::insert::TomlValueInsertExt;
use failure::Fallible as Result;
use failure::Error;
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagentrydatetime::datepath::compiler::DatePathCompiler;
use constants::*;
use iter::get::TimeTrackingsGetIterator;
use tag::TimeTrackingTag as TTT;
pub trait TimeTrackStore<'a> {
fn create_timetracking_now(&'a self, ts: &TTT) -> Result<FileLockEntry<'a>>;
fn create_timetracking_at(&'a self, start: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>;
fn create_timetracking(&'a self, start: &NDT, end: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>;
fn get_timetrackings(&'a self) -> Result<TimeTrackingsGetIterator<'a>>;
}
fn now() -> NDT {
use chrono::offset::Local;
Local::now().naive_local()
}
lazy_static! {
static ref COMPILER: DatePathCompiler = {
use libimagentrydatetime::datepath::accuracy::Accuracy;
use libimagentrydatetime::datepath::format::Format;
DatePathCompiler::new(Accuracy::Second, Format::ElementIsFolder)
};
}
impl<'a> TimeTrackStore<'a> for Store {
fn create_timetracking_now(&'a self, ts: &TTT) -> Result<FileLockEntry<'a>> {
self.create_timetracking_at(&now(), ts)
}
fn create_timetracking_at(&'a self, start: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>> {
use std::path::PathBuf;
COMPILER.compile(CRATE_NAME, start)
.map_err(Error::from)
.map(|mut id| {
id.local_push(PathBuf::from(ts.as_str()));
id
})
.and_then(|id| self.create(id))
.and_then(|mut fle| {
let v = Value::String(ts.as_str().to_owned());
fle.get_header_mut()
.insert(DATE_TIME_TAG_HEADER_PATH, v)
.map_err(Error::from)
.map(|_| fle)
})
.and_then(|mut fle| {
let v = Value::String(start.format(DATE_TIME_FORMAT).to_string());
fle.get_header_mut()
.insert(DATE_TIME_START_HEADER_PATH, v)
.map_err(Error::from)
.map(|_| fle)
})
}
fn create_timetracking(&'a self, start: &NDT, end: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>> {
self.create_timetracking_at(start, ts)
.and_then(|mut fle| {
let v = Value::String(end.format(DATE_TIME_FORMAT).to_string());
fle.get_header_mut()
.insert(DATE_TIME_END_HEADER_PATH, v)
.map_err(Error::from)
.map(|_| fle)
})
}
fn get_timetrackings(&'a self) -> Result<TimeTrackingsGetIterator<'a>> {
Ok(TimeTrackingsGetIterator::new(self.entries()?, self))
}
}