Skip to main content

pii_vault/
entity.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub struct EntityType(pub String);
6
7impl EntityType {
8    pub fn new(name: &str) -> Self {
9        Self(name.to_uppercase())
10    }
11
12    pub fn as_str(&self) -> &str {
13        &self.0
14    }
15}
16
17impl fmt::Display for EntityType {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(f, "{}", self.0)
20    }
21}
22
23impl From<&str> for EntityType {
24    fn from(s: &str) -> Self {
25        Self::new(s)
26    }
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct RecognizerResult {
31    pub entity_type: EntityType,
32    pub start: usize,
33    pub end: usize,
34    pub score: f64,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub recognizer_name: Option<String>,
37}
38
39impl RecognizerResult {
40    pub fn text<'a>(&self, input: &'a str) -> &'a str {
41        &input[self.start..self.end]
42    }
43
44    pub fn overlaps(&self, other: &RecognizerResult) -> bool {
45        self.start < other.end && other.start < self.end
46    }
47}
48
49pub const EMAIL_ADDRESS: &str = "EMAIL_ADDRESS";
50pub const PHONE_NUMBER: &str = "PHONE_NUMBER";
51pub const CREDIT_CARD: &str = "CREDIT_CARD";
52pub const CRYPTO: &str = "CRYPTO";
53pub const IP_ADDRESS: &str = "IP_ADDRESS";
54pub const MAC_ADDRESS: &str = "MAC_ADDRESS";
55pub const IBAN_CODE: &str = "IBAN_CODE";
56pub const URL: &str = "URL";
57pub const UUID: &str = "UUID";
58pub const US_SSN: &str = "US_SSN";
59pub const US_ITIN: &str = "US_ITIN";
60pub const CN_ID_CARD: &str = "CN_ID_CARD";
61pub const CN_PHONE: &str = "CN_PHONE";
62pub const UK_NHS: &str = "UK_NHS";
63pub const UK_NINO: &str = "UK_NINO";