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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use std::borrow;
use std::fmt;
use std::slice;
pub trait PredicateReflection: fmt::Display {
fn parameters<'a>(&'a self) -> Box<dyn Iterator<Item = Parameter<'a>> + 'a> {
let params = vec![];
Box::new(params.into_iter())
}
fn children<'a>(&'a self) -> Box<dyn Iterator<Item = Child<'a>> + 'a> {
let params = vec![];
Box::new(params.into_iter())
}
}
pub struct Parameter<'a>(&'a str, &'a dyn fmt::Display);
impl<'a> Parameter<'a> {
pub fn new(key: &'a str, value: &'a dyn fmt::Display) -> Self {
Self(key, value)
}
pub fn name(&self) -> &str {
self.0
}
pub fn value(&self) -> &dyn fmt::Display {
self.1
}
}
impl<'a> fmt::Display for Parameter<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.0, self.1)
}
}
impl<'a> fmt::Debug for Parameter<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?}, {})", self.0, self.1)
}
}
pub struct Child<'a>(&'a str, &'a dyn PredicateReflection);
impl<'a> Child<'a> {
pub fn new(key: &'a str, value: &'a dyn PredicateReflection) -> Self {
Self(key, value)
}
pub fn name(&self) -> &str {
self.0
}
pub fn value(&self) -> &dyn PredicateReflection {
self.1
}
}
impl<'a> fmt::Display for Child<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.0, self.1)
}
}
impl<'a> fmt::Debug for Child<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?}, {})", self.0, self.1)
}
}
pub struct Case<'a> {
predicate: Option<&'a dyn PredicateReflection>,
result: bool,
products: Vec<Product>,
children: Vec<Case<'a>>,
}
impl<'a> Case<'a> {
pub fn new(predicate: Option<&'a dyn PredicateReflection>, result: bool) -> Self {
Self {
predicate,
result,
products: Default::default(),
children: Default::default(),
}
}
pub fn add_product(mut self, product: Product) -> Self {
self.products.push(product);
self
}
pub fn add_child(mut self, child: Case<'a>) -> Self {
self.children.push(child);
self
}
pub fn predicate(&self) -> Option<&dyn PredicateReflection> {
self.predicate
}
pub fn result(&self) -> bool {
self.result
}
pub fn products(&self) -> CaseProducts<'_> {
CaseProducts(self.products.iter())
}
pub fn children(&self) -> CaseChildren<'_> {
CaseChildren(self.children.iter())
}
}
impl<'a> fmt::Debug for Case<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let predicate = if let Some(ref predicate) = self.predicate {
format!("Some({})", predicate)
} else {
"None".to_owned()
};
f.debug_struct("Case")
.field("predicate", &predicate)
.field("result", &self.result)
.field("products", &self.products)
.field("children", &self.children)
.finish()
}
}
#[derive(Debug, Clone)]
pub struct CaseProducts<'a>(slice::Iter<'a, Product>);
impl<'a> Iterator for CaseProducts<'a> {
type Item = &'a Product;
fn next(&mut self) -> Option<&'a Product> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn count(self) -> usize {
self.0.count()
}
}
#[derive(Debug, Clone)]
pub struct CaseChildren<'a>(slice::Iter<'a, Case<'a>>);
impl<'a> Iterator for CaseChildren<'a> {
type Item = &'a Case<'a>;
fn next(&mut self) -> Option<&'a Case<'a>> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
fn count(self) -> usize {
self.0.count()
}
}
pub struct Product(borrow::Cow<'static, str>, Box<dyn fmt::Display>);
impl Product {
pub fn new<S, D>(key: S, value: D) -> Self
where
S: Into<borrow::Cow<'static, str>>,
D: fmt::Display + 'static,
{
Self(key.into(), Box::new(value))
}
pub fn name(&self) -> &str {
self.0.as_ref()
}
pub fn value(&self) -> &dyn fmt::Display {
&self.1
}
}
impl fmt::Display for Product {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.0, self.1)
}
}
impl fmt::Debug for Product {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:?}, {})", self.0, self.1)
}
}