summaryrefslogtreecommitdiffstats
path: root/lib/domain/libimagtodo/src/entry.rs
blob: 35fd9b3a796e15fb2fd8f09a1b45edcc3b0259f8 (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
//
// 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 libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
use libimagstore::store::Entry;
use libimagutil::date::datetime_from_string;

use failure::Fallible as Result;
use failure::Error;
use failure::ResultExt;
use chrono::NaiveDateTime;
use toml_query::read::Partial;
use toml_query::read::TomlValueReadExt;
use toml_query::insert::TomlValueInsertExt;
use uuid::Uuid;

use crate::status::Status;
use crate::priority::Priority;

#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct TodoHeader {
    pub(crate) uuid:       Uuid,
    pub(crate) status:     Status,
    pub(crate) scheduled:  Option<String>,
    pub(crate) hidden:     Option<String>,
    pub(crate) due:        Option<String>,
    pub(crate) priority:   Option<Priority>,
}

impl<'a> Partial<'a> for TodoHeader {
    const LOCATION: &'static str = "todo";
    type Output                  = Self;
}

pub trait Todo {
    fn is_todo(&self)                                     -> Result<bool>;
    fn get_uuid(&self)                                    -> Result<Uuid>;
    fn get_status(&self)                                  -> Result<Status>;
    fn set_status(&mut self, status: Status)              -> Result<()>;
    fn get_scheduled(&self)                               -> Result<Option<NaiveDateTime>>;
    fn set_scheduled(&mut self, scheduled: NaiveDateTime) -> Result<()>;
    fn get_hidden(&self)                                  -> Result<Option<NaiveDateTime>>;
    fn set_hidden(&mut self, hidden: NaiveDateTime)       -> Result<()>;
    fn get_due(&self)                                     -> Result<Option<NaiveDateTime>>;
    fn set_due(&mut self, due: NaiveDateTime)             -> Result<()>;
    fn get_priority(&self)                                -> Result<Option<Priority>>;
    fn set_priority(&mut self, prio: Priority)            -> Result<()>;
}

provide_kindflag_path!(pub IsTodo, "todo.is_todo");

impl Todo for Entry {
    fn is_todo(&self) -> Result<bool> {
        self.is::<IsTodo>().context("Cannot check whether Entry is a todo").map_err(From::from)
    }

    fn get_uuid(&self) -> Result<Uuid> {
        get_header(self).map(|hdr| hdr.uuid)
    }

    fn get_status(&self) -> Result<Status> {
        get_header(self).map(|hdr| hdr.status)
    }

    fn set_status(&mut self, status: Status) -> Result<()> {
        self.get_header_mut().insert_serialized("todo.status", status)?;
        Ok(())
    }

    fn get_scheduled(&self) -> Result<Option<NaiveDateTime>> {
        get_optional_ndt(self, |hdr| hdr.scheduled)
    }

    fn set_scheduled(&mut self, scheduled: NaiveDateTime) -> Result<()> {
        self.get_header_mut().insert_serialized("todo.scheduled", scheduled)?;
        Ok(())
    }

    fn get_hidden(&self) -> Result<Option<NaiveDateTime>> {
        get_optional_ndt(self, |hdr| hdr.hidden)
    }

    fn set_hidden(&mut self, hidden: NaiveDateTime) -> Result<()> {
        self.get_header_mut().insert_serialized("todo.hidden", hidden)?;
        Ok(())
    }

    fn get_due(&self) -> Result<Option<NaiveDateTime>> {
        get_optional_ndt(self, |hdr| hdr.due)
    }

    fn set_due(&mut self, due: NaiveDateTime) -> Result<()> {
        self.get_header_mut().insert_serialized("todo.due", due)?;
        Ok(())
    }

    fn get_priority(&self) -> Result<Option<Priority>> {
        get_header(self).map(|hdr| hdr.priority)
    }

    fn set_priority(&mut self, priority: Priority) -> Result<()> {
        self.get_header_mut().insert_serialized("todo.priority", priority)?;
        Ok(())
    }

}

fn get_header(entry: &Entry) -> Result<TodoHeader> {
    entry.get_header()
        .read_partial::<TodoHeader>()?
        .ok_or_else(|| {
            format_err!("{} does not contain a TODO header", entry.get_location())
        })
}

fn get_optional_ndt<F>(entry: &Entry, extractor: F)
    -> Result<Option<NaiveDateTime>>
    where F: FnOnce(TodoHeader) -> Option<String>
{
    get_header(entry).map(extractor)?.map(datetime_from_string).transpose().map_err(Error::from)
}