summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-timetrack/src/stop.rs
blob: c2662f742f6e119323a87d868840799cbc85a068 (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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 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::str::FromStr;

use filters::filter::Filter;
use chrono::NaiveDateTime;
use anyhow::Result;
use anyhow::Error;
use resiter::Filter as RFilter;
use resiter::Map;
use resiter::AndThen;

use libimagrt::runtime::Runtime;
use libimagtimetrack::tag::TimeTrackingTag;
use libimagtimetrack::store::*;
use libimagtimetrack::iter::filter::has_end_time;
use libimagtimetrack::iter::filter::has_one_of_tags;
use libimagtimetrack::timetracking::TimeTracking;

pub fn stop(rt: &Runtime) -> Result<()> {
    let (_, cmd) = rt.cli().subcommand();
    let cmd = cmd.unwrap(); // checked in main()

    let stop_time = match cmd.value_of("stop-time") {
        None | Some("now") => Ok(::chrono::offset::Local::now().naive_local()),
        Some(ndt)          => NaiveDateTime::from_str(ndt).map_err(Error::from),
    }?;

    let tags = cmd.values_of("tags").map(|tags| {
        tags.map(String::from)
            .map(TimeTrackingTag::from)
            .collect()
    });

    let tags : Vec<TimeTrackingTag> = match tags {
        Some(s) => s,
        None => {
            // Get all timetrackings which do not have an end datetime.
            rt.store()
                .get_timetrackings()?
                .and_then_ok(|t| Ok((t.get_end_datetime()?.is_none(), t)))
                .filter_ok(|tpl| (*tpl).0)
                .map_ok(|tpl| tpl.1)
                .and_then_ok(|t| t.get_timetrack_tag())
                .collect::<Result<Vec<_>>>()?
        }
    };


    let filter = has_end_time.not().and(has_one_of_tags(&tags));
    rt
        .store()
        .get_timetrackings()?

        // Filter all timetrackings for the ones that are not yet ended.
        .filter_ok(|e| filter.filter(e))

        // for each of these timetrackings, end them
        // for each result, print the backtrace (if any)
        .and_then_ok(|mut elem| {
            elem.set_end_datetime(stop_time.clone())?;
            debug!("Setting end time worked: {:?}", elem);
            rt.report_touched(elem.get_location()).map_err(Error::from)
        })
        .collect::<Result<Vec<_>>>()
        .map(|_| ())
}