limiteron 0.2.4

Unified flow control framework for Rust
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! 规则匹配引擎
//!
//! 包含规则匹配相关的类型:MatchCondition, IpRange, LogicalOperator,
//! CompositeCondition, Rule, RuleMatcher, ConditionEvaluator

use super::traits::RequestContext;
use crate::config::ConfigMatcher;
use crate::error::LimiteronError;
use parking_lot::RwLock;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use std::sync::Arc;
use std::time::Instant;

// ============================================================================
// 匹配条件
// ============================================================================

/// 匹配条件
///
/// 定义单个匹配条件。
#[derive(Clone)]
pub enum MatchCondition {
    /// 用户ID匹配
    User(Vec<String>),
    /// IP范围匹配
    Ip(Vec<IpRange>),
    /// 地理位置匹配
    Geo(Vec<String>),
    /// API版本匹配
    ApiVersion(Vec<String>),
    /// 设备类型匹配
    Device(Vec<String>),
    /// 自定义匹配
    Custom(Arc<dyn Fn(&RequestContext) -> bool + Send + Sync>),
}

impl std::fmt::Debug for MatchCondition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MatchCondition::User(ids) => f.debug_tuple("User").field(ids).finish(),
            MatchCondition::Ip(ranges) => f.debug_tuple("Ip").field(&ranges.len()).finish(),
            MatchCondition::Geo(countries) => f.debug_tuple("Geo").field(countries).finish(),
            MatchCondition::ApiVersion(versions) => {
                f.debug_tuple("ApiVersion").field(versions).finish()
            }
            MatchCondition::Device(device_types) => {
                f.debug_tuple("Device").field(device_types).finish()
            }
            MatchCondition::Custom(_) => f.debug_tuple("Custom").field(&"<closure>").finish(),
        }
    }
}

// ============================================================================
// IP范围
// ============================================================================

/// IP范围
#[derive(Debug, Clone)]
pub enum IpRange {
    /// 单个IP
    Single(IpAddr),
    /// IPv4 CIDR
    Ipv4Cidr { addr: Ipv4Addr, prefix: u8 },
    /// IPv6 CIDR
    Ipv6Cidr { addr: Ipv6Addr, prefix: u8 },
    /// IPv4范围
    Ipv4Range { start: Ipv4Addr, end: Ipv4Addr },
}

impl IpRange {
    /// 检查IP是否在范围内
    pub fn contains(&self, ip: &IpAddr) -> bool {
        match self {
            IpRange::Single(addr) => addr == ip,
            IpRange::Ipv4Cidr { addr, prefix } => {
                if let IpAddr::V4(ipv4) = ip {
                    self.ipv4_in_cidr(ipv4, addr, *prefix)
                } else {
                    false
                }
            }
            IpRange::Ipv6Cidr { addr, prefix } => {
                if let IpAddr::V6(ipv6) = ip {
                    self.ipv6_in_cidr(ipv6, addr, *prefix)
                } else {
                    false
                }
            }
            IpRange::Ipv4Range { start, end } => {
                if let IpAddr::V4(ipv4) = ip {
                    ipv4 >= start && ipv4 <= end
                } else {
                    false
                }
            }
        }
    }

    /// 检查IPv4是否在CIDR范围内
    fn ipv4_in_cidr(&self, ip: &Ipv4Addr, network: &Ipv4Addr, prefix: u8) -> bool {
        let ip_u32 = u32::from(*ip);
        let network_u32 = u32::from(*network);
        let mask = if prefix == 0 {
            0
        } else {
            0xFFFFFFFF << (32 - prefix)
        };

        (ip_u32 & mask) == (network_u32 & mask)
    }

    /// 检查IPv6是否在CIDR范围内
    fn ipv6_in_cidr(&self, ip: &Ipv6Addr, network: &Ipv6Addr, prefix: u8) -> bool {
        let ip_segments = ip.segments();
        let network_segments = network.segments();

        let full_segments = (prefix / 16) as usize;
        let remaining_bits = prefix % 16;

        // 检查完整的段
        for i in 0..full_segments {
            if ip_segments[i] != network_segments[i] {
                return false;
            }
        }

        // 检查剩余的位
        if remaining_bits > 0 && full_segments < 8 {
            let mask = 0xFFFFu16 << (16 - remaining_bits);
            if (ip_segments[full_segments] & mask) != (network_segments[full_segments] & mask) {
                return false;
            }
        }

        true
    }
}

impl FromStr for IpRange {
    type Err = LimiteronError;

