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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! # whereexpr
//!
//! A library for building and evaluating **type-safe, compiled boolean filter
//! expressions** over arbitrary Rust structs.
//!
//! Expressions are written as human-readable strings — e.g.
//! `"age > 30 && name is-one-of [Alice, Bob]"` — parsed once at build time,
//! and then evaluated at zero-allocation cost against any number of objects.
//!
//! ---
//!
//! ## Quick start
//!
//! ### 1. Implement [`Attributes`] for your type
//!
//! Declare one [`AttributeIndex`] constant per field and implement the three
//! trait methods:
//!
//! ```rust
//! use whereexpr::{Attributes, AttributeIndex, Value, ValueKind};
//!
//! struct Person {
//! name: String,
//! age: u32,
//! }
//!
//! impl Person {
//! const NAME: AttributeIndex = AttributeIndex::new(0);
//! const AGE: AttributeIndex = AttributeIndex::new(1);
//! }
//!
//! impl Attributes for Person {
//! const TYPE_ID: u64 = 0x517652f2; // unique ID for Person type (a hash or other unique identifier)
//! const TYPE_NAME: &'static str = "Person";
//! fn get(&self, idx: AttributeIndex) -> Option<Value<'_>> {
//! match idx {
//! Self::NAME => Some(Value::String(&self.name)),
//! Self::AGE => Some(Value::U32(self.age)),
//! _ => None,
//! }
//! }
//! fn kind(idx: AttributeIndex) -> Option<ValueKind> {
//! match idx {
//! Self::NAME => Some(ValueKind::String),
//! Self::AGE => Some(ValueKind::U32),
//! _ => None,
//! }
//! }
//! fn index(name: &str) -> Option<AttributeIndex> {
//! match name {
//! "name" => Some(Self::NAME),
//! "age" => Some(Self::AGE),
//! _ => None,
//! }
//! }
//! }
//! ```
//!
//! ### 2. Build an expression
//!
//! Use [`ExpressionBuilder`] to register named conditions, then compile them
//! into a reusable [`Expression`] with a boolean expression string:
//!
//! ```rust
//! # use whereexpr::*;
//! # struct Person { name: String, age: u32 }
//! # impl Person {
//! # const NAME: AttributeIndex = AttributeIndex::new(0);
//! # const AGE: AttributeIndex = AttributeIndex::new(1);
//! # }
//! # impl Attributes for Person {
//! # const TYPE_ID: u64 = 0x517652f2; // unique ID for Person type (a hash or other unique identifier)
//! # const TYPE_NAME: &'static str = "Person";
//! # fn get(&self, idx: AttributeIndex) -> Option<Value<'_>> {
//! # match idx { Self::NAME => Some(Value::String(&self.name)), Self::AGE => Some(Value::U32(self.age)), _ => None }
//! # }
//! # fn kind(idx: AttributeIndex) -> Option<ValueKind> {
//! # match idx { Self::NAME => Some(ValueKind::String), Self::AGE => Some(ValueKind::U32), _ => None }
//! # }
//! # fn index(name: &str) -> Option<AttributeIndex> {
//! # match name { "name" => Some(Self::NAME), "age" => Some(Self::AGE), _ => None }
//! # }
//! # }
//! let expr = ExpressionBuilder::<Person>::new()
//! .add("has_name", Condition::from_str("name is-one-of [Alice, Bob] {ignore-case}"))
//! .add("is_adult", Condition::from_str("age >= 18"))
//! .build("has_name && is_adult")
//! .unwrap();
//! ```
//!
//! ### 3. Evaluate
//!
//! Call [`Expression::matches`] to test any object:
//!
//! ```rust
//! # use whereexpr::*;
//! # struct Person { name: String, age: u32 }
//! # impl Person {
//! # const NAME: AttributeIndex = AttributeIndex::new(0);
//! # const AGE: AttributeIndex = AttributeIndex::new(1);
//! # }
//! # impl Attributes for Person {
//! # const TYPE_ID: u64 = 0x517652f2; // unique ID for Person type (a hash or other unique identifier)
//! # const TYPE_NAME: &'static str = "Person";
//! # fn get(&self, idx: AttributeIndex) -> Option<Value<'_>> {
//! # match idx { Self::NAME => Some(Value::String(&self.name)), Self::AGE => Some(Value::U32(self.age)), _ => None }
//! # }
//! # fn kind(idx: AttributeIndex) -> Option<ValueKind> {
//! # match idx { Self::NAME => Some(ValueKind::String), Self::AGE => Some(ValueKind::U32), _ => None }
//! # }
//! # fn index(name: &str) -> Option<AttributeIndex> {
//! # match name { "name" => Some(Self::NAME), "age" => Some(Self::AGE), _ => None }
//! # }
//! # }
//! # let expr = ExpressionBuilder::<Person>::new()
//! # .add("has_name", Condition::from_str("name is-one-of [Alice, Bob] {ignore-case}"))
//! # .add("is_adult", Condition::from_str("age >= 18"))
//! # .build("has_name && is_adult")
//! # .unwrap();
//! let people = vec![
//! Person { name: "Alice".into(), age: 30 },
//! Person { name: "Charlie".into(), age: 25 },
//! ];
//!
//! let matches: Vec<_> = people.iter().filter(|p| expr.matches(*p)).collect();
//! // → only Alice
//! ```
//!
//! ---
//!
//! ## Core concepts
//!
//! | Type | Role |
//! |---|---|
//! | [`Attributes`] | Trait your struct implements to expose its fields |
//! | [`AttributeIndex`] | Opaque index that identifies a field |
//! | [`Value`] | The runtime value of a field (tagged union) |
//! | [`ValueKind`] | The type tag of a field (no data) |
//! | [`Condition`] | Maps one attribute to a [`Predicate`] |
//! | [`Predicate`] | A compiled single-field test (operation + reference value) |
//! | [`Operation`] | The comparison operator (`is`, `>`, `contains`, `glob`, …) |
//! | [`ExpressionBuilder`] | Fluent builder: registers conditions and compiles the boolean expression |
//! | [`Expression`] | The compiled, reusable filter — call `.matches(&obj)` to evaluate |
//! | [`Error`] | All errors that can occur during building or predicate construction |
//!
//! ---
//!
//! ## Condition string syntax
//!
//! A condition string has the form:
//!
//! ```text
//! <attribute> <operation> <value> [<modifiers>]
//! ```
//!
//! Single value:
//!
//! ```text
//! age > 30
//! name is Alice
//! status is-not deleted
//! filename ends-with .log
//! path glob /var/log/**/*.log
//! ```
//!
//! List value (one or more comma-separated entries in `[ ]`):
//!
//! ```text
//! status is-one-of [active, pending, paused]
//! extension ends-with-one-of [.jpg, .jpeg, .png]
//! message contains-one-of [error, fatal, critical]
//! ```
//!
//! Range (exactly two values in `[ ]`):
//!
//! ```text
//! age in-range [18, 65]
//! score not-in-range [0, 10]
//! port in-range [1024, 65535]
//! ```
//!
//! Modifiers (appended in `{ }`):
//!
//! ```text
//! name is-one-of [alice, bob] {ignore-case}
//! path starts-with /Home {ignore-case}
//! ```
//!
//! ---
//!
//! ## Boolean expression syntax
//!
//! The string passed to [`ExpressionBuilder::build`] combines named conditions
//! using standard boolean operators:
//!
//! | Operator | Aliases | Example |
//! |---|---|---|
//! | AND | `&&`, `AND` | `cond_a && cond_b` |
//! | OR | `\|\|`, `OR` | `cond_a \|\| cond_b` |
//! | NOT | `!`, `NOT` | `!cond_a` |
//! | Grouping | `( )` | `(cond_a \|\| cond_b) && cond_c` |
//!
//! `&&` and `||` **cannot be mixed** at the same nesting level without
//! parentheses — this is intentional to avoid precedence ambiguity:
//!
//! ```text
//! // ✗ error: mixed operators
//! cond_a && cond_b || cond_c
//!
//! // ✓ ok: grouped explicitly
//! (cond_a && cond_b) || cond_c
//! cond_a && (cond_b || cond_c)
//! ```
//!
//! ---
//!
//! ## Available operations
//!
//! See [`Operation`] for the full list with per-variant aliases and examples.
//!
//! | Family | Operations |
//! |---|---|
//! | Equality | `is`, `is-not` |
//! | Membership | `is-one-of`, `is-not-one-of` |
//! | String prefix | `starts-with`, `not-starts-with`, `starts-with-one-of`, `not-starts-with-one-of` |
//! | String suffix | `ends-with`, `not-ends-with`, `ends-with-one-of`, `not-ends-with-one-of` |
//! | Substring | `contains`, `not-contains`, `contains-one-of`, `not-contains-one-of` |
//! | Glob pattern | `glob`, `not-glob` |
//! | Numeric | `>`, `>=`, `<`, `<=`, `in-range`, `not-in-range` |
//!
//! ---
//!
//! ## Supported value types
//!
//! See [`Value`] and [`ValueKind`] for details. In brief:
//! `String`, `Path`, `u8`–`u64`, `i8`–`i64`, `f32`, `f64`,
//! `Hash128`, `Hash160`, `Hash256`, `IpAddr`, `DateTime` (Unix timestamp),
//! `Bool`.
//!
//! ---
//!
//! ## Feature flags
//!
//! | Flag | Effect |
//! |---|---|
//! | `error_description` | Implements [`std::fmt::Display`] for [`Error`], providing annotated error messages with `^` underlines pointing at the problematic token. |
//! | `enable_type_check` | Enables type checking at runtime. This is useful for debugging and for ensuring that the correct type is used when evaluating an expression. |
pub use CompiledCondition;
pub use Condition;
pub use ConditionAttribute;
pub use ConditionPredicate;
pub use ConditionList;
pub use Error;
pub use Expression;
pub use ExpressionBuilder;
pub use Operation;
pub use Predicate;
pub use AttributeIndex;
pub use Attributes;
pub use Value;
pub use ValueKind;