limiteron 0.1.0

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
//! Copyright (c) 2026, Kirky.X
//!
//! MIT License
//!
//! 设备类型匹配器
//!
//! 基于User-Agent解析设备类型、浏览器和操作系统信息。
//!
//! # 特性
//!
//! - 识别设备类型(移动端/桌面端/平板/API/未知)
//! - 识别浏览器类型
//! - 识别操作系统
//! - 内置缓存(性能开销 < 500μs)
//! - 支持自定义规则
//!
//! # 性能
//!
//! - 识别准确率 > 90%
//! - 性能开销 P99 < 500μs
//! - 缓存命中率 > 90%
//!
//! # 使用示例
//!
//! ```rust
//! use limiteron::matchers::device::DeviceMatcher;
//!
//! #[tokio::main]
//! async fn main() {
//!     let matcher = DeviceMatcher::new().await.unwrap();
//!     let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15";
//!     let info = matcher.parse(user_agent).unwrap();
//! }
//! ```

#[cfg(feature = "device-matching")]
use crate::error::FlowGuardError;
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::{debug, info, instrument, warn};
use woothee::parser::Parser;

// ============================================================================
// 输入验证常量
// ============================================================================

/// 最大 User-Agent 长度
const MAX_USER_AGENT_LENGTH: usize = 2048;

// ============================================================================
// 输入验证函数
// ============================================================================

/// 清理 User-Agent 字符串
///
/// # 参数
/// - `user_agent`: User-Agent 字符串
///
/// # 返回
/// - 清理后的字符串
fn sanitize_user_agent(user_agent: &str) -> String {
    user_agent
        .chars()
        .filter(|c| c.is_ascii() || c.is_alphanumeric() || " -./:;()[]{}@".contains(*c))
        .collect()
}

// ============================================================================
// 设备类型
// ============================================================================

#[cfg(feature = "device-matching")]
/// 设备类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DeviceType {
    /// 移动设备
    Mobile,
    /// 桌面设备
    Desktop,
    /// 平板设备
    Tablet,
    /// API客户端
    API,
    /// 未知设备
    Unknown,
}

impl DeviceType {
    /// 从字符串解析设备类型
    pub fn parse(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "mobile" | "smartphone" => DeviceType::Mobile,
            "desktop" | "pc" => DeviceType::Desktop,
            "tablet" | "ipad" => DeviceType::Tablet,
            "api" | "bot" | "crawler" => DeviceType::API,
            _ => DeviceType::Unknown,
        }
    }

    /// 转换为字符串
    pub fn as_str(&self) -> &'static str {
        match self {
            DeviceType::Mobile => "mobile",
            DeviceType::Desktop => "desktop",
            DeviceType::Tablet => "tablet",
            DeviceType::API => "api",
            DeviceType::Unknown => "unknown",
        }
    }

    /// 检查是否为移动设备(包括平板)
    pub fn is_mobile(&self) -> bool {
        matches!(self, DeviceType::Mobile | DeviceType::Tablet)
    }

    /// 检查是否为桌面设备
    pub fn is_desktop(&self) -> bool {
        matches!(self, DeviceType::Desktop)
    }

    /// 检查是否为API客户端
    pub fn is_api(&self) -> bool {
        matches!(self, DeviceType::API)
    }
}

impl std::fmt::Display for DeviceType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

// ============================================================================
// 设备信息
// ============================================================================

#[cfg(feature = "device-matching")]
/// 设备信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DeviceInfo {
    /// 设备类型
    pub device_type: DeviceType,
    /// 浏览器名称
    pub browser: Option<String>,
    /// 浏览器版本
    pub browser_version: Option<String>,
    /// 操作系统
    pub os: Option<String>,
    /// 操作系统版本
    pub os_version: Option<String>,
    /// 原始User-Agent
    pub user_agent: Option<String>,
}

impl DeviceInfo {
    /// 创建空的设备信息
    pub fn empty() -> Self {
        Self {
            device_type: DeviceType::Unknown,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        }
    }