    /// 从字符串解析IP范围
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.contains('/') {
            // CIDR格式
            let parts: Vec<&str> = s.split('/').collect();
            if parts.len() != 2 {
                return Err(LimiteronError::ConfigError(format!(
                    "无效的CIDR格式: {}",
                    s
                )));
            }

            let addr: IpAddr = parts[0]
                .parse()
                .map_err(|_| LimiteronError::ConfigError(format!("无效的IP地址: {}", parts[0])))?;
            let prefix: u8 = parts[1]
                .parse()
                .map_err(|_| LimiteronError::ConfigError(format!("无效的前缀: {}", parts[1])))?;

            match addr {
                IpAddr::V4(ipv4) => {
                    if prefix > 32 {
                        return Err(LimiteronError::ConfigError(format!(
                            "IPv4前缀不能超过32: {}",
                            prefix
                        )));
                    }
                    Ok(IpRange::Ipv4Cidr { addr: ipv4, prefix })
                }
                IpAddr::V6(ipv6) => {
                    if prefix > 128 {
                        return Err(LimiteronError::ConfigError(format!(
                            "IPv6前缀不能超过128: {}",
                            prefix
                        )));
                    }
                    Ok(IpRange::Ipv6Cidr { addr: ipv6, prefix })
                }
            }
        } else if s.contains('-') {
            // 范围格式
            let parts: Vec<&str> = s.split('-').collect();
            if parts.len() != 2 {
                return Err(LimiteronError::ConfigError(format!(
                    "无效的IP范围格式: {}",
                    s
                )));
            }

            let start: Ipv4Addr = parts[0]
                .parse()
                .map_err(|_| LimiteronError::ConfigError(format!("无效的起始IP: {}", parts[0])))?;
            let end: Ipv4Addr = parts[1]
                .parse()
                .map_err(|_| LimiteronError::ConfigError(format!("无效的结束IP: {}", parts[1])))?;

            if start > end {
                return Err(LimiteronError::ConfigError(format!(
                    "起始IP不能大于结束IP: {} - {}",
                    parts[0], parts[1]
                )));
            }

            Ok(IpRange::Ipv4Range { start, end })
        } else {
            // 单个IP
            let addr: IpAddr = s
                .parse()
                .map_err(|_| LimiteronError::ConfigError(format!("无效的IP地址: {}", s)))?;
            Ok(IpRange::Single(addr))
        }
    }
}

// ============================================================================
// 逻辑操作符
// ============================================================================

/// 逻辑操作符
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogicalOperator {
    /// 逻辑与
    And,
    /// 逻辑或
    Or,
    /// 逻辑非
    Not,
}

// ============================================================================
// 复合条件
// ============================================================================

/// 复合条件
///
/// 支持AND/OR/NOT逻辑操作。
pub struct CompositeCondition {
    /// 子条件列表
    pub conditions: Vec<Box<dyn ConditionEvaluator>>,
    /// 逻辑操作符
    pub operator: LogicalOperator,
}

impl std::fmt::Debug for CompositeCondition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CompositeCondition")
            .field("conditions", &self.conditions.len())
            .field("operator", &self.operator)
            .finish()
    }
}

impl Clone for CompositeCondition {
    fn clone(&self) -> Self {
        // 由于 ConditionEvaluator 不能 Clone,我们创建一个新的 CompositeCondition
        // 实际使用时,用户需要重新构建条件
        Self {
            conditions: self
                .conditions
                .iter()
                .map(|_| {
                    // 由于无法克隆 trait 对象,这里返回一个占位符
                    // 实际使用中,需要重新构建条件
                    Box::new(MatchCondition::User(vec![])) as Box<dyn ConditionEvaluator>
                })
                .collect(),
            operator: self.operator,
        }
    }
}

// ============================================================================
// 条件评估器
// ============================================================================

/// 条件评估器 trait
///
/// 所有条件都需要实现此trait。
pub trait ConditionEvaluator: Send + Sync {
    /// 评估条件
    fn evaluate(&self, context: &RequestContext) -> bool;

    /// 获取条件描述
    fn description(&self) -> String;
}

impl ConditionEvaluator for MatchCondition {
    fn evaluate(&self, context: &RequestContext) -> bool {
        match self {
            MatchCondition::User(user_ids) => {
                if let Some(user_id) = context.get_header("X-User-Id") {
                    user_ids.contains(&user_id.to_string()) || user_ids.contains(&"*".to_string())
                } else {
                    user_ids.contains(&"*".to_string())
                }
            }
            MatchCondition::Ip(ip_ranges) => {
                if let Some(client_ip) = &context.client_ip {
                    if let Ok(ip) = client_ip.parse::<IpAddr>() {
                        return ip_ranges.iter().any(|range| range.contains(&ip));
                    }
                }
                false
            }
            MatchCondition::Geo(countries) => {
                if let Some(country) = context.get_header("X-Country") {
                    countries.contains(&country.to_string()) || countries.contains(&"*".to_string())
                } else {
                    countries.contains(&"*".to_string())
                }
            }
            MatchCondition::ApiVersion(versions) => {
                if let Some(version) = context.get_header("X-API-Version") {
                    versions.contains(&version.to_string()) || versions.contains(&"*".to_string())
                } else {
                    versions.contains(&"*".to_string())
                }
            }
            MatchCondition::Device(device_types) => {
                if let Some(device_type) = context.get_header("X-Device-Type") {
                    device_types.contains(&device_type.to_string())
                        || device_types.contains(&"*".to_string())
                } else {
                    device_types.contains(&"*".to_string())
                }
            }
            MatchCondition::Custom(eval_fn) => eval_fn(context),
        }
    }

    fn description(&self) -> String {
        match self {
            MatchCondition::User(ids) => format!("User in {:?}", ids),
            MatchCondition::Ip(ranges) => format!("IP in {} ranges", ranges.len()),
            MatchCondition::Geo(countries) => format!("Country in {:?}", countries),
            MatchCondition::ApiVersion(versions) => format!("API version in {:?}", versions),
            MatchCondition::Device(device_types) => format!("Device type in {:?}", device_types),
            MatchCondition::Custom(_) => "Custom condition".to_string(),
        }
    }
}

impl ConditionEvaluator for CompositeCondition {
    fn evaluate(&self, context: &RequestContext) -> bool {
        match self.operator {
            LogicalOperator::And => self.conditions.iter().all(|c| c.evaluate(context)),
            LogicalOperator::Or => self.conditions.iter().any(|c| c.evaluate(context)),
            LogicalOperator::Not => {
                // NOT操作符只应该有一个子条件
                self.conditions
                    .first()
                    .is_some_and(|c| !c.evaluate(context))
            }
        }
    }

