trojan_rules/lib.rs
1//! Rule-based routing engine for trojan-rs.
2//!
3//! Provides a rule matching engine compatible with Surge rule-set (`.list`)
4//! and Clash rule-provider (YAML) formats. Supports DOMAIN, DOMAIN-SUFFIX,
5//! DOMAIN-KEYWORD, and IP-CIDR rule types with optimized data structures.
6//!
7//! # Architecture
8//!
9//! - **Matchers**: `DomainMatcher` (FxHashSet), `KeywordMatcher` (Aho-Corasick),
10//! `CidrMatcher` (sorted binary search)
11//! - **Parsers**: Surge `.list` and Clash YAML formats
12//! - **Providers**: File-based loading (HTTP in Phase 3)
13//! - **Engine**: `RuleEngine` compiles rule-sets and evaluates rules in order
14//!
15//! # Example
16//!
17//! ```
18//! use trojan_rules::{RuleEngineBuilder, Action};
19//! use trojan_rules::rule::{ParsedRule, MatchContext};
20//! use std::net::{IpAddr, Ipv4Addr};
21//!
22//! let mut builder = RuleEngineBuilder::new();
23//! builder.add_rule_set("ads", vec![
24//! ParsedRule::DomainSuffix("ad.example.com".into()),
25//! ParsedRule::DomainKeyword("tracking".into()),
26//! ]);
27//! builder.add_rule_set_rule("ads", Action::Reject);
28//! builder.set_final(Action::Direct);
29//!
30//! let engine = builder.build().unwrap();
31//!
32//! let ctx = MatchContext {
33//! domain: Some("tracker.ad.example.com"),
34//! dest_ip: None,
35//! dest_port: 443,
36//! src_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
37//! };
38//! assert_eq!(engine.match_request(&ctx), &Action::Reject);
39//! ```
40
41pub mod engine;
42pub mod error;
43#[cfg(feature = "geoip")]
44pub mod geoip_db;
45pub mod matcher;
46pub mod parser;
47pub mod provider;
48pub mod rule;
49
50pub use engine::{HotRuleEngine, MatchDecision, RuleEngine, RuleEngineBuilder};
51pub use error::RulesError;
52pub use rule::Action;