    /// 检查是否为空
    pub fn is_empty(&self) -> bool {
        self.device_type == DeviceType::Unknown && self.browser.is_none() && self.os.is_none()
    }

    /// 获取设备描述
    pub fn description(&self) -> String {
        match (&self.device_type, &self.browser, &self.os) {
            (DeviceType::API, _, _) => "API Client".to_string(),
            (_dt, Some(browser), Some(os)) => {
                format!(
                    "{} {} on {} {}",
                    browser,
                    self.browser_version.as_deref().unwrap_or(""),
                    os,
                    self.os_version.as_deref().unwrap_or("")
                )
            }
            (dt, Some(browser), None) => {
                format!(
                    "{} {} on {}",
                    browser,
                    self.browser_version.as_deref().unwrap_or(""),
                    dt
                )
            }
            (dt, None, Some(os)) => {
                format!("{} on {}", os, dt)
            }
            (dt, None, None) => dt.to_string(),
        }
    }

    /// 从woothee结果创建设备信息
    fn from_woothee(result: &woothee::parser::WootheeResult) -> Self {
        let device_type = Self::map_woothee_device_type(result.category);

        let browser = if device_type != DeviceType::API {
            Some(result.name.to_string())
        } else {
            None
        };

        let browser_version = if device_type != DeviceType::API {
            Some(result.version.to_string())
        } else {
            None
        };

        let os = if device_type != DeviceType::API {
            Some(result.os.to_string())
        } else {
            None
        };

        let os_version = if device_type != DeviceType::API {
            Some(result.os_version.to_string())
        } else {
            None
        };

        Self {
            device_type,
            browser,
            browser_version,
            os,
            os_version,
            user_agent: None,
        }
    }

    /// 映射woothee设备类型
    fn map_woothee_device_type(category: &str) -> DeviceType {
        match category.to_lowercase().as_str() {
            "pc" => DeviceType::Desktop,
            "smartphone" => DeviceType::Mobile,
            "mobilephone" => DeviceType::Mobile,
            "tablet" => DeviceType::Tablet,
            "appliance" => DeviceType::API,
            "crawler" => DeviceType::API,
            "misc" => DeviceType::API,
            _ => DeviceType::Unknown,
        }
    }
}

impl Default for DeviceInfo {
    fn default() -> Self {
        Self::empty()
    }
}

// ============================================================================
// 设备匹配条件
// ============================================================================

#[cfg(feature = "device-matching")]
/// 设备匹配条件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DeviceCondition {
    /// 设备类型列表
    pub device_types: Vec<DeviceType>,
    /// 浏览器名称列表
    pub browsers: Vec<String>,
    /// 操作系统列表
    pub os: Vec<String>,
}

impl DeviceCondition {
    /// 创建空的匹配条件
    pub fn empty() -> Self {
        Self {
            device_types: vec![],
            browsers: vec![],
            os: vec![],
        }
    }

    /// 创建设备类型匹配条件
    pub fn device_types(device_types: Vec<DeviceType>) -> Self {
        Self {
            device_types,
            browsers: vec![],
            os: vec![],
        }
    }

    /// 创建浏览器匹配条件
    pub fn browsers(browsers: Vec<String>) -> Self {
        Self {
            device_types: vec![],
            browsers,
            os: vec![],
        }
    }

    /// 创建操作系统匹配条件
    pub fn os(os: Vec<String>) -> Self {
        Self {
            device_types: vec![],
            browsers: vec![],
            os,
        }
    }

    /// 检查是否为空
    pub fn is_empty(&self) -> bool {
        self.device_types.is_empty() && self.browsers.is_empty() && self.os.is_empty()
    }