    fn description(&self) -> String {
        let op_str = match self.operator {
            LogicalOperator::And => "AND",
            LogicalOperator::Or => "OR",
            LogicalOperator::Not => "NOT",
        };
        format!("{} ({})", op_str, self.conditions.len())
    }
}

// ============================================================================
// 规则
// ============================================================================

/// 规则
pub struct Rule {
    /// 规则ID
    pub id: String,
    /// 规则名称
    pub name: String,
    /// 优先级(数值越大优先级越高)
    pub priority: u16,
    /// 匹配条件
    pub condition: Box<dyn ConditionEvaluator>,
    /// 是否启用
    pub enabled: bool,
}

impl std::fmt::Debug for Rule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Rule")
            .field("id", &self.id)
            .field("name", &self.name)
            .field("priority", &self.priority)
            .field("enabled", &self.enabled)
            .field("condition", &"<condition>")
            .finish()
    }
}

impl Clone for Rule {
    fn clone(&self) -> Self {
        // 由于 ConditionEvaluator 不能 Clone,我们创建一个新的 Rule
        // 实际使用时,用户需要重新构建规则
        Self {
            id: self.id.clone(),
            name: self.name.clone(),
            priority: self.priority,
            condition: Box::new(MatchCondition::User(vec![])) as Box<dyn ConditionEvaluator>,
            enabled: self.enabled,
        }
    }
}

// ============================================================================
// 匹配器统计
// ============================================================================

/// 匹配器统计信息
#[derive(Debug, Clone, Default)]
pub struct MatcherStats {
    /// 总匹配次数
    pub total_matches: u64,
    /// 总不匹配次数
    pub total_mismatches: u64,
    /// 最后匹配时间
    pub last_match_time: Option<Instant>,
    /// 平均匹配时间(纳秒)
    pub avg_match_time_ns: u64,
}

// ============================================================================
// 规则匹配器
// ============================================================================

/// 规则匹配器
///
/// 高性能规则匹配引擎,支持优先级排序和复合条件。
pub struct RuleMatcher {
    /// 规则列表(按优先级排序)
    rules: Vec<Rule>,
    /// 匹配统计
    stats: RwLock<MatcherStats>,
}

/// RuleMatcher 构建器
///
/// 用于链式配置 RuleMatcher 实例。
#[derive(Debug, Default)]
pub struct RuleMatcherBuilder {
    rules: Vec<Rule>,
}

impl RuleMatcherBuilder {
    /// 创建新的 RuleMatcherBuilder
    pub fn new() -> Self {
        Self { rules: Vec::new() }
    }

    /// 添加规则
    pub fn add_rule(mut self, rule: Rule) -> Self {
        self.rules.push(rule);
        self
    }

    /// 批量添加规则
    pub fn add_rules(mut self, rules: Vec<Rule>) -> Self {
        self.rules.extend(rules);
        self
    }

    /// 构建 RuleMatcher 实例
    pub fn build(self) -> RuleMatcher {
        RuleMatcher::with_dependencies(self.rules)
    }
}

impl RuleMatcher {
    /// 创建新的规则匹配器
    ///
    /// # 参数
    /// - `rules`: 规则列表
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::matchers::{RuleMatcher, Rule, MatchCondition};
    ///
    /// let matcher = RuleMatcher::new(vec![
    ///     Rule {
    ///         id: "rule1".to_string(),
    ///         name: "Test Rule".to_string(),
    ///         priority: 100,
    ///         condition: Box::new(MatchCondition::User(vec!["user1".to_string()])),
    ///         enabled: true,
    ///     },
    /// ]);
    /// ```
    pub fn new(rules: Vec<Rule>) -> Self {
        let mut matcher = Self {
            rules: Vec::new(),
            stats: RwLock::new(MatcherStats::default()),
        };

        for rule in rules {
            matcher.add_rule(rule);
        }

        matcher
    }

    /// 使用依赖注入创建 RuleMatcher(用于应用容器集成)
    pub fn with_dependencies(rules: Vec<Rule>) -> Self {
        let mut matcher = Self {
            rules: Vec::new(),
            stats: RwLock::new(MatcherStats::default()),
        };

        for rule in rules {
            matcher.add_rule(rule);
        }

        matcher
    }

    /// 添加规则
    ///
    /// # 参数
    /// - `rule`: 规则
    pub fn add_rule(&mut self, rule: Rule) {
        // 按优先级排序(降序)
        let pos = self
            .rules
            .binary_search_by(|r| r.priority.cmp(&rule.priority).reverse())
            .unwrap_or_else(|pos| pos);

        self.rules.insert(pos, rule);
    }

    /// 移除规则
    ///
    /// # 参数
    /// - `rule_id`: 规则ID
    pub fn remove_rule(&mut self, rule_id: &str) -> Option<Rule> {
        if let Some(pos) = self.rules.iter().position(|r| r.id == rule_id) {
            Some(self.rules.remove(pos))
        } else {
            None
        }
    }

