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