    /// 检查设备信息是否匹配条件
    pub fn matches(&self, info: &DeviceInfo) -> bool {
        if self.is_empty() {
            return true;
        }

        // 检查设备类型匹配
        if !self.device_types.is_empty() {
            if self.device_types.contains(&info.device_type) {
                return true;
            }
            return false;
        }

        // 检查浏览器匹配
        if !self.browsers.is_empty() {
            if let Some(browser) = &info.browser {
                if self.browsers.iter().any(|b| {
                    browser.to_lowercase().contains(&b.to_lowercase())
                        || b.to_lowercase().contains(&browser.to_lowercase())
                }) {
                    return true;
                }
            }
            return false;
        }

        // 检查操作系统匹配
        if !self.os.is_empty() {
            if let Some(os) = &info.os {
                if self.os.iter().any(|o| {
                    os.to_lowercase().contains(&o.to_lowercase())
                        || o.to_lowercase().contains(&os.to_lowercase())
                }) {
                    return true;
                }
            }
            return false;
        }

        false
    }
}

impl Default for DeviceCondition {
    fn default() -> Self {
        Self::empty()
    }
}

// ============================================================================
// 设备匹配器
// ============================================================================

/// 设备匹配器
///
#[cfg(feature = "device-matching")]
/// 解析User-Agent并识别设备类型、浏览器和操作系统。
pub struct DeviceMatcher {
    /// Woothee解析器
    parser: Arc<Parser>,
    /// 查询缓存
    cache: Arc<DashMap<String, DeviceInfo>>,
    /// 缓存大小限制
    cache_size_limit: usize,
    /// 自定义规则
    custom_rules: Vec<DeviceCustomRule>,
}

/// 自定义设备规则
#[derive(Debug, Clone)]
struct DeviceCustomRule {
    /// 规则名称
    name: String,
    /// 匹配模式(正则表达式)
    pattern: String,
    /// 设备类型
    device_type: DeviceType,
    /// 浏览器名称
    browser: Option<String>,
    /// 操作系统
    os: Option<String>,
}

impl DeviceMatcher {
    /// 创建新的设备匹配器
    ///
    /// # 返回
    /// - `Ok(DeviceMatcher)`: 成功创建匹配器
    /// - `Err(FlowGuardError)`: 创建失败
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// let matcher = DeviceMatcher::new().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument]
    pub async fn new() -> Result<Self, FlowGuardError> {
        info!("创建DeviceMatcher");

        let parser = Parser::new();

        let matcher = Self {
            parser: Arc::new(parser),
            cache: Arc::new(DashMap::new()),
            cache_size_limit: 10_000,
            custom_rules: Self::default_custom_rules(),
        };

        info!("DeviceMatcher创建成功");
        Ok(matcher)
    }

    /// 创建带缓存大小限制的设备匹配器
    ///
    /// # 参数
    /// - `cache_size_limit`: 缓存大小限制
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// let matcher = DeviceMatcher::with_cache_limit(5000).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument]
    pub async fn with_cache_limit(cache_size_limit: usize) -> Result<Self, FlowGuardError> {
        let mut matcher = Self::new().await?;
        matcher.cache_size_limit = cache_size_limit;
        Ok(matcher)
    }

    /// 解析User-Agent
    ///
    /// # 参数
    /// - `user_agent`: User-Agent字符串
    ///
    /// # 返回
    /// - `Ok(DeviceInfo)`: 设备信息
    /// - `Err(FlowGuardError)`: 解析失败
    ///
    /// # 性能
    /// - 首次解析: ~100μs
    /// - 缓存命中: < 10μs
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// let matcher = DeviceMatcher::new().await?;
    /// let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";
    /// let info = matcher.parse(user_agent)?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self))]
    pub fn parse(&self, user_agent: &str) -> Result<DeviceInfo, FlowGuardError> {
        // 清理 User-Agent
        let sanitized = sanitize_user_agent(user_agent);
        let user_agent = sanitized.trim();

        // 如果清理后为空,直接返回空的 DeviceInfo
        if user_agent.is_empty() {
            return Ok(DeviceInfo::empty());
        }

        // 验证 User-Agent 长度
        if user_agent.len() > MAX_USER_AGENT_LENGTH {
            return Err(FlowGuardError::ConfigError(format!(
                "User-Agent 长度超过限制(最大 {} 字符)",
                MAX_USER_AGENT_LENGTH
            )));
        }

        // 检查缓存
        if let Some(cached) = self.cache.get(user_agent) {
            debug!("缓存命中: {}", user_agent);
            return Ok(cached.clone());
        }

        debug!("解析User-Agent: {}", user_agent);

        // 检查自定义规则
        for rule in &self.custom_rules {
            if let Ok(re) = regex::Regex::new(&rule.pattern) {
                if re.is_match(user_agent) {
                    let info = DeviceInfo {
                        device_type: rule.device_type,
                        browser: rule.browser.clone(),
                        browser_version: None,
                        os: rule.os.clone(),
                        os_version: None,
                        user_agent: Some(user_agent.to_string()),
                    };
                    self.update_cache(user_agent, &info);
                    debug!("自定义规则匹配: {}", rule.name);
                    return Ok(info);
                }
            }
        }

        // 使用woothee解析
        let result = self.parser.parse(user_agent);
        let mut info = if let Some(res) = result {
            DeviceInfo::from_woothee(&res)
        } else {
            DeviceInfo::empty()
        };
        info.user_agent = Some(user_agent.to_string());

        // 更新缓存
        self.update_cache(user_agent, &info);

        debug!(
            "User-Agent解析成功: {} -> {}",
            user_agent,
            info.description()
        );
        Ok(info)
    }

