Skip to main content

libimagtimetrack/
timetracking.rs

1
2// imag - the personal information management suite for the commandline
3// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; version
8// 2.1 of the License.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18//
19
20//! Event types
21//!
22//! This module contains types that represent events. These types (which wrap FileLockEntries from
23//! the Store) represent Events, thus they have functionality for settings the start and end-time,
24//! getting the start and end time and also deleting start and end time.
25//!
26
27use chrono::naive::NaiveDateTime;
28
29use libimagstore::store::Entry;
30use libimagerror::errors::ErrorMsg as EM;
31use libimagentryutil::isa::Is;
32use libimagentryutil::isa::IsKindHeaderPathProvider;
33
34use crate::tag::TimeTrackingTag as TTT;
35use crate::constants::*;
36
37use toml::Value;
38use toml_query::delete::TomlValueDeleteExt;
39use toml_query::insert::TomlValueInsertExt;
40use toml_query::read::TomlValueReadTypeExt;
41use failure::Fallible as Result;
42use failure::ResultExt;
43use failure::Error;
44
45provide_kindflag_path!(pub IsTimeTracking, "timetrack.is_timetracking");
46
47pub trait TimeTracking {
48
49    fn is_timetracking(&self) -> Result<bool>;
50
51    fn get_timetrack_tag(&self) -> Result<TTT>;
52
53    fn set_start_datetime(&mut self, dt: NaiveDateTime) -> Result<()>;
54
55    fn get_start_datetime(&self) -> Result<Option<NaiveDateTime>>;
56
57    fn delete_start_datetime(&mut self) -> Result<()>;
58
59    fn set_end_datetime(&mut self, dt: NaiveDateTime) -> Result<()>;
60
61    fn get_end_datetime(&self) -> Result<Option<NaiveDateTime>>;
62
63    fn delete_end_datetime(&mut self) -> Result<()>;
64
65    fn valid(&self) -> Result<bool>;
66
67}
68
69impl TimeTracking for Entry {
70
71    fn is_timetracking(&self) -> Result<bool> {
72        self.is::<IsTimeTracking>().map_err(From::from)
73    }
74
75    fn get_timetrack_tag(&self) -> Result<TTT> {
76        self.get_header()
77            .read_string(DATE_TIME_TAG_HEADER_PATH)
78            .context(format_err!("Failed to read header '{}' of {}", DATE_TIME_TAG_HEADER_PATH,
79                                 self.get_location()))
80            .map_err(Error::from)?
81            .ok_or_else(|| Error::from(EM::EntryHeaderReadError))
82            .map(Into::into)
83    }
84
85    fn set_start_datetime(&mut self, dt: NaiveDateTime) -> Result<()> {
86        let s = dt.format(DATE_TIME_FORMAT).to_string();
87
88        self.get_header_mut()
89            .insert(DATE_TIME_START_HEADER_PATH, Value::String(s))
90            .context(format_err!("Failed get header '{}' of {}", DATE_TIME_START_HEADER_PATH,
91                                 self.get_location()))
92            .map_err(Error::from)
93            .map(|_| ())
94    }
95
96    fn get_start_datetime(&self) -> Result<Option<NaiveDateTime>> {
97        self.get_header()
98            .read_string(DATE_TIME_START_HEADER_PATH)
99            .context(format_err!("Failed read header '{}' of {}", DATE_TIME_START_HEADER_PATH,
100                                 self.get_location()))
101            .map_err(Error::from)
102            .and_then(header_value_to_dt)
103    }
104
105    fn delete_start_datetime(&mut self) -> Result<()> {
106        self.get_header_mut()
107            .delete(DATE_TIME_START_HEADER_PATH)
108            .context(format_err!("Failed delete header '{}' of {}", DATE_TIME_START_HEADER_PATH,
109                                 self.get_location()))
110            .map_err(Error::from)
111            .map(|_| ())
112    }
113
114    fn set_end_datetime(&mut self, dt: NaiveDateTime) -> Result<()> {
115        let s = dt.format(DATE_TIME_FORMAT).to_string();
116
117        self.get_header_mut()
118            .insert(DATE_TIME_END_HEADER_PATH, Value::String(s))
119            .context(format_err!("Failed insert header '{}' in {}", DATE_TIME_END_HEADER_PATH,
120                                 self.get_location()))
121            .map_err(Error::from)
122            .map(|_| ())
123    }
124
125    fn get_end_datetime(&self) -> Result<Option<NaiveDateTime>> {
126        self.get_header()
127            .read_string(DATE_TIME_END_HEADER_PATH)
128            .context(format_err!("Failed read header '{}' of {}", DATE_TIME_END_HEADER_PATH,
129                                 self.get_location()))
130            .map_err(Error::from)
131            .and_then(header_value_to_dt)
132    }
133
134    fn delete_end_datetime(&mut self) -> Result<()> {
135        self.get_header_mut()
136            .delete(DATE_TIME_END_HEADER_PATH)
137            .context(format_err!("Failed delete header '{}' of {}", DATE_TIME_END_HEADER_PATH,
138                                 self.get_location()))
139            .map_err(Error::from)
140            .map(|_| ())
141    }
142
143    /// Check whether the Event is valid
144    ///
145    /// That is:
146    ///
147    /// * The end date is after the start date (or not set)
148    ///
149    /// # Return values
150    ///
151    /// Ok(true) if Event is valid
152    /// Ok(false) if Event is invalid
153    /// Err(e) if checking validity failed
154    ///
155    fn valid(&self) -> Result<bool> {
156        self.get_start_datetime().and_then(|st| self.get_end_datetime().map(|et| st <= et))
157    }
158
159}
160
161fn header_value_to_dt(val: Option<String>) -> Result<Option<NaiveDateTime>> {
162    match val {
163        Some(ref s) => NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT)
164            .context(format_err!("Failed to parse '{}' datetime with format '{}'",
165                                 s, DATE_TIME_FORMAT))
166            .map_err(Error::from).map(Some),
167        None => Ok(None),
168    }
169}
170