Skip to main content

redact_core/recognizers/
mod.rs

1// Copyright 2026 Censgate LLC.
2// Licensed under the Apache License, Version 2.0. See the LICENSE file
3// in the project root for license information.
4
5pub mod pattern;
6pub mod registry;
7pub mod validation;
8
9pub use registry::RecognizerRegistry;
10pub use validation::validate_entity;
11
12use crate::types::{EntityType, RecognizerResult};
13use anyhow::Result;
14use std::fmt::Debug;
15
16/// Trait for all PII recognizers
17pub trait Recognizer: Send + Sync + Debug {
18    /// Get the name of this recognizer
19    fn name(&self) -> &str;
20
21    /// Get the entity types this recognizer can detect
22    fn supported_entities(&self) -> &[EntityType];
23
24    /// Analyze text and return detected entities
25    fn analyze(&self, text: &str, language: &str) -> Result<Vec<RecognizerResult>>;
26
27    /// Get the minimum confidence score for this recognizer
28    fn min_score(&self) -> f32 {
29        0.0
30    }
31
32    /// Check if this recognizer supports the given language
33    fn supports_language(&self, language: &str) -> bool {
34        language == "en" // Default to English only
35    }
36}
37
38/// Trait for recognizers that can be loaded from configuration
39pub trait ConfigurableRecognizer: Recognizer {
40    /// Configuration type for this recognizer
41    type Config;
42
43    /// Create a new instance from configuration
44    fn from_config(config: Self::Config) -> Result<Self>
45    where
46        Self: Sized;
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[derive(Debug)]
54    struct TestRecognizer;
55
56    impl Recognizer for TestRecognizer {
57        fn name(&self) -> &str {
58            "test"
59        }
60
61        fn supported_entities(&self) -> &[EntityType] {
62            &[EntityType::Person]
63        }
64
65        fn analyze(&self, _text: &str, _language: &str) -> Result<Vec<RecognizerResult>> {
66            Ok(vec![])
67        }
68    }
69
70    #[test]
71    fn test_recognizer_trait() {
72        let recognizer = TestRecognizer;
73        assert_eq!(recognizer.name(), "test");
74        assert_eq!(recognizer.supported_entities(), &[EntityType::Person]);
75        assert!(recognizer.supports_language("en"));
76        assert!(!recognizer.supports_language("es"));
77    }
78}