    /// 批量解析User-Agent
    ///
    /// # 参数
    /// - `user_agents`: User-Agent字符串列表
    ///
    /// # 返回
    /// - `Vec<Result<DeviceInfo>>`: 设备信息列表
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let matcher = DeviceMatcher::new().await.unwrap();
    ///     let user_agents: Vec<String> = vec![
    ///         "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)".to_string(),
    ///         "Mozilla/5.0 (Windows NT 10.0; Win64; x64)".to_string(),
    ///     ];
    ///     let results = matcher.batch_parse(&user_agents);
    /// }
    /// ```
    #[instrument(skip(self, user_agents))]
    pub fn batch_parse(&self, user_agents: &[String]) -> Vec<Result<DeviceInfo, FlowGuardError>> {
        user_agents.iter().map(|ua| self.parse(ua)).collect()
    }

    /// 检查User-Agent是否匹配设备条件
    ///
    /// # 参数
    /// - `user_agent`: User-Agent字符串
    /// - `condition`: 设备匹配条件
    ///
    /// # 返回
    /// - `Ok(true)`: 匹配
    /// - `Ok(false)`: 不匹配
    /// - `Err(FlowGuardError)`: 解析失败
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::{DeviceMatcher, DeviceCondition, DeviceType};
    ///
    /// let matcher = DeviceMatcher::new().await?;
    /// let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);
    /// let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";
    /// let matched = matcher.matches_user_agent(user_agent, &condition)?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(self, condition))]
    pub fn matches_user_agent(
        &self,
        user_agent: &str,
        condition: &DeviceCondition,
    ) -> Result<bool, FlowGuardError> {
        let info = self.parse(user_agent)?;
        Ok(condition.matches(&info))
    }

    /// 检查设备信息是否匹配条件
    ///
    /// # 参数
    /// - `info`: 设备信息
    /// - `condition`: 设备匹配条件
    ///
    /// # 返回
    /// - `true`: 匹配
    /// - `false`: 不匹配
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::matchers::device::{DeviceInfo, DeviceCondition, DeviceType};
    ///
    /// let info = DeviceInfo {
    ///     device_type: DeviceType::Mobile,
    ///     browser: Some("Safari".to_string()),
    ///     browser_version: Some("14.0".to_string()),
    ///     os: Some("iOS".to_string()),
    ///     os_version: Some("14.0".to_string()),
    ///     user_agent: None,
    /// };
    ///
    /// let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);
    /// let matched = condition.matches(&info);
    /// ```
    pub fn matches(&self, info: &DeviceInfo, condition: &DeviceCondition) -> bool {
        condition.matches(info)
    }

