rust-expect 0.1.0

Next-generation Expect-style terminal automation library 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
//! Custom PII pattern registry.
//!
//! This module provides a centralized registry for managing custom PII patterns.
//! It includes pre-defined pattern sets for common compliance scenarios.
//!
//! # Example
//!
//! ```rust
//! use rust_expect::pii::{PatternRegistry, PiiDetector};
//!
//! // Load pre-built patterns
//! let registry = PatternRegistry::new()
//!     .with_healthcare()
//!     .with_financial();
//!
//! // Apply to detector
//! let detector = registry.apply(PiiDetector::new());
//! ```

use std::collections::HashMap;

use super::detector::{CustomPattern, PiiDetector};

/// A pattern set name for common compliance scenarios.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PatternSet {
    /// Healthcare identifiers (MRN, NPI, DEA).
    Healthcare,
    /// Financial identifiers (IBAN, SWIFT, routing numbers).
    Financial,
    /// Government identifiers (passport, driver license, national ID).
    Government,
    /// Network identifiers (MAC addresses, UUIDs).
    Network,
    /// Authentication (JWT tokens, OAuth tokens).
    Authentication,
    /// Cloud provider identifiers (GCP, Azure keys).
    Cloud,
    /// Cryptocurrency (wallet addresses).
    Cryptocurrency,
    /// Employee identifiers (typical corporate patterns).
    Corporate,
}

/// Entry for a pattern in the registry.
#[derive(Debug, Clone)]
pub struct PatternEntry {
    /// Pattern name.
    pub name: String,
    /// Regex pattern.
    pub pattern: String,
    /// Redaction placeholder.
    pub placeholder: String,
    /// Confidence score (0.0 - 1.0).
    pub confidence: f32,
    /// Description of what this pattern detects.
    pub description: String,
    /// Pattern set this belongs to (if pre-built).
    pub set: Option<PatternSet>,
}

impl PatternEntry {
    /// Create a new pattern entry.
    pub fn new(
        name: impl Into<String>,
        pattern: impl Into<String>,
        placeholder: impl Into<String>,
        confidence: f32,
    ) -> Self {
        Self {
            name: name.into(),
            pattern: pattern.into(),
            placeholder: placeholder.into(),
            confidence,
            description: String::new(),
            set: None,
        }
    }

    /// Add a description.
    #[must_use]
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Mark as part of a pattern set.
    #[must_use]
    pub const fn with_set(mut self, set: PatternSet) -> Self {
        self.set = Some(set);
        self
    }
}

/// Registry of custom PII patterns.
///
/// The registry provides a centralized way to manage custom patterns,
/// including pre-built pattern sets for common compliance scenarios.
#[derive(Debug, Clone, Default)]
pub struct PatternRegistry {
    /// All registered patterns.
    patterns: Vec<PatternEntry>,
    /// Named collections of patterns.
    collections: HashMap<String, Vec<String>>,
}

impl PatternRegistry {
    /// Create a new empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a custom pattern.
    #[must_use]
    #[allow(clippy::should_implement_trait)]
    pub fn add(mut self, entry: PatternEntry) -> Self {
        self.patterns.push(entry);
        self
    }

    /// Add a pattern with minimal parameters.
    #[must_use]
    pub fn add_pattern(
        self,
        name: impl Into<String>,
        pattern: impl Into<String>,
        placeholder: impl Into<String>,
        confidence: f32,
    ) -> Self {
        self.add(PatternEntry::new(name, pattern, placeholder, confidence))
    }

    /// Get all registered patterns.
    #[must_use]
    pub fn patterns(&self) -> &[PatternEntry] {
        &self.patterns
    }

