condition_matcher/
lib.rs

1//! # Condition Matcher
2//!
3//! A flexible and type-safe condition matching library for Rust with automatic struct field access.
4//!
5//! ## Features
6//!
7//! - **Automatic struct matching** with derive macro
8//! - Multiple matching modes (AND, OR, XOR)
9//! - Support for various condition types (value, length, type, field)
10//! - String operations (contains, starts_with, ends_with)
11//! - Numeric comparisons on fields
12//! - Detailed match results with error information
13//! - Builder pattern for ergonomic API
14//! - Optional serde, regex, and parallel processing support
15//!
16//! ## Quick Start
17//!
18//! ```rust
19//! use condition_matcher::{RuleMatcher, MatcherMode, Condition, ConditionSelector, ConditionOperator, MatchableDerive, Matcher, Matchable, MatcherBuilder};
20//!
21//! #[derive(MatchableDerive, PartialEq, Debug)]
22//! struct User {
23//!     name: String,
24//!     age: u32,
25//! }
26//!
27//! let user = User { name: "Alice".to_string(), age: 30 };
28//!
29//! let mut matcher = MatcherBuilder::<User>::new()
30//!     .mode(MatcherMode::AND)
31//!     .value_equals(User { name: "Alice".to_string(), age: 30 })
32//!     .build();
33//! matcher.add_condition(Condition {
34//!     selector: ConditionSelector::FieldValue("age", &18u32),
35//!     operator: ConditionOperator::GreaterThanOrEqual,
36//! });
37//!
38//! assert!(matcher.matches(&user));
39//! ```
40//!
41//! ## Builder API
42//!
43//! ```rust
44//! use condition_matcher::{MatcherBuilder, MatcherMode, Matcher};
45//!
46//! let matcher = MatcherBuilder::<&str>::new()
47//!     .mode(MatcherMode::AND)
48//!     .length_gte(4)
49//!     .value_not_equals("bad")
50//!     .build();
51//!
52//! assert!(matcher.matches(&"good"));
53//! ```
54//!
55//! ## Batch Operations
56//!
57//! ```rust,ignore
58//! use condition_matcher::{batch, JsonMatcher, Matcher};
59//!
60//! let matchers: Vec<JsonMatcher> = load_from_database()?;
61//! let records: Vec<Record> = get_records();
62//!
63//! // Find which matchers apply to a single record
64//! let applicable = batch::matching(&records[0], &matchers);
65//!
66//! // Evaluate all combinations (with parallel feature)
67//! let all_matches = batch::parallel::evaluate_matrix(&records, &matchers);
68//! ```
69
70// Core modules
71mod traits;
72mod condition;
73mod matchable;
74mod matchers;
75mod evaluators;
76mod result;
77mod error;
78
79/// Builder module for creating matchers.
80pub mod builder;
81
82// Batch operations module
83pub mod batch;
84
85// Keep old matcher module for backwards compatibility
86mod matcher;
87
88#[cfg(test)]
89mod test;
90
91// ============================================================================
92// Core Traits
93// ============================================================================
94
95pub use traits::{Evaluate, Matcher, MatcherExt, Predicate};
96
97// ============================================================================
98// Matchers
99// ============================================================================
100
101pub use matchers::RuleMatcher;
102
103#[cfg(feature = "json_condition")]
104pub use matchers::JsonMatcher;
105
106// ============================================================================
107// Conditions
108// ============================================================================
109
110pub use condition::{
111    Condition, ConditionMode, ConditionOperator, ConditionSelector, NestedCondition,
112};
113
114#[cfg(feature = "json_condition")]
115pub use condition::{JsonCondition, JsonNestedCondition};
116
117// ============================================================================
118// Builder
119// ============================================================================
120
121pub use builder::{field, FieldConditionBuilder, MatcherBuilder};
122
123// ============================================================================
124// Results and Errors
125// ============================================================================
126
127pub use result::{ConditionResult, MatchResult};
128
129#[cfg(feature = "json_condition")]
130pub use result::{JsonConditionResult, JsonEvalResult};
131
132pub use error::MatchError;
133
134// ============================================================================
135// Data Access
136// ============================================================================
137
138pub use matchable::Matchable;
139
140// ============================================================================
141// Derive Macro
142// ============================================================================
143
144pub use condition_matcher_derive::Matchable as MatchableDerive;
145
146// ============================================================================
147// Legacy Aliases (backwards compatibility)
148// ============================================================================
149
150/// Alias for [`ConditionMode`] for backwards compatibility.
151pub type MatcherMode = ConditionMode;
152
153// Re-export the old Matcher struct for backwards compatibility
154// Users should migrate to RuleMatcher
155#[doc(hidden)]
156pub use matcher::Matcher as OldMatcher;
157
158// Re-export evaluate_json_condition for backwards compatibility
159#[cfg(feature = "json_condition")]
160pub use matcher::evaluate_json_condition;