-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.cc
97 lines (77 loc) · 2.38 KB
/
filter.cc
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
#include <algorithm>
#include <string>
#include "atl/string.h"
#include "worklog.h"
#include "filter.h"
namespace worklog {
Filter ParseFilter(const std::string& text) {
Filter filter;
bool ignore_empty = true;
auto queries = atl::Split(text, " ", ignore_empty);
for (const auto& query : queries) {
std::vector<std::string> key_value = atl::Split(query, ":", ignore_empty);
if (key_value.size() != 2) {
// TODO(an): Display some better error message
continue;
}
bool is_negated = false;
if (key_value[0].find("-") == 0) {
key_value[0] = key_value[0].substr(1);
is_negated = true;
}
const std::string& key = key_value[0];
const std::string& value = key_value[1];
if (key == "tag") {
if (is_negated) {
filter.tags_negative.insert(value);
continue;
}
filter.tags.insert(value);
} else if (key == "subject") {
filter.subject = value;
}
}
return filter;
}
void ApplyFilter(std::function<bool(const worklog::Log&)> apply_func, std::vector<worklog::Log>* logs) {
logs->erase(std::remove_if(logs->begin(), logs->end(), std::not1(apply_func)),
logs->end());
}
bool Contains(const std::set<std::string>& haystack,
const std::string& needle) {
auto res = haystack.find(needle);
return res != haystack.end();
}
std::function<bool(const worklog::Log&)> SubjectFilter(const Filter& filter) {
return [&filter](const worklog::Log& log) -> bool {
auto found = log.subject.find(filter.subject);
return found != std::string::npos;
};
}
std::function<bool(const worklog::Log&)> TagsFilter(const Filter& filter) {
return [&filter](const worklog::Log& log) -> bool {
int should_keep_score = 0;
for (const auto& tag : log.tags) {
if (Contains(filter.tags_negative, tag)) {
should_keep_score--;
}
if (Contains(filter.tags, tag)) {
should_keep_score++;
}
}
return should_keep_score > 0;
};
}
std::function<bool(const worklog::Log&)> OnlyValidFilter() {
return [](const worklog::Log& log) -> bool {
atl::Status valid_check = worklog::Validate(log);
return valid_check.ok();
};
}
std::function<bool(const worklog::Log&)> OnlyInvalidFilter() {
return [](const worklog::Log& log) -> bool {
atl::Status valid_check = worklog::Validate(log);
return !valid_check.ok();
};
}
} // namespace worklog