summaryrefslogtreecommitdiffstats
path: root/lib/domain/libimagtodo/src/store.rs
blob: cc1f2aa0fc89cea9c16d863561b8b482ad3f2466 (plain)
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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use std::result::Result as RResult;

use failure::Fallible as Result;
use chrono::NaiveDateTime;
use uuid::Uuid;
use toml_query::insert::TomlValueInsertExt;

use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::iter::Entries;
use libimagutil::date::datetime_to_string;
use libimagentryutil::isa::Is;

use crate::status::Status;
use crate::priority::Priority;
use crate::entry::TodoHeader;
use crate::entry::IsTodo;

pub trait TodoStore<'a> {
    fn create_todo(&'a self,
                   status: Status,
                   scheduled: Option<NaiveDateTime>,
                   hidden: Option<NaiveDateTime>,
                   due: Option<NaiveDateTime>,
                   prio: Option<Priority>,
                   check_sanity: bool) -> Result<FileLockEntry<'a>>;

    fn get_todo_by_uuid(&'a self, uuid: &Uuid) -> Result<Option<FileLockEntry<'a>>>;

    fn get_todos(&self) -> Result<Entries>;
}

impl<'a> TodoStore<'a> for Store {

    /// Create a new todo entry
    ///
    /// # Warning
    ///
    /// If check_sanity is set to false, this does not sanity-check the scheduled/hidden/due dates.
    /// This might result in unintended behaviour (hidden after due date, scheduled before hidden
    /// date... etc)
    ///
    /// An user of this function might want to use `date_sanity_check()` to perform sanity checks
    /// before calling TodoStore::create_todo() and show the Err(String) as a warning to user in an
    /// interactive way.
    fn create_todo(&'a self,
                   status: Status,
                   scheduled: Option<NaiveDateTime>,
                   hidden: Option<NaiveDateTime>,
                   due: Option<NaiveDateTime>,
                   prio: Option<Priority>,
                   check_sanity: bool) -> Result<FileLockEntry<'a>>
    {
        if check_sanity {
            trace!("Checking sanity before creating todo");
            if let Err(s) = date_sanity_check(scheduled.as_ref(), hidden.as_ref(), due.as_ref()) {
                trace!("Not sane.");
                return Err(format_err!("{}", s))
            }
        }

        let uuid = Uuid::new_v4();
        let uuid_s = format!("{}", uuid.to_hyphenated_ref()); // TODO: not how it is supposed to be
        debug!("Created new UUID for todo = {}", uuid_s);

        let mut entry = crate::module_path::new_id(uuid_s).and_then(|id| self.create(id))?;

        let header = TodoHeader {
            uuid,
            status,
            scheduled: scheduled.as_ref().map(datetime_to_string),
            hidden: hidden.as_ref().map(datetime_to_string),
            due: due.as_ref().map(datetime_to_string),
            priority: prio
        };

        debug!("Created header for todo: {:?}", header);

        let _ = entry.get_header_mut().insert_serialized("todo", header)?;
        let _ = entry.set_isflag::<IsTodo>()?;

        Ok(entry)
    }

    fn get_todo_by_uuid(&'a self, uuid: &Uuid) -> Result<Option<FileLockEntry<'a>>> {
        let uuid_s = format!("{}", uuid.to_hyphenated_ref()); // TODO: not how it is supposed to be
        debug!("Created new UUID for todo = {}", uuid_s);
        let id = crate::module_path::new_id(uuid_s)?;
        self.get(id)
    }

    /// Get all todos using Store::entries()
    fn get_todos(&self) -> Result<Entries> {
        self.entries().and_then(|es| es.in_collection("todo"))
    }
}

/// Perform a sanity check on the scheduled/hidden/due dates
///
/// This function returns a String as error, which can be shown as a warning to the user or as an
/// error.
pub fn date_sanity_check(scheduled: Option<&NaiveDateTime>,
                         hidden: Option<&NaiveDateTime>,
                         due: Option<&NaiveDateTime>)
    -> RResult<(), String>
{
    if let (Some(sched), Some(hid)) = (scheduled.as_ref(), hidden.as_ref()) {
        if sched > hid {
            return Err(format!("Scheduled date after hidden date: {s}, {h}",
                               s = sched,
                               h = hid))
        }
    }

    if let (Some(hid), Some(due)) = (hidden.as_ref(), due.as_ref()) {
        if hid > due {
            return Err(format!("Hidden date after due date: {h}, {d}",
                               h = hid,
                               d = due))
        }
    }

    if let (Some(sched), Some(due)) = (scheduled.as_ref(), due.as_ref()) {
        if sched > due {
            return Err(format!("Scheduled date after due date: {s}, {d}",
                               s = sched,
                               d = due))
        }
    }

    Ok(())
}