    /// Get pattern count.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.patterns.len()
    }

    /// Check if registry is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.patterns.is_empty()
    }

    /// Get patterns by set.
    #[must_use]
    pub fn get_by_set(&self, set: PatternSet) -> Vec<&PatternEntry> {
        self.patterns
            .iter()
            .filter(|p| p.set == Some(set))
            .collect()
    }

    /// Create a named collection from current patterns.
    #[must_use]
    pub fn save_collection(mut self, name: impl Into<String>) -> Self {
        let names: Vec<String> = self.patterns.iter().map(|p| p.name.clone()).collect();
        self.collections.insert(name.into(), names);
        self
    }

    /// Apply all patterns to a detector.
    #[must_use]
    pub fn apply(&self, mut detector: PiiDetector) -> PiiDetector {
        for entry in &self.patterns {
            if let Ok(pattern) = CustomPattern::new(
                &entry.name,
                &entry.pattern,
                &entry.placeholder,
                entry.confidence,
            ) {
                detector = detector.with_pattern(pattern);
            }
        }
        detector
    }

    /// Create a new detector with only registry patterns.
    #[must_use]
    pub fn to_detector(&self) -> PiiDetector {
        self.apply(PiiDetector::custom_only())
    }

    // ======= Pre-built Pattern Sets =======

    /// Add healthcare patterns (HIPAA-relevant).
    #[must_use]
    pub fn with_healthcare(self) -> Self {
        self.add(
            PatternEntry::new("mrn", r"\bMRN[-:]?\s*\d{6,10}\b", "[MRN REDACTED]", 0.85)
                .with_description("Medical Record Number")
                .with_set(PatternSet::Healthcare),
        )
        .add(
            PatternEntry::new("npi", r"\b(?:NPI[-:]?\s*)?\d{10}\b", "[NPI REDACTED]", 0.75)
                .with_description("National Provider Identifier")
                .with_set(PatternSet::Healthcare),
        )
        .add(
            PatternEntry::new("dea", r"\b[A-Z]{2}\d{7}\b", "[DEA REDACTED]", 0.7)
                .with_description("DEA Number")
                .with_set(PatternSet::Healthcare),
        )
        .add(
            PatternEntry::new(
                "health_plan_id",
                r"\b(?:member[-_]?id|policy[-_]?(?:no|num|number)?)\s*[:=]?\s*[A-Z0-9]{8,15}\b",
                "[HEALTH PLAN ID]",
                0.8,
            )
            .with_description("Health Plan Beneficiary Number")
            .with_set(PatternSet::Healthcare),
        )
    }

    /// Add financial patterns.
    #[must_use]
    pub fn with_financial(self) -> Self {
        self.add(
            PatternEntry::new(
                "iban",
                r"\b[A-Z]{2}\d{2}[A-Z0-9]{4,30}\b",
                "[IBAN REDACTED]",
                0.8,
            )
            .with_description("International Bank Account Number")
            .with_set(PatternSet::Financial),
        )
        .add(
            PatternEntry::new(
                "swift_bic",
                r"\b[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?\b",
                "[SWIFT REDACTED]",
                0.75,
            )
            .with_description("SWIFT/BIC Code")
            .with_set(PatternSet::Financial),
        )
        .add(
            PatternEntry::new("routing_number", r"\b\d{9}\b", "[ROUTING REDACTED]", 0.5)
                .with_description("US Bank Routing Number")
                .with_set(PatternSet::Financial),
        )
        .add(
            PatternEntry::new(
                "account_number",
                r"(?i)\baccount\s*(?:no|num|number|#)?\s*[:=]?\s*(\d{8,17})\b",
                "[ACCOUNT REDACTED]",
                0.85,
            )
            .with_description("Bank Account Number")
            .with_set(PatternSet::Financial),
        )
    }

    /// Add government ID patterns.
    #[must_use]
    pub fn with_government(self) -> Self {
        self
            .add(
                PatternEntry::new(
                    "passport",
                    r"(?i)\bpassport\s*(?:no|num|number|#)?\s*[:=]?\s*([A-Z0-9]{6,12})\b",
                    "[PASSPORT REDACTED]",
                    0.85,
                )
                .with_description("Passport Number")
                .with_set(PatternSet::Government),
            )
            .add(
                PatternEntry::new(
                    "drivers_license",
                    r"(?i)\b(?:dl|driver'?s?\s*lic(?:ense)?)\s*(?:no|num|number|#)?\s*[:=]?\s*([A-Z0-9]{5,15})\b",
                    "[DL REDACTED]",
                    0.8,
                )
                .with_description("Driver's License Number")
                .with_set(PatternSet::Government),
            )
            .add(
                PatternEntry::new(
                    "national_id",
                    r"(?i)\b(?:national\s*id|id\s*card)\s*(?:no|num|number|#)?\s*[:=]?\s*([A-Z0-9]{8,20})\b",
                    "[NATIONAL ID REDACTED]",
                    0.8,
                )
                .with_description("National ID Number")
                .with_set(PatternSet::Government),
            )
    }

    /// Add network patterns.
    #[must_use]
    pub fn with_network(self) -> Self {
        self.add(
            PatternEntry::new(
                "mac_address",
                r"\b(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b",
                "[MAC REDACTED]",
                0.9,
            )
            .with_description("MAC Address")
            .with_set(PatternSet::Network),
        )
        .add(
            PatternEntry::new(
                "uuid",
                r"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b",
                "[UUID REDACTED]",
                0.9,
            )
            .with_description("UUID")
            .with_set(PatternSet::Network),
        )
        .add(
            PatternEntry::new(
                "ipv6",
                r"\b(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b",
                "[IPv6 REDACTED]",
                0.85,
            )
            .with_description("IPv6 Address")
            .with_set(PatternSet::Network),
        )
    }

    /// Add authentication token patterns.
    #[must_use]
    pub fn with_authentication(self) -> Self {
        self.add(
            PatternEntry::new(
                "jwt",
                r"\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\b",
                "[JWT REDACTED]",
                0.95,
            )
            .with_description("JSON Web Token")
            .with_set(PatternSet::Authentication),
        )
        .add(
            PatternEntry::new(
                "bearer_token",
                r"(?i)Bearer\s+([A-Za-z0-9_-]{20,})",
                "[BEARER TOKEN REDACTED]",
                0.9,
            )
            .with_description("Bearer Token")
            .with_set(PatternSet::Authentication),
        )
        .add(
            PatternEntry::new(
                "basic_auth",
                r"(?i)Basic\s+([A-Za-z0-9+/=]{20,})",
                "[BASIC AUTH REDACTED]",
                0.9,
            )
            .with_description("Basic Authentication Header")
            .with_set(PatternSet::Authentication),
        )
        .add(
            PatternEntry::new(
                "oauth_token",
                r#"(?i)(?:access_token|refresh_token)\s*[:=]\s*['"]?([A-Za-z0-9_-]{20,})['"]?"#,
                "[OAUTH TOKEN REDACTED]",
                0.9,
            )
            .with_description("OAuth Token")
            .with_set(PatternSet::Authentication),
        )
    }

    /// Add cloud provider patterns.
    #[must_use]
    pub fn with_cloud(self) -> Self {
        self
            .add(
                PatternEntry::new(
                    "gcp_api_key",
                    r"\bAIza[0-9A-Za-z_-]{35}\b",
                    "[GCP KEY REDACTED]",
                    0.95,
                )
                .with_description("Google Cloud API Key")
                .with_set(PatternSet::Cloud),
            )
            .add(
                PatternEntry::new(
                    "azure_key",
                    r#"(?i)\b(?:azure|subscription)[-_]?(?:key|secret)\s*[:=]\s*['"]?([A-Za-z0-9+/=]{20,})['"]?"#,
                    "[AZURE KEY REDACTED]",
                    0.85,
                )
                .with_description("Azure Key")
                .with_set(PatternSet::Cloud),
            )
            .add(
                PatternEntry::new(
                    "github_token",
                    r"\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}\b",
                    "[GITHUB TOKEN REDACTED]",
                    0.95,
                )
                .with_description("GitHub Token")
                .with_set(PatternSet::Cloud),
            )
            .add(
                PatternEntry::new(
                    "slack_token",
                    r"\bxox[baprs]-[A-Za-z0-9-]+\b",
                    "[SLACK TOKEN REDACTED]",
                    0.95,
                )
                .with_description("Slack Token")
                .with_set(PatternSet::Cloud),
            )
    }

    /// Add cryptocurrency patterns.
    #[must_use]
    pub fn with_cryptocurrency(self) -> Self {
        self.add(
            PatternEntry::new(
                "bitcoin_address",
                r"\b(?:bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}\b",
                "[BTC ADDR REDACTED]",
                0.9,
            )
            .with_description("Bitcoin Address")
            .with_set(PatternSet::Cryptocurrency),
        )
        .add(
            PatternEntry::new(
                "ethereum_address",
                r"\b0x[a-fA-F0-9]{40}\b",
                "[ETH ADDR REDACTED]",
                0.9,
            )
            .with_description("Ethereum Address")
            .with_set(PatternSet::Cryptocurrency),
        )
    }

    /// Add typical corporate patterns.
    #[must_use]
    pub fn with_corporate(self) -> Self {
        self.add(
            PatternEntry::new(
                "employee_id",
                r"\b(?:EMP|E)[-#]?\d{5,8}\b",
                "[EMPLOYEE ID REDACTED]",
                0.85,
            )
            .with_description("Employee ID")
            .with_set(PatternSet::Corporate),
        )
        .add(
            PatternEntry::new(
                "badge_number",
                r"(?i)\bbadge\s*(?:no|num|number|#)?\s*[:=]?\s*(\d{4,10})\b",
                "[BADGE REDACTED]",
                0.8,
            )
            .with_description("Badge Number")
            .with_set(PatternSet::Corporate),
        )
        .add(
            PatternEntry::new(
                "internal_project",
                r"\b(?:PROJ|PRJ|PROJECT)[-#]\d{3,6}\b",
                "[PROJECT ID REDACTED]",
                0.75,
            )
            .with_description("Internal Project Code")
            .with_set(PatternSet::Corporate),
        )
    }

    /// Add all pre-built patterns.
    #[must_use]
    pub fn with_all(self) -> Self {
        self.with_healthcare()
            .with_financial()
            .with_government()
            .with_network()
            .with_authentication()
            .with_cloud()
            .with_cryptocurrency()
            .with_corporate()
    }

    /// Create a HIPAA-focused registry.
    #[must_use]
    pub fn hipaa() -> Self {
        Self::new().with_healthcare()
    }

    /// Create a PCI-DSS-focused registry.
    #[must_use]
    pub fn pci_dss() -> Self {
        Self::new().with_financial()
    }

    /// Create a comprehensive security-focused registry.
    #[must_use]
    pub fn security() -> Self {
        Self::new().with_authentication().with_cloud()
    }
}

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

    #[test]
    fn registry_new() {
        let registry = PatternRegistry::new();
        assert!(registry.is_empty());
    }

    #[test]
    fn registry_add_pattern() {
        let registry = PatternRegistry::new().add_pattern("test", r"\btest\b", "[TEST]", 0.9);

        assert_eq!(registry.len(), 1);
        assert_eq!(registry.patterns()[0].name, "test");
    }

    #[test]
    fn registry_with_healthcare() {
        let registry = PatternRegistry::new().with_healthcare();
        assert!(!registry.is_empty());

        let healthcare_patterns = registry.get_by_set(PatternSet::Healthcare);
        assert!(!healthcare_patterns.is_empty());
        assert!(healthcare_patterns.iter().any(|p| p.name == "mrn"));
    }

    #[test]
    fn registry_apply_to_detector() {
        let registry = PatternRegistry::new().add_pattern("emp_id", r"EMP-\d{6}", "[EMP]", 0.9);

        let detector = registry.apply(PiiDetector::new());
        let matches = detector.detect("Contact EMP-123456");

        assert_eq!(matches.len(), 1);
        assert!(matches[0].is_custom());
        assert_eq!(matches[0].name(), "emp_id");
    }

    #[test]
    fn registry_to_detector() {
        let registry = PatternRegistry::new().add_pattern("test", r"\btest\b", "[TEST]", 0.9);

        let detector = registry.to_detector();
        let matches = detector.detect("this is a test");

        assert_eq!(matches.len(), 1);
    }

    #[test]
    fn registry_with_all() {
        let registry = PatternRegistry::new().with_all();
        assert!(registry.len() > 20); // Should have many patterns
    }

    #[test]
    fn registry_get_by_set() {
        let registry = PatternRegistry::new().with_all();

        let financial = registry.get_by_set(PatternSet::Financial);
        assert!(!financial.is_empty());
        assert!(
            financial
                .iter()
                .all(|p| p.set == Some(PatternSet::Financial))
        );

        let healthcare = registry.get_by_set(PatternSet::Healthcare);
        assert!(!healthcare.is_empty());
    }

    #[test]
    fn pre_built_registries() {
        let hipaa = PatternRegistry::hipaa();
        assert!(!hipaa.is_empty());

        let pci = PatternRegistry::pci_dss();
        assert!(!pci.is_empty());

        let security = PatternRegistry::security();
        assert!(!security.is_empty());
    }

    #[test]
    fn pattern_entry_with_description() {
        let entry = PatternEntry::new("test", r"\btest\b", "[TEST]", 0.9)
            .with_description("A test pattern")
            .with_set(PatternSet::Corporate);

        assert_eq!(entry.description, "A test pattern");
        assert_eq!(entry.set, Some(PatternSet::Corporate));
    }

    #[test]
    fn registry_detects_jwt() {
        let registry = PatternRegistry::new().with_authentication();
        let detector = registry.to_detector();

        // Sample JWT (not valid, just pattern matching)
        let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U";
        let matches = detector.detect(jwt);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].name(), "jwt");
    }

    #[test]
    fn registry_detects_github_token() {
        let registry = PatternRegistry::new().with_cloud();
        let detector = registry.to_detector();

        let text = "GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        let matches = detector.detect(text);

        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].name(), "github_token");
    }

    #[test]
    fn registry_detects_mac_address() {
        let registry = PatternRegistry::new().with_network();
        let detector = registry.to_detector();

        let matches = detector.detect("MAC: 00:1B:44:11:3A:B7");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].name(), "mac_address");
    }

    #[test]
    fn registry_detects_bitcoin_address() {
        let registry = PatternRegistry::new().with_cryptocurrency();
        let detector = registry.to_detector();

        // Sample BTC address
        let matches = detector.detect("Send to 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].name(), "bitcoin_address");
    }
}