summaryrefslogtreecommitdiff
path: root/lib/parser/proc-iface.hh
blob: e13fd0ff364907b0e672eda0d8e7036c639f2195 (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
/*
** Copyright (C) 2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
**  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; either version 2.1
**  of the License, or (at your option) any later version.
**
**  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, 51 Franklin Street, Fifth Floor, Boston, MA
**  02110-1301, USA.
*/
#ifndef __PROC_IFACE_HH__
#define __PROC_IFACE_HH__

#include <string>
#include <vector>
#include <tuple>
#include <regex>

namespace Mux {

struct ProcIface {

	virtual ~ProcIface() = default;

	/**
	 * Get the "shortcut"/internal fields for the the given fieldstr or empty if there is none
	 *
	 * @param fieldstr a fieldstr, e.g "subject" or "s" for the subject field
	 *
	 * @return a vector with "exploded" values, with a code and a fullname. E.g. "s" might map
	 * to [<"S","subject">], while "recip" could map to [<"to", "T">, <"cc", "C">, <"bcc", "B">]
	 */
	struct FieldInfo {
		const std::string	field;
		const std::string	prefix;
		bool			supports_phrase;
		unsigned		id;
	};
	using FieldInfoVec = std::vector<FieldInfo>;

	virtual FieldInfoVec process_field (const std::string& field) const = 0;

	/**
	 * Process a value
	 *
	 * @param field a field name
	 * @param value a value
	 *
	 * @return the processed value
	 */
	virtual std::string process_value (
		const std::string& field, const std::string& value) const = 0;

	/**
	 * Is this a range field?
	 *
	 * @param field some field
	 *
	 * @return true if it is a range-field; false otherwise.
	 */
	virtual bool is_range_field (const std::string& field) const = 0;


	/**
	 * Process a range field
	 *
	 * @param fieldstr a fieldstr, e.g "date" or "d" for the date field
	 * @param lower lower bound or empty
	 * @param upper upper bound or empty
	 *
	 * @return the processed range
	 */
	struct Range {
		std::string lower;
		std::string upper;
	};
	virtual Range process_range (const std::string& field, const std::string& lower,
				     const std::string& upper) const = 0;

	/**
	 *
	 *
	 * @param field
	 * @param rx
	 *
	 * @return
	 */
	virtual std::vector<std::string>
	process_regex (const std::string& field, const std::regex& rx) const = 0;

}; // ProcIface


struct DummyProc: public ProcIface { // For testing

	std::vector<FieldInfo>
	process_field (const std::string& field) const override {
		return {{ field, "x", false, 0 }};
	}

	std::string
	process_value (const std::string& field, const std::string& value) const override {
		return value;
	}

	bool is_range_field (const std::string& field) const override {
		return field == "range";
	}

	Range process_range (const std::string& field, const std::string& lower,
			     const std::string& upper) const override {
		return { lower, upper };
	}

	std::vector<std::string>
	process_regex (const std::string& field, const std::regex& rx) const override {
		return {};
	}
}; //Dummy


} // Mux

#endif /* __PROC_IFACE_HH__ */