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
//! # rule_kit
//!
//! **A blazing-fast, composable, and DSL-friendly rule engine kit for Rust.**
//!
//! > Define your rules. Plug your context (mutable or immutable). Let the engine do the rest.
//!
//! ---
//!
//! ## ✨ Features
//!
//! - Minimal core: no assumptions, no boilerplate
//! - Pluggable rules: implement `Rule<T>` for any context (mutable support)
//! - DSL-friendly: support JSON/YAML/Struct-based rules
//! - Built for scale: evaluate hundreds of rules with ease
//! - Supports mutable context during rule application for stateful workflows
//!
//! ---
//!
//! ## 🚀 Quick Start
//!
//! Define a context (e.g., a struct), implement `Rule<T>` with mutable `apply`, and plug it into `RuleEngine`:
//!
//! ```rust
//! use rule_kit::{Rule, RuleEngine};
//! use rule_kit::builder::RuleEngineBuilder;
//! use rule_kit::utils::PriorityOrder;
//!
//! #[derive(Debug)]
//! struct Order {
//! pub total: f64,
//! pub discount: f64,
//! }
//!
//! #[derive(Debug, Clone)]
//! enum OrderRule {
//! DiscountIfHighValue,
//! }
//!
//! impl Rule<Order> for OrderRule {
//! type RuleError = ();
//!
//! fn name(&self) -> &str {
//! match self {
//! OrderRule::DiscountIfHighValue => "DiscountIfHighValue",
//! }
//! }
//!
//! fn priority(&self) -> u32 {
//! 1
//! }
//!
//! fn evaluate(&self, ctx: &Order) -> Result<bool, Self::RuleError> {
//! match self {
//! OrderRule::DiscountIfHighValue => Ok(ctx.total > 100.0),
//! }
//! }
//!
//! /// Note: `apply` takes `&mut self` and `&mut ctx`, allowing rule and context mutation.
//! fn apply(&mut self, ctx: &mut Order) -> Result<(), Self::RuleError> {
//! match self {
//! OrderRule::DiscountIfHighValue => {
//! let discount = ctx.total * 0.10;
//! ctx.discount += discount;
//! Ok(())
//! }
//! }
//! }
//!
//! fn before_apply(&self, ctx: &Order) {
//! println!("Checking order total: {}", ctx.total);
//! }
//!
//! fn after_apply(&self, ctx: &Order) {
//! println!("Applied discount, new total discount: {}", ctx.discount);
//! }
//! }
//!
//! // fn main() {
//! let mut order = Order {
//! total: 150.0,
//! discount: 0.0,
//! };
//!
//! let rules = vec![OrderRule::DiscountIfHighValue];
//!
//! // Using RuleEngine directly; pass mutable reference to context
//! let mut engine = RuleEngine::new(rules.clone(), None);
//! engine.evaluate_all(&mut order).unwrap();
//! println!("Discount after RuleEngine: {:.2}", order.discount);
//!
//! // Using builder (with priority); also requires mutable context
//! let mut order2 = Order {
//! total: 150.0,
//! discount: 0.0,
//! };
//!
//! let mut engine_built = RuleEngineBuilder::new()
//! .with_rules(rules)
//! .priority_asc()
//! .build();
//!
//! engine_built.evaluate_all(&mut order2).unwrap();
//! println!("Discount after RuleEngineBuilder: {:.2}", order2.discount);
//! // }
//! ```
//!
//! ---
//!
//! ## 📦 Design Philosophy
//!
//! `rule_kit` is designed to be:
//!
//! - **Composable** — add your own rule logic without modifying the engine
//! - **Extensible** — supports rule metadata, DSL parsing, logging, etc.
//! - **Performant** — built with scaling in mind (Rayon-friendly)
//! - **Flexible** — supports mutable context in rules for stateful business logic
//!
//! You implement the `Rule<T>` trait for your domain, where `apply` can mutate both the rule instance and the context, enabling advanced scenarios like workflow progression, state updates, or side effects.
//!
//! ---
//!
//! ## 📜 License
//!
//! Licensed under:
//! - Apache License, Version 2.0 [LICENSE](http://www.apache.org/licenses/LICENSE-2.0.txt)
//!
//! ---
//!
//! ## 🧑💻 Author
//!
//! Created and maintained by [Jerry Maheswara](https://github.com/jerry-maheswara-github)
//!
//! Feel free to reach out for suggestions, issues, or improvements!
//!
//! ---
//!
//! ## ❤️ Built with Love in Rust
//!
//! This project is built with ❤️ using **Rust** — a systems programming language that is safe, fast, and concurrent. Rust is the perfect choice for building reliable and efficient applications.
//!
//! ---
//!
//! ## 👋 Contributing
//!
//! Pull requests, issues, and feedback are welcome!
//! If you find this crate useful, give it a ⭐ and share it with others in the Rust community.
//!
//! ---
/// Core rule evaluation engine that runs rules based on context and priority.
/// Defines the `Rule` trait and any related rule abstractions.
/// Contains error types used throughout the rule engine, including [`error::RuleError`] and [`error::RuleEngineError`].
/// Provides the builder pattern for constructing a [`RuleEngine`] instance with fluent configuration.
/// Utility enums or structs used across the crate, such as [`PriorityOrder`].
// Public re-exports
pub use Rule;
pub use RuleEngine;
pub use RuleEngineBuilder;
pub use PriorityOrder;