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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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 chrono::NaiveDateTime;

use libimagstore::store::FileLockEntry;

use tag::TimeTrackingTag as TTT;
use timetracking::TimeTracking;


pub fn has_start_time(entry: &FileLockEntry) -> bool {
    is_match!(entry.get_start_datetime(), Ok(Some(_)))
}

pub fn has_end_time(entry: &FileLockEntry) -> bool {
    is_match!(entry.get_end_datetime(), Ok(Some(_)))
}

pub fn has_tag(entry: &FileLockEntry) -> bool {
    is_match!(entry.get_timetrack_tag(), Ok(_))
}

pub fn has_start_time_where<F>(f: F) -> HasStartTimeWhere<F>
    where F: Fn(&NaiveDateTime) -> bool
{
    HasStartTimeWhere::new(f)
}

pub fn has_end_time_where<F>(f: F) -> HasEndTimeWhere<F>
    where F: Fn(&NaiveDateTime) -> bool
{
    HasEndTimeWhere::new(f)
}

pub fn has_one_of_tags<'a>(tags: &'a Vec<TTT>) -> HasOneOfTags<'a> {
    HasOneOfTags::new(tags)
}

mod types {
    use chrono::NaiveDateTime;
    use filters::filter::Filter;

    use tag::TimeTrackingTag as TTT;
    use timetracking::TimeTracking;

    use libimagstore::store::FileLockEntry;

    pub struct HasStartTimeWhere<F>(F)
        where F: Fn(&NaiveDateTime) -> bool;

    impl<F: Fn(&NaiveDateTime) -> bool> HasStartTimeWhere<F> {
        pub fn new(f: F) -> HasStartTimeWhere<F> {
            HasStartTimeWhere(f)
        }
    }

    impl<'a, F> Filter<FileLockEntry<'a>> for HasStartTimeWhere<F>
        where F: Fn(&NaiveDateTime) -> bool
    {
        fn filter(&self, entry: &FileLockEntry) -> bool {
            entry.get_start_datetime()
                .map(|o| o.map(|dt| (self.0)(&dt)).unwrap_or(false))
                .unwrap_or(false)
        }
    }

    pub struct HasEndTimeWhere<F>(F)
        where F: Fn(&NaiveDateTime) -> bool;

    impl<F: Fn(&NaiveDateTime) -> bool> HasEndTimeWhere<F> {
        pub fn new(f: F) -> HasEndTimeWhere<F> {
            HasEndTimeWhere(f)
        }
    }

    impl<'a, F> Filter<FileLockEntry<'a>> for HasEndTimeWhere<F>
        where F: Fn(&NaiveDateTime) -> bool
    {
        fn filter(&self, entry: &FileLockEntry) -> bool {
            entry.get_end_datetime()
                .map(|o| o.map(|dt| (self.0)(&dt)).unwrap_or(false))
                .unwrap_or(false)
        }
    }

    pub struct HasOneOfTags<'a>(&'a Vec<TTT>);

    impl<'a> HasOneOfTags<'a> {
        pub fn new(tags: &'a Vec<TTT>) -> HasOneOfTags<'a> {
            HasOneOfTags(tags)
        }
    }

    impl<'a, 'b> Filter<FileLockEntry<'b>> for HasOneOfTags<'a> {
        fn filter(&self, entry: &FileLockEntry) -> bool {
            entry.get_timetrack_tag().map(|t| self.0.contains(&t)).unwrap_or(false)
        }
    }

}
pub use self::types::HasStartTimeWhere;
pub use self::types::HasEndTimeWhere;
pub use self::types::HasOneOfTags;