Skip to main content

redact_core/recognizers/
mod.rs

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