Condition Matcher
A flexible and type-safe condition matching library for Rust with automatic struct field access.
Features
- Automatic struct matching with derive macro
- Multiple matching modes (AND, OR, XOR)
- Support for various condition types (value, length, type, field)
- Numeric comparisons on fields (>, <, >=, <=)
- String operations (contains, starts_with, ends_with)
- Regex matching (optional feature)
- NOT operator for negating conditions
- Optional field handling (Option support)
- Detailed match results with error information
- Builder pattern for ergonomic API
- Serde support (optional feature)
- Zero-cost abstractions with compile-time type safety
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
# Optional features
= { = "0.1.0", = ["serde", "regex"] }
# Or all features
= { = "0.1.0", = ["full"] }
Quick Start
use ;
// Simply derive Matchable to get automatic field access!
let user = User ;
// Create a matcher with AND mode
let mut matcher = new;
matcher
.add_condition
.add_condition;
assert!;
Builder API
For a more ergonomic experience, use the builder pattern:
use ;
let matcher = new
.mode
.length_gte
.value_not_equals
.build;
assert!;
Or use the field condition builder:
use ;
let condition = .gte;
let mut matcher = new;
matcher.add_condition;
Matching Modes
AND Mode
All conditions must match:
let mut matcher = new;
OR Mode
At least one condition must match:
let mut matcher = new;
XOR Mode
Exactly one condition must match:
let mut matcher = new;
Condition Types
Value Matching
Condition
Length Matching
Condition
Field Value Matching
Condition
NOT Operator
let inner = Condition ;
Condition
Supported Operators
| Operator | Description | Works With |
|---|---|---|
Equals |
Exact equality | All types |
NotEquals |
Inequality | All types |
GreaterThan |
Greater than | Numeric types, strings |
LessThan |
Less than | Numeric types, strings |
GreaterThanOrEqual |
Greater or equal | Numeric types, strings |
LessThanOrEqual |
Less or equal | Numeric types, strings |
Contains |
Contains substring | Strings |
NotContains |
Does not contain | Strings |
StartsWith |
Starts with prefix | Strings |
EndsWith |
Ends with suffix | Strings |
Regex |
Matches regex pattern | Strings (requires regex feature) |
IsEmpty |
Check if empty | Strings, collections |
IsNotEmpty |
Check if not empty | Strings, collections |
IsNone |
Check if Option is None | Option types |
IsSome |
Check if Option is Some | Option types |
Supported Types
The matcher automatically supports comparison for:
- Integers:
i8,i16,i32,i64,i128,isize - Unsigned:
u8,u16,u32,u64,u128,usize - Floats:
f32,f64 - Other:
bool,char,String,&str
Detailed Results
Get detailed information about why a match succeeded or failed:
let result = matcher.run_detailed.unwrap;
println!;
println!;
println!;
for condition in result.condition_results
Error Handling
The library provides detailed error information:
use MatchError;
match matcher.run
Optional Features
Serde Support
Enable serialization/deserialization of operators and modes:
= { = "0.1.0", = ["serde"] }
Regex Support
Enable regex pattern matching:
= { = "0.1.0", = ["regex"] }
Condition
All Features
= { = "0.1.0", = ["full"] }
Custom Types
To make your custom type matchable, simply derive Matchable:
The derive macro automatically:
- Implements field access for all named fields
- Handles
Option<T>fields by unwrapping when present - Returns
Nonefor missing optional fields
Examples
Run the examples to see the library in action:
License
MIT OR Apache-2.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.