    /// 检查请求是否匹配任何规则
    ///
    /// # 参数
    /// - `context`: 请求上下文
    ///
    /// # 返回
    /// - `Some(rule)`: 匹配的规则
    /// - `None`: 没有匹配的规则
    ///
    /// # 性能
    /// - P99延迟 < 200μs
    /// - 支持至少100条规则
    pub fn matches(&self, context: &RequestContext) -> Option<&Rule> {
        let start = Instant::now();

        // 按优先级顺序检查规则
        for rule in &self.rules {
            if !rule.enabled {
                continue;
            }

            if rule.condition.evaluate(context) {
                // 更新统计信息
                let elapsed = start.elapsed().as_nanos() as u64;
                {
                    let mut stats = self.stats.write();
                    stats.total_matches += 1;
                    stats.last_match_time = Some(Instant::now());

                    // 更新平均匹配时间(使用指数移动平均)
                    if stats.total_matches == 1 {
                        stats.avg_match_time_ns = elapsed;
                    } else {
                        stats.avg_match_time_ns = (stats.avg_match_time_ns * 9 + elapsed) / 10;
                    }
                }

                return Some(rule);
            }
        }

        {
            let mut stats = self.stats.write();
            stats.total_mismatches += 1;
        }
        None
    }

    /// 获取所有匹配的规则
    ///
    /// # 参数
    /// - `context`: 请求上下文
    ///
    /// # 返回
    /// - 匹配的规则列表(按优先级排序)
    pub fn match_all(&self, context: &RequestContext) -> Vec<&Rule> {
        self.rules
            .iter()
            .filter(|rule| rule.enabled && rule.condition.evaluate(context))
            .collect()
    }

    /// 获取统计信息
    pub fn stats(&self) -> MatcherStats {
        self.stats.read().clone()
    }

    /// 重置统计信息
    pub fn reset_stats(&self) {
        let mut stats = self.stats.write();
        *stats = MatcherStats::default();
    }

    /// 获取规则数量
    pub fn rule_count(&self) -> usize {
        self.rules.len()
    }

    /// 从配置创建规则匹配器
    ///
    /// # 参数
    /// - `config_matchers`: 配置中的匹配器列表
    pub fn from_config(config_matchers: &[ConfigMatcher]) -> Result<Self, LimiteronError> {
        let mut rules = Vec::new();

        for (index, matcher) in config_matchers.iter().enumerate() {
            let condition: Box<dyn ConditionEvaluator> = match matcher {
                ConfigMatcher::User { user_ids } => {
                    Box::new(MatchCondition::User(user_ids.clone()))
                }
                ConfigMatcher::Ip { ip_ranges } => {
                    let ranges: Result<Vec<IpRange>, _> =
                        ip_ranges.iter().map(|s| s.parse()).collect();

                    Box::new(MatchCondition::Ip(ranges?))
                }
                ConfigMatcher::Geo { countries } => {
                    Box::new(MatchCondition::Geo(countries.clone()))
                }
                ConfigMatcher::ApiVersion { versions } => {
                    Box::new(MatchCondition::ApiVersion(versions.clone()))
                }
                ConfigMatcher::Device { device_types } => {
                    Box::new(MatchCondition::Device(device_types.clone()))
                }
                ConfigMatcher::Custom { name, config: _ } => {
                    // 自定义匹配器需要在运行时通过CustomMatcherRegistry处理
                    // 这里返回一个占位符,实际匹配逻辑由CustomMatcherRegistry处理
                    let name = name.clone();
                    Box::new(MatchCondition::Custom(Arc::new(move |_context| {
                        // 自定义匹配器的实际匹配逻辑在CustomMatcherRegistry中实现
                        // 这里只是占位符,返回false表示不匹配
                        log::warn!("自定义匹配器 '{}' 需要通过CustomMatcherRegistry处理", name);
                        false
                    })))
                }
            };

            rules.push(Rule {
                id: format!("rule_{}", index),
                name: format!("Rule {}", index),
                priority: 100,
                condition,
                enabled: true,
            });
        }

        Ok(Self::new(rules))
    }
}

#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod tests {
    use super::*;
    use std::sync::Arc;

    // ==================== IpRange ====================

    #[test]
    fn test_ip_range_single() {
        let range: IpRange = "192.168.1.1".parse().unwrap();
        assert!(range.contains(&"192.168.1.1".parse::<IpAddr>().unwrap()));
        assert!(!range.contains(&"192.168.1.2".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_ipv4_cidr() {
        let range: IpRange = "192.168.0.0/16".parse().unwrap();
        assert!(range.contains(&"192.168.1.1".parse::<IpAddr>().unwrap()));
        assert!(range.contains(&"192.168.255.255".parse::<IpAddr>().unwrap()));
        assert!(!range.contains(&"10.0.0.1".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_ipv4_cidr_single() {
        let range: IpRange = "10.0.0.1/32".parse().unwrap();
        assert!(range.contains(&"10.0.0.1".parse::<IpAddr>().unwrap()));
        assert!(!range.contains(&"10.0.0.2".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_ipv6_cidr() {
        let range: IpRange = "2001:db8::/32".parse().unwrap();
        assert!(range.contains(&"2001:db8::1".parse::<IpAddr>().unwrap()));
        assert!(!range.contains(&"::1".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_v4_range() {
        let range: IpRange = "192.168.1.1-192.168.1.10".parse().unwrap();
        assert!(range.contains(&"192.168.1.5".parse::<IpAddr>().unwrap()));
        assert!(!range.contains(&"192.168.2.1".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_single_ipv6() {
        let range: IpRange = "::1".parse().unwrap();
        assert!(range.contains(&"::1".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_invalid_format() {
        assert!("not-an-ip".parse::<IpRange>().is_err());
        assert!("192.168.1.0/33".parse::<IpRange>().is_err());
        assert!("::1/129".parse::<IpRange>().is_err());
    }

    // ==================== MatchCondition ====================

    fn make_ctx(headers: Vec<(&str, &str)>, client_ip: Option<&str>) -> RequestContext {
        let mut ctx = RequestContext::new();
        for (k, v) in headers {
            ctx = ctx.with_header(k, v);
        }
        if let Some(ip) = client_ip {
            ctx = ctx.with_client_ip(ip);
        }
        ctx
    }

    #[test]
    fn test_match_user_match() {
        let cond = MatchCondition::User(vec!["user1".into(), "user2".into()]);
        let ctx = make_ctx(vec![("X-User-Id", "user1")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_user_no_match() {
        let cond = MatchCondition::User(vec!["user1".into()]);
        let ctx = make_ctx(vec![("X-User-Id", "user2")], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_user_wildcard() {
        let cond = MatchCondition::User(vec!["*".into()]);
        let ctx = make_ctx(vec![("X-User-Id", "anyone")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_user_no_header_wildcard() {
        let cond = MatchCondition::User(vec!["*".into()]);
        let ctx = make_ctx(vec![], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_user_no_header_no_wildcard() {
        let cond = MatchCondition::User(vec!["user1".into()]);
        let ctx = make_ctx(vec![], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_ip_match() {
        let cond = MatchCondition::Ip(vec!["192.168.0.0/16".parse().unwrap()]);
        let ctx = make_ctx(vec![], Some("192.168.1.1"));
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_ip_no_match() {
        let cond = MatchCondition::Ip(vec!["192.168.0.0/16".parse().unwrap()]);
        let ctx = make_ctx(vec![], Some("10.0.0.1"));
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_ip_no_client_ip() {
        let cond = MatchCondition::Ip(vec!["192.168.0.0/16".parse().unwrap()]);
        let ctx = make_ctx(vec![], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_geo_match() {
        let cond = MatchCondition::Geo(vec!["US".into(), "CN".into()]);
        let ctx = make_ctx(vec![("X-Country", "CN")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_geo_no_match() {
        let cond = MatchCondition::Geo(vec!["US".into()]);
        let ctx = make_ctx(vec![("X-Country", "CN")], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_geo_wildcard() {
        let cond = MatchCondition::Geo(vec!["*".into()]);
        let ctx = make_ctx(vec![], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_api_version_match() {
        let cond = MatchCondition::ApiVersion(vec!["v1".into(), "v2".into()]);
        let ctx = make_ctx(vec![("X-API-Version", "v2")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_api_version_no_match() {
        let cond = MatchCondition::ApiVersion(vec!["v1".into()]);
        let ctx = make_ctx(vec![("X-API-Version", "v3")], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_device_match() {
        let cond = MatchCondition::Device(vec!["mobile".into(), "desktop".into()]);
        let ctx = make_ctx(vec![("X-Device-Type", "mobile")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_device_no_match() {
        let cond = MatchCondition::Device(vec!["mobile".into()]);
        let ctx = make_ctx(vec![("X-Device-Type", "tablet")], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_custom_closure() {
        let cond = MatchCondition::Custom(Arc::new(|ctx| {
            ctx.get_header("X-Feature").is_some_and(|v| v == "enabled")
        }));
        let ctx = make_ctx(vec![("X-Feature", "enabled")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_custom_closure_no_match() {
        let cond = MatchCondition::Custom(Arc::new(|_| false));
        let ctx = make_ctx(vec![], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_match_condition_description() {
        let cond = MatchCondition::User(vec!["u1".into()]);
        assert!(cond.description().contains("User"));
        let cond = MatchCondition::Geo(vec!["US".into()]);
        assert!(cond.description().contains("Country"));
        let cond = MatchCondition::Custom(Arc::new(|_| true));
        assert_eq!(cond.description(), "Custom condition");
    }

    // ==================== CompositeCondition ====================

    #[test]
    fn test_composite_and_all_true() {
        let cond = CompositeCondition {
            conditions: vec![
                Box::new(MatchCondition::User(vec!["u1".into()])),
                Box::new(MatchCondition::Geo(vec!["US".into()])),
            ],
            operator: LogicalOperator::And,
        };
        let ctx = make_ctx(vec![("X-User-Id", "u1"), ("X-Country", "US")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_composite_and_one_false() {
        let cond = CompositeCondition {
            conditions: vec![
                Box::new(MatchCondition::User(vec!["u1".into()])),
                Box::new(MatchCondition::Geo(vec!["US".into()])),
            ],
            operator: LogicalOperator::And,
        };
        let ctx = make_ctx(vec![("X-User-Id", "u1"), ("X-Country", "CN")], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_composite_or_all_false() {
        let cond = CompositeCondition {
            conditions: vec![
                Box::new(MatchCondition::User(vec!["u1".into()])),
                Box::new(MatchCondition::Geo(vec!["US".into()])),
            ],
            operator: LogicalOperator::Or,
        };
        let ctx = make_ctx(vec![("X-User-Id", "u2"), ("X-Country", "CN")], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_composite_or_one_true() {
        let cond = CompositeCondition {
            conditions: vec![
                Box::new(MatchCondition::User(vec!["u1".into()])),
                Box::new(MatchCondition::Geo(vec!["US".into()])),
            ],
            operator: LogicalOperator::Or,
        };
        let ctx = make_ctx(vec![("X-User-Id", "u1"), ("X-Country", "CN")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_composite_not_true() {
        let cond = CompositeCondition {
            conditions: vec![Box::new(MatchCondition::User(vec!["u1".into()]))],
            operator: LogicalOperator::Not,
        };
        let ctx = make_ctx(vec![("X-User-Id", "u2")], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_composite_not_false() {
        let cond = CompositeCondition {
            conditions: vec![Box::new(MatchCondition::User(vec!["u1".into()]))],
            operator: LogicalOperator::Not,
        };
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_composite_not_empty() {
        let cond = CompositeCondition {
            conditions: vec![],
            operator: LogicalOperator::Not,
        };
        let ctx = make_ctx(vec![], None);
        assert!(!cond.evaluate(&ctx));
    }

    // ==================== RuleMatcher ====================

    #[test]
    fn test_rule_matcher_match() {
        let matcher = RuleMatcher::new(vec![Rule {
            id: "r1".into(),
            name: "Test".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["u1".into()])),
            enabled: true,
        }]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        let result = matcher.matches(&ctx);
        assert!(result.is_some());
        assert_eq!(result.unwrap().id, "r1");
    }

    #[test]
    fn test_rule_matcher_no_match() {
        let matcher = RuleMatcher::new(vec![Rule {
            id: "r1".into(),
            name: "Test".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["u1".into()])),
            enabled: true,
        }]);
        let ctx = make_ctx(vec![("X-User-Id", "u2")], None);
        assert!(matcher.matches(&ctx).is_none());
    }

    #[test]
    fn test_rule_matcher_disabled_rule() {
        let matcher = RuleMatcher::new(vec![Rule {
            id: "r1".into(),
            name: "Disabled".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["*".into()])),
            enabled: false,
        }]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        assert!(matcher.matches(&ctx).is_none());
    }

    #[test]
    fn test_rule_matcher_priority() {
        let matcher = RuleMatcher::new(vec![
            Rule {
                id: "low".into(),
                name: "Low".into(),
                priority: 50,
                condition: Box::new(MatchCondition::User(vec!["u1".into()])),
                enabled: true,
            },
            Rule {
                id: "high".into(),
                name: "High".into(),
                priority: 100,
                condition: Box::new(MatchCondition::User(vec!["*".into()])),
                enabled: true,
            },
        ]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        let result = matcher.matches(&ctx);
        assert_eq!(result.unwrap().id, "high");
    }

    #[test]
    fn test_rule_matcher_add_rule() {
        let mut matcher = RuleMatcher::new(vec![]);
        matcher.add_rule(Rule {
            id: "r1".into(),
            name: "Added".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["*".into()])),
            enabled: true,
        });
        assert_eq!(matcher.rule_count(), 1);
    }

    #[test]
    fn test_rule_matcher_remove_rule() {
        let mut matcher = RuleMatcher::new(vec![Rule {
            id: "r1".into(),
            name: "Test".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["*".into()])),
            enabled: true,
        }]);
        let removed = matcher.remove_rule("r1");
        assert!(removed.is_some());
        assert_eq!(matcher.rule_count(), 0);
    }

    #[test]
    fn test_rule_matcher_remove_nonexistent() {
        let mut matcher = RuleMatcher::new(vec![]);
        assert!(matcher.remove_rule("nonexistent").is_none());
    }

    #[test]
    fn test_rule_matcher_match_all() {
        let matcher = RuleMatcher::new(vec![
            Rule {
                id: "r1".into(),
                name: "R1".into(),
                priority: 100,
                condition: Box::new(MatchCondition::User(vec!["*".into()])),
                enabled: true,
            },
            Rule {
                id: "r2".into(),
                name: "R2".into(),
                priority: 50,
                condition: Box::new(MatchCondition::Geo(vec!["*".into()])),
                enabled: true,
            },
        ]);
        let ctx = make_ctx(vec![("X-User-Id", "u1"), ("X-Country", "US")], None);
        let results = matcher.match_all(&ctx);
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_rule_matcher_stats() {
        let matcher = RuleMatcher::new(vec![Rule {
            id: "r1".into(),
            name: "Test".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["*".into()])),
            enabled: true,
        }]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        matcher.matches(&ctx);
        let stats = matcher.stats();
        assert_eq!(stats.total_matches, 1);
        assert_eq!(stats.total_mismatches, 0);
    }

    #[test]
    fn test_rule_matcher_reset_stats() {
        let matcher = RuleMatcher::new(vec![Rule {
            id: "r1".into(),
            name: "Test".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["*".into()])),
            enabled: true,
        }]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        matcher.matches(&ctx);
        matcher.reset_stats();
        let stats = matcher.stats();
        assert_eq!(stats.total_matches, 0);
    }

    #[test]
    fn test_rule_matcher_rule_count() {
        let matcher = RuleMatcher::new(vec![
            Rule {
                id: "r1".into(),
                name: "R1".into(),
                priority: 100,
                condition: Box::new(MatchCondition::User(vec!["*".into()])),
                enabled: true,
            },
            Rule {
                id: "r2".into(),
                name: "R2".into(),
                priority: 50,
                condition: Box::new(MatchCondition::User(vec!["*".into()])),
                enabled: true,
            },
        ]);
        assert_eq!(matcher.rule_count(), 2);
    }

    #[test]
    fn test_rule_matcher_rule_matcher_builder() {
        let matcher = RuleMatcherBuilder::new()
            .add_rule(Rule {
                id: "r1".into(),
                name: "Builder".into(),
                priority: 100,
                condition: Box::new(MatchCondition::User(vec!["*".into()])),
                enabled: true,
            })
            .build();
        assert_eq!(matcher.rule_count(), 1);
    }

    #[test]
    fn test_rule_matcher_add_rules() {
        let matcher = RuleMatcherBuilder::new()
            .add_rules(vec![
                Rule {
                    id: "r1".into(),
                    name: "R1".into(),
                    priority: 100,
                    condition: Box::new(MatchCondition::User(vec!["*".into()])),
                    enabled: true,
                },
                Rule {
                    id: "r2".into(),
                    name: "R2".into(),
                    priority: 50,
                    condition: Box::new(MatchCondition::User(vec!["*".into()])),
                    enabled: true,
                },
            ])
            .build();
        assert_eq!(matcher.rule_count(), 2);
    }

    // ==================== Edge cases ====================

    #[test]
    fn test_empty_rules_no_match() {
        let matcher = RuleMatcher::new(vec![]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        assert!(matcher.matches(&ctx).is_none());
        assert!(matcher.match_all(&ctx).is_empty());
    }

    #[test]
    fn test_rule_matcher_with_dependencies_direct() {
        // 直接调用 with_dependencies 构造(应用容器集成路径)
        let matcher = RuleMatcher::with_dependencies(vec![Rule {
            id: "wd1".into(),
            name: "WithDeps".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["u1".into()])),
            enabled: true,
        }]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        let result = matcher.matches(&ctx);
        assert!(result.is_some());
        assert_eq!(result.unwrap().id, "wd1");
        assert_eq!(matcher.rule_count(), 1);
    }

    #[test]
    fn test_rule_matcher_with_dependencies_empty() {
        // with_dependencies 接收空规则列表
        let matcher = RuleMatcher::with_dependencies(vec![]);
        assert_eq!(matcher.rule_count(), 0);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        assert!(matcher.matches(&ctx).is_none());
    }

    #[test]
    fn test_composite_and_empty_conditions() {
        // 空 conditions + AND 操作符:空集所有元素都满足(vacuous truth)→ true
        let cond = CompositeCondition {
            conditions: vec![],
            operator: LogicalOperator::And,
        };
        let ctx = make_ctx(vec![], None);
        assert!(cond.evaluate(&ctx));
    }

    #[test]
    fn test_composite_or_empty_conditions() {
        // 空 conditions + OR 操作符:空集没有任何元素满足 → false
        let cond = CompositeCondition {
            conditions: vec![],
            operator: LogicalOperator::Or,
        };
        let ctx = make_ctx(vec![], None);
        assert!(!cond.evaluate(&ctx));
    }

    #[test]
    fn test_from_config_user() {
        use crate::config::ConfigMatcher;
        let matchers = vec![ConfigMatcher::User {
            user_ids: vec!["u1".into()],
        }];
        let matcher = RuleMatcher::from_config(&matchers).unwrap();
        assert_eq!(matcher.rule_count(), 1);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        assert!(matcher.matches(&ctx).is_some());
    }

    #[test]
    fn test_from_config_ip() {
        use crate::config::ConfigMatcher;
        let matchers = vec![ConfigMatcher::Ip {
            ip_ranges: vec!["10.0.0.0/8".into()],
        }];
        let matcher = RuleMatcher::from_config(&matchers).unwrap();
        assert_eq!(matcher.rule_count(), 1);
    }

    #[test]
    fn test_from_config_geo() {
        use crate::config::ConfigMatcher;
        let matchers = vec![ConfigMatcher::Geo {
            countries: vec!["US".into()],
        }];
        let matcher = RuleMatcher::from_config(&matchers).unwrap();
        assert_eq!(matcher.rule_count(), 1);
    }

    #[test]
    fn test_from_config_custom() {
        use crate::config::ConfigMatcher;
        let matchers = vec![ConfigMatcher::Custom {
            name: "my-custom".into(),
            config: serde_json::Value::Null,
        }];
        let matcher = RuleMatcher::from_config(&matchers).unwrap();
        assert_eq!(matcher.rule_count(), 1);
    }

    #[test]
    fn test_ip_range_description() {
        let cond = MatchCondition::Ip(vec!["10.0.0.0/8".parse().unwrap()]);
        assert!(cond.description().contains("IP in"));
    }

    // ==================== IpRange additional error paths ====================

    #[test]
    fn test_ip_range_invalid_cidr_extra_parts() {
        assert!("10.0.0.0/8/extra".parse::<IpRange>().is_err());
    }

    #[test]
    fn test_ip_range_invalid_range_extra_parts() {
        assert!("10.0.0.0-10.0.0.255-extra".parse::<IpRange>().is_err());
    }

    #[test]
    fn test_ip_range_start_gt_end() {
        assert!("10.0.0.10-10.0.0.5".parse::<IpRange>().is_err());
    }

    // ==================== IpRange cross-type ====================

    #[test]
    fn test_ip_range_ipv4_cidr_with_ipv6() {
        let range: IpRange = "10.0.0.0/8".parse().unwrap();
        assert!(!range.contains(&"::1".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_ipv6_cidr_with_ipv4() {
        let range: IpRange = "2001:db8::/32".parse().unwrap();
        assert!(!range.contains(&"10.0.0.1".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_v4_range_with_ipv6() {
        let range: IpRange = "10.0.0.1-10.0.0.10".parse().unwrap();
        assert!(!range.contains(&"::1".parse::<IpAddr>().unwrap()));
    }

    // ==================== Debug implementations ====================

    #[test]
    fn test_match_condition_debug_all() {
        let _ = format!("{:?}", MatchCondition::User(vec!["u1".into()]));
        let _ = format!("{:?}", MatchCondition::Ip(vec![]));
        let _ = format!("{:?}", MatchCondition::Geo(vec!["US".into()]));
        let _ = format!("{:?}", MatchCondition::ApiVersion(vec!["v1".into()]));
        let _ = format!("{:?}", MatchCondition::Device(vec!["mobile".into()]));
        let _ = format!("{:?}", MatchCondition::Custom(Arc::new(|_| true)));
    }

    #[test]
    fn test_composite_condition_debug() {
        let cond = CompositeCondition {
            conditions: vec![Box::new(MatchCondition::User(vec!["u1".into()]))],
            operator: LogicalOperator::And,
        };
        let _ = format!("{:?}", cond);
    }

    #[test]
    fn test_rule_debug() {
        let rule = Rule {
            id: "test".into(),
            name: "Test".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["*".into()])),
            enabled: true,
        };
        let _ = format!("{:?}", rule);
    }

    // ==================== Clone implementations ====================

    #[test]
    fn test_composite_condition_clone() {
        let cond = CompositeCondition {
            conditions: vec![Box::new(MatchCondition::User(vec!["u1".into()]))],
            operator: LogicalOperator::And,
        };
        let cloned = cond.clone();
        assert_eq!(cloned.conditions.len(), 1);
        assert_eq!(cloned.operator, LogicalOperator::And);
    }

    #[test]
    fn test_rule_clone() {
        let rule = Rule {
            id: "test-rule".into(),
            name: "Test Rule".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["u1".into()])),
            enabled: true,
        };
        let cloned = rule.clone();
        assert_eq!(cloned.id, "test-rule");
        assert_eq!(cloned.name, "Test Rule");
        assert_eq!(cloned.priority, 100);
        assert!(cloned.enabled);
    }

    // ==================== from_config additional config types ====================

    #[test]
    fn test_from_config_api_version() {
        use crate::config::ConfigMatcher;
        let matchers = vec![ConfigMatcher::ApiVersion {
            versions: vec!["v2".into()],
        }];
        let matcher = RuleMatcher::from_config(&matchers).unwrap();
        let ctx = make_ctx(vec![("X-API-Version", "v2")], None);
        assert!(matcher.matches(&ctx).is_some());
    }

    #[test]
    fn test_from_config_device() {
        use crate::config::ConfigMatcher;
        let matchers = vec![ConfigMatcher::Device {
            device_types: vec!["mobile".into()],
        }];
        let matcher = RuleMatcher::from_config(&matchers).unwrap();
        let ctx = make_ctx(vec![("X-Device-Type", "mobile")], None);
        assert!(matcher.matches(&ctx).is_some());
    }

    #[test]
    fn test_from_config_custom_evaluate() {
        use crate::config::ConfigMatcher;
        let matchers = vec![ConfigMatcher::Custom {
            name: "test-custom".into(),
            config: serde_json::Value::Null,
        }];
        let matcher = RuleMatcher::from_config(&matchers).unwrap();
        let ctx = make_ctx(vec![], None);
        assert!(matcher.matches(&ctx).is_none());
    }

    // ==================== matches() EMA path ====================

    #[test]
    fn test_rule_matcher_stats_ema() {
        let matcher = RuleMatcher::new(vec![Rule {
            id: "r1".into(),
            name: "Test".into(),
            priority: 100,
            condition: Box::new(MatchCondition::User(vec!["*".into()])),
            enabled: true,
        }]);
        let ctx = make_ctx(vec![("X-User-Id", "u1")], None);
        matcher.matches(&ctx);
        matcher.matches(&ctx);
        let stats = matcher.stats();
        assert_eq!(stats.total_matches, 2);
        assert!(stats.avg_match_time_ns > 0);
    }

    // ==================== IPv6 CIDR 非对齐前缀测试 ====================

    #[test]
    fn test_ip_range_ipv6_cidr_non_aligned_prefix() {
        // /20 前缀:full_segments=1, remaining_bits=4
        // 覆盖 ipv6_in_cidr 中的剩余位检查分支
        let range: IpRange = "2001:db8::/20".parse().unwrap();
        assert!(range.contains(&"2001:db8::1".parse::<IpAddr>().unwrap()));
        assert!(range.contains(&"2001:0fff::1".parse::<IpAddr>().unwrap()));
        assert!(!range.contains(&"2002:db8::1".parse::<IpAddr>().unwrap()));
    }

    #[test]
    fn test_ip_range_ipv6_cidr_non_aligned_prefix_match() {
        // /48 前缀:full_segments=3, remaining_bits=0 -> 不触发剩余位分支
        // /52 前缀:full_segments=3, remaining_bits=4 -> 触发剩余位分支
        let range: IpRange = "2001:db8:abcd::/52".parse().unwrap();
        assert!(range.contains(&"2001:db8:abcd::1".parse::<IpAddr>().unwrap()));
        assert!(!range.contains(&"2001:db8:abef::1".parse::<IpAddr>().unwrap()));
    }

    // ==================== CompositeCondition description 测试 ====================

    #[test]
    fn test_composite_description_and() {
        let cond = CompositeCondition {
            conditions: vec![
                Box::new(MatchCondition::User(vec!["u1".into()])),
                Box::new(MatchCondition::Geo(vec!["US".into()])),
            ],
            operator: LogicalOperator::And,
        };
        let desc = cond.description();
        assert!(desc.contains("AND"));
        assert!(desc.contains("2"));
    }

    #[test]
    fn test_composite_description_or() {
        let cond = CompositeCondition {
            conditions: vec![
                Box::new(MatchCondition::User(vec!["u1".into()])),
                Box::new(MatchCondition::Geo(vec!["US".into()])),
            ],
            operator: LogicalOperator::Or,
        };
        let desc = cond.description();
        assert!(desc.contains("OR"));
    }

    #[test]
    fn test_composite_description_not() {
        let cond = CompositeCondition {
            conditions: vec![Box::new(MatchCondition::User(vec!["u1".into()]))],
            operator: LogicalOperator::Not,
        };
        let desc = cond.description();
        assert!(desc.contains("NOT"));
    }
}