#![cfg_attr(not(feature = "ffi"), forbid(unsafe_code))]
#![warn(missing_docs)]
pub mod checksum;
mod detect;
mod entity;
mod mask;
mod text;
pub use detect::Terms;
pub use entity::{Kind, KindSet, Match};
pub use mask::{LeakReport, Session, Strategy, StreamRestorer};
#[cfg(feature = "ffi")]
pub mod ffi;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct Kalkan {
kinds: KindSet,
strategy: Strategy,
min_confidence: u8,
secret: u64,
terms: Option<Arc<Terms>>,
}
impl Default for Kalkan {
fn default() -> Self {
Self {
kinds: KindSet::all(),
strategy: Strategy::Token,
min_confidence: 70,
secret: 0x6b61_6c6b_616e,
terms: None,
}
}
}
impl Kalkan {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn builder() -> Builder {
Builder(Self::default())
}
#[must_use]
pub fn detect(&self, text: &str) -> Vec<Match> {
detect::detect(text, self.kinds, self.min_confidence, self.terms.as_deref())
}
#[must_use]
pub fn session(&self) -> Session {
Session::new(self.clone())
}
#[must_use]
pub fn mask(&self, text: &str) -> String {
self.session().mask(text)
}
#[must_use]
pub fn contains_pii(&self, text: &str) -> bool {
!self.detect(text).is_empty()
}
#[must_use]
pub fn strategy(&self) -> Strategy {
self.strategy
}
}
#[derive(Debug, Clone)]
pub struct Builder(Kalkan);
impl Builder {
#[must_use]
pub fn strategy(mut self, s: Strategy) -> Self {
self.0.strategy = s;
self
}
#[must_use]
pub fn kinds(mut self, k: impl IntoIterator<Item = Kind>) -> Self {
self.0.kinds = k.into_iter().collect();
self
}
#[must_use]
pub fn without(mut self, k: Kind) -> Self {
self.0.kinds = self.0.kinds.without(k);
self
}
#[must_use]
pub fn min_confidence(mut self, c: u8) -> Self {
self.0.min_confidence = c;
self
}
#[must_use]
pub fn secret(mut self, s: u64) -> Self {
self.0.secret = s;
self
}
#[must_use]
pub fn terms<I: IntoIterator<Item = S>, S: AsRef<str>>(mut self, terms: I) -> Self {
let t = Terms::new(terms);
self.0.terms = (!t.is_empty()).then(|| Arc::new(t));
self
}
#[must_use]
pub fn build(self) -> Kalkan {
self.0
}
}