    /// 添加自定义规则
    ///
    /// # 参数
    /// - `name`: 规则名称
    /// - `pattern`: 匹配模式(正则表达式)
    /// - `device_type`: 设备类型
    /// - `browser`: 浏览器名称(可选)
    /// - `os`: 操作系统(可选)
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::{DeviceMatcher, DeviceType};
    ///
    /// let mut matcher = DeviceMatcher::new().await?;
    /// matcher.add_custom_rule(
    ///     "MyCustomApp",
    ///     r"MyCustomApp/\d+\.\d+",
    ///     DeviceType::Mobile,
    ///     Some("MyCustomApp".to_string()),
    ///     Some("Android".to_string()),
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn add_custom_rule(
        &mut self,
        name: &str,
        pattern: &str,
        device_type: DeviceType,
        browser: Option<String>,
        os: Option<String>,
    ) {
        let rule = DeviceCustomRule {
            name: name.to_string(),
            pattern: pattern.to_string(),
            device_type,
            browser,
            os,
        };

        // 验证正则表达式
        if regex::Regex::new(&rule.pattern).is_err() {
            warn!("无效的正则表达式: {}", pattern);
            return;
        }

        self.custom_rules.push(rule);
        info!("添加自定义规则: {}", name);
    }

    /// 移除自定义规则
    ///
    /// # 参数
    /// - `name`: 规则名称
    ///
    /// # 返回
    /// - `true`: 成功移除
    /// - `false`: 规则不存在
    pub fn remove_custom_rule(&mut self, name: &str) -> bool {
        let original_len = self.custom_rules.len();
        self.custom_rules.retain(|r| r.name != name);
        let removed = self.custom_rules.len() < original_len;
        if removed {
            info!("移除自定义规则: {}", name);
        }
        removed
    }

    /// 清空缓存
    #[instrument(skip(self))]
    pub fn clear_cache(&self) {
        let size = self.cache.len();
        self.cache.clear();
        info!("缓存已清空,移除 {} 条记录", size);
    }

    /// 获取缓存统计信息
    pub fn cache_stats(&self) -> DeviceCacheStats {
        DeviceCacheStats {
            size: self.cache.len(),
            limit: self.cache_size_limit,
            hit_rate: 0.0, // 需要额外统计
        }
    }

    /// 更新缓存
    fn update_cache(&self, user_agent: &str, info: &DeviceInfo) {
        if self.cache.len() >= self.cache_size_limit {
            // 缓存已满,清理最旧的条目(简单实现:清理10%)
            let remove_count = self.cache_size_limit / 10;
            let keys_to_remove: Vec<_> = self
                .cache
                .iter()
                .take(remove_count)
                .map(|k| k.key().clone())
                .collect();
            for key in keys_to_remove {
                self.cache.remove(&key);
            }
            debug!("缓存清理完成,移除 {} 条记录", remove_count);
        }

        self.cache.insert(user_agent.to_string(), info.clone());
    }

    /// 默认自定义规则
    fn default_custom_rules() -> Vec<DeviceCustomRule> {
        vec![
            // 常见的API客户端
            DeviceCustomRule {
                name: "curl".to_string(),
                pattern: r"^curl/".to_string(),
                device_type: DeviceType::API,
                browser: Some("curl".to_string()),
                os: None,
            },
            DeviceCustomRule {
                name: "wget".to_string(),
                pattern: r"^Wget/".to_string(),
                device_type: DeviceType::API,
                browser: Some("wget".to_string()),
                os: None,
            },
            // 常见的爬虫
            DeviceCustomRule {
                name: "googlebot".to_string(),
                pattern: r"Googlebot".to_string(),
                device_type: DeviceType::API,
                browser: Some("Googlebot".to_string()),
                os: None,
            },
            DeviceCustomRule {
                name: "bingbot".to_string(),
                pattern: r"Bingbot".to_string(),
                device_type: DeviceType::API,
                browser: Some("Bingbot".to_string()),
                os: None,
            },
        ]
    }
}

// ============================================================================
// 缓存统计信息
// ============================================================================

/// 设备缓存统计信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceCacheStats {
    /// 当前缓存大小
    pub size: usize,
    /// 缓存大小限制
    pub limit: usize,
    /// 缓存命中率(需要额外统计)
    pub hit_rate: f64,
}

// ============================================================================
// 单元测试
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_device_type_from_str() {
        assert_eq!(DeviceType::parse("mobile"), DeviceType::Mobile);
        assert_eq!(DeviceType::parse("desktop"), DeviceType::Desktop);
        assert_eq!(DeviceType::parse("tablet"), DeviceType::Tablet);
        assert_eq!(DeviceType::parse("api"), DeviceType::API);
        assert_eq!(DeviceType::parse("unknown"), DeviceType::Unknown);
        assert_eq!(DeviceType::parse("invalid"), DeviceType::Unknown);
    }

    #[test]
    fn test_device_type_as_str() {
        assert_eq!(DeviceType::Mobile.as_str(), "mobile");
        assert_eq!(DeviceType::Desktop.as_str(), "desktop");
        assert_eq!(DeviceType::Tablet.as_str(), "tablet");
        assert_eq!(DeviceType::API.as_str(), "api");
        assert_eq!(DeviceType::Unknown.as_str(), "unknown");
    }

    #[test]
    fn test_device_type_is_mobile() {
        assert!(DeviceType::Mobile.is_mobile());
        assert!(DeviceType::Tablet.is_mobile());
        assert!(!DeviceType::Desktop.is_mobile());
        assert!(!DeviceType::API.is_mobile());
    }

    #[test]
    fn test_device_info_empty() {
        let info = DeviceInfo::empty();
        assert!(info.is_empty());
        assert_eq!(info.description(), "unknown");
    }

    #[test]
    fn test_device_info_description() {
        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Safari".to_string()),
            browser_version: Some("14.0".to_string()),
            os: Some("iOS".to_string()),
            os_version: Some("14.0".to_string()),
            user_agent: None,
        };
        assert!(info1.description().contains("Safari"));
        assert!(info1.description().contains("iOS"));

        let info2 = DeviceInfo {
            device_type: DeviceType::API,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert_eq!(info2.description(), "API Client");
    }

    #[test]
    fn test_device_condition_empty() {
        let condition = DeviceCondition::empty();
        assert!(condition.is_empty());

        let info = DeviceInfo::empty();
        assert!(condition.matches(&info));
    }

    #[test]
    fn test_device_condition_device_types() {
        let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);

        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(condition.matches(&info1));

        let info2 = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info2));
    }

    #[test]
    fn test_device_condition_browsers() {
        let condition = DeviceCondition::browsers(vec!["Safari".to_string(), "Chrome".to_string()]);

        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Safari".to_string()),
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(condition.matches(&info1));

        let info2 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Firefox".to_string()),
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info2));
    }

    #[test]
    fn test_device_condition_os() {
        let condition = DeviceCondition::os(vec!["iOS".to_string(), "Android".to_string()]);

        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: None,
            browser_version: None,
            os: Some("iOS".to_string()),
            os_version: None,
            user_agent: None,
        };
        assert!(condition.matches(&info1));

        let info2 = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: Some("Windows".to_string()),
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info2));
    }

    #[test]
    fn test_device_condition_default() {
        let condition = DeviceCondition::default();
        assert!(condition.is_empty());
    }

    #[test]
    fn test_device_info_serialization() {
        let info = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Safari".to_string()),
            browser_version: Some("14.0".to_string()),
            os: Some("iOS".to_string()),
            os_version: Some("14.0".to_string()),
            user_agent: Some("Test".to_string()),
        };

        let json = serde_json::to_string(&info).unwrap();
        let deserialized: DeviceInfo = serde_json::from_str(&json).unwrap();

        assert_eq!(info, deserialized);
    }

    #[test]
    fn test_device_condition_serialization() {
        let condition = DeviceCondition {
            device_types: vec![DeviceType::Mobile, DeviceType::Tablet],
            browsers: vec!["Safari".to_string()],
            os: vec!["iOS".to_string()],
        };

        let json = serde_json::to_string(&condition).unwrap();
        let deserialized: DeviceCondition = serde_json::from_str(&json).unwrap();

        assert_eq!(condition, deserialized);
    }

    #[tokio::test]
    async fn test_device_matcher_parse_empty() {
        let matcher = DeviceMatcher::new().await.unwrap();
        // 空字符串应该被清理为空,然后返回空的 DeviceInfo
        let info = matcher.parse("").unwrap();
        assert!(info.is_empty());

        // 只有空格的字符串也应该返回空的 DeviceInfo
        let info = matcher.parse("   ").unwrap();
        assert!(info.is_empty());
    }

    #[tokio::test]
    async fn test_device_matcher_parse_iphone() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) \
                          AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 \
                          Safari/604.1";
        let info = matcher.parse(user_agent).unwrap();
        assert_eq!(info.device_type, DeviceType::Mobile);
        assert!(info.browser.as_ref().unwrap().contains("Safari"));
        // woothee可能返回不同的OS名称,所以只检查不为空
        assert!(info.os.is_some());
    }

    #[tokio::test]
    async fn test_device_matcher_parse_desktop() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, \
                          like Gecko) Chrome/91.0.4472.124 Safari/537.36";
        let info = matcher.parse(user_agent).unwrap();
        assert_eq!(info.device_type, DeviceType::Desktop);
        assert!(info.browser.as_ref().unwrap().contains("Chrome"));
        assert!(info.os.as_ref().unwrap().contains("Windows"));
    }

    #[tokio::test]
    async fn test_device_matcher_parse_curl() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "curl/7.68.0";
        let info = matcher.parse(user_agent).unwrap();
        assert_eq!(info.device_type, DeviceType::API);
        assert_eq!(info.browser, Some("curl".to_string()));
    }

    #[tokio::test]
    async fn test_device_matcher_cache() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";

        // 第一次解析
        let info1 = matcher.parse(user_agent).unwrap();
        let stats1 = matcher.cache_stats();
        assert_eq!(stats1.size, 1);

        // 第二次解析(应该命中缓存)
        let info2 = matcher.parse(user_agent).unwrap();
        assert_eq!(info1, info2);

        // 清空缓存
        matcher.clear_cache();
        let stats2 = matcher.cache_stats();
        assert_eq!(stats2.size, 0);
    }

    #[tokio::test]
    async fn test_device_matcher_custom_rule() {
        let mut matcher = DeviceMatcher::new().await.unwrap();

        matcher.add_custom_rule(
            "TestApp",
            r"TestApp/\d+\.\d+",
            DeviceType::Mobile,
            Some("TestApp".to_string()),
            Some("Android".to_string()),
        );

        let user_agent = "TestApp/1.0.0";
        let info = matcher.parse(user_agent).unwrap();
        assert_eq!(info.device_type, DeviceType::Mobile);
        assert_eq!(info.browser, Some("TestApp".to_string()));
    }

    #[tokio::test]
    async fn test_device_matcher_remove_custom_rule() {
        let mut matcher = DeviceMatcher::new().await.unwrap();

        matcher.add_custom_rule(
            "TestApp",
            r"TestApp/\d+\.\d+",
            DeviceType::Mobile,
            Some("TestApp".to_string()),
            Some("Android".to_string()),
        );

        assert!(matcher.remove_custom_rule("TestApp"));
        assert!(!matcher.remove_custom_rule("NonExistent"));
    }

    #[tokio::test]
    async fn test_device_matcher_batch_parse() {
        let matcher = DeviceMatcher::new().await.unwrap();

        let user_agents = vec![
            "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)".to_string(),
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36".to_string(),
            "curl/7.68.0".to_string(),
        ];

        let results = matcher.batch_parse(&user_agents);
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_ok()));

        assert_eq!(results[0].as_ref().unwrap().device_type, DeviceType::Mobile);
        assert_eq!(
            results[1].as_ref().unwrap().device_type,
            DeviceType::Desktop
        );
        assert_eq!(results[2].as_ref().unwrap().device_type, DeviceType::API);
    }

    #[tokio::test]
    async fn test_device_matcher_matches_user_agent() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);

        let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";
        let matched = matcher.matches_user_agent(user_agent, &condition).unwrap();
        assert!(matched);

        let user_agent2 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)";
        let matched2 = matcher.matches_user_agent(user_agent2, &condition).unwrap();
        assert!(!matched2);
    }
}