predicates_core/
reflection.rs
1use std::borrow;
12use std::fmt;
13use std::slice;
14
15pub trait PredicateReflection: fmt::Display {
17 fn parameters<'a>(&'a self) -> Box<dyn Iterator<Item = Parameter<'a>> + 'a> {
19 let params = vec![];
20 Box::new(params.into_iter())
21 }
22
23 fn children<'a>(&'a self) -> Box<dyn Iterator<Item = Child<'a>> + 'a> {
25 let params = vec![];
26 Box::new(params.into_iter())
27 }
28}
29
30pub struct Parameter<'a>(&'a str, &'a dyn fmt::Display);
39
40impl<'a> Parameter<'a> {
41 pub fn new(key: &'a str, value: &'a dyn fmt::Display) -> Self {
43 Self(key, value)
44 }
45
46 pub fn name(&self) -> &str {
48 self.0
49 }
50
51 pub fn value(&self) -> &dyn fmt::Display {
53 self.1
54 }
55}
56
57impl fmt::Display for Parameter<'_> {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 write!(f, "{}: {}", self.0, self.1)
60 }
61}
62
63impl fmt::Debug for Parameter<'_> {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 write!(f, "({:?}, {})", self.0, self.1)
66 }
67}
68
69pub struct Child<'a>(&'a str, &'a dyn PredicateReflection);
71
72impl<'a> Child<'a> {
73 pub fn new(key: &'a str, value: &'a dyn PredicateReflection) -> Self {
75 Self(key, value)
76 }
77
78 pub fn name(&self) -> &str {
80 self.0
81 }
82
83 pub fn value(&self) -> &dyn PredicateReflection {
85 self.1
86 }
87}
88
89impl fmt::Display for Child<'_> {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 write!(f, "{}: {}", self.0, self.1)
92 }
93}
94
95impl fmt::Debug for Child<'_> {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(f, "({:?}, {})", self.0, self.1)
98 }
99}
100
101pub struct Case<'a> {
103 predicate: Option<&'a dyn PredicateReflection>,
104 result: bool,
105 products: Vec<Product>,
106 children: Vec<Case<'a>>,
107}
108
109impl<'a> Case<'a> {
110 pub fn new(predicate: Option<&'a dyn PredicateReflection>, result: bool) -> Self {
112 Self {
113 predicate,
114 result,
115 products: Default::default(),
116 children: Default::default(),
117 }
118 }
119
120 pub fn add_product(mut self, product: Product) -> Self {
122 self.products.push(product);
123 self
124 }
125
126 pub fn add_child(mut self, child: Case<'a>) -> Self {
128 self.children.push(child);
129 self
130 }
131
132 pub fn predicate(&self) -> Option<&dyn PredicateReflection> {
134 self.predicate
135 }
136
137 pub fn result(&self) -> bool {
139 self.result
140 }
141
142 pub fn products(&self) -> CaseProducts<'_> {
144 CaseProducts(self.products.iter())
145 }
146
147 pub fn children(&self) -> CaseChildren<'_> {
149 CaseChildren(self.children.iter())
150 }
151}
152
153impl fmt::Debug for Case<'_> {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 let predicate = if let Some(ref predicate) = self.predicate {
156 format!("Some({})", predicate)
157 } else {
158 "None".to_owned()
159 };
160 f.debug_struct("Case")
161 .field("predicate", &predicate)
162 .field("result", &self.result)
163 .field("products", &self.products)
164 .field("children", &self.children)
165 .finish()
166 }
167}
168
169#[derive(Debug, Clone)]
171pub struct CaseProducts<'a>(slice::Iter<'a, Product>);
172
173impl<'a> Iterator for CaseProducts<'a> {
174 type Item = &'a Product;
175
176 fn next(&mut self) -> Option<&'a Product> {
177 self.0.next()
178 }
179
180 fn size_hint(&self) -> (usize, Option<usize>) {
181 self.0.size_hint()
182 }
183
184 fn count(self) -> usize {
185 self.0.count()
186 }
187}
188
189#[derive(Debug, Clone)]
191pub struct CaseChildren<'a>(slice::Iter<'a, Case<'a>>);
192
193impl<'a> Iterator for CaseChildren<'a> {
194 type Item = &'a Case<'a>;
195
196 fn next(&mut self) -> Option<&'a Case<'a>> {
197 self.0.next()
198 }
199
200 fn size_hint(&self) -> (usize, Option<usize>) {
201 self.0.size_hint()
202 }
203
204 fn count(self) -> usize {
205 self.0.count()
206 }
207}
208
209pub struct Product(borrow::Cow<'static, str>, Box<dyn fmt::Display>);
220
221impl Product {
222 pub fn new<S, D>(key: S, value: D) -> Self
224 where
225 S: Into<borrow::Cow<'static, str>>,
226 D: fmt::Display + 'static,
227 {
228 Self(key.into(), Box::new(value))
229 }
230
231 pub fn name(&self) -> &str {
233 self.0.as_ref()
234 }
235
236 pub fn value(&self) -> &dyn fmt::Display {
238 &self.1
239 }
240}
241
242impl fmt::Display for Product {
243 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
244 write!(f, "{}: {}", self.0, self.1)
245 }
246}
247
248impl fmt::Debug for Product {
249 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250 write!(f, "({:?}, {})", self.0, self.1)
251 }
252}