use core::fmt;
use core::ops::Range;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Kind {
Tckn,
Vkn,
Iban,
Phone,
Email,
Card,
Plate,
Name,
Ip,
BirthDate,
Passport,
Custom,
Address,
SocialSecurity,
}
impl Kind {
pub const ALL: [Kind; 14] = [
Kind::Tckn,
Kind::Vkn,
Kind::Iban,
Kind::Phone,
Kind::Email,
Kind::Card,
Kind::Plate,
Kind::Name,
Kind::Ip,
Kind::BirthDate,
Kind::Passport,
Kind::Custom,
Kind::Address,
Kind::SocialSecurity,
];
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Kind::Tckn => "TCKN",
Kind::Vkn => "VKN",
Kind::Iban => "IBAN",
Kind::Phone => "TELEFON",
Kind::Email => "EPOSTA",
Kind::Card => "KART",
Kind::Plate => "PLAKA",
Kind::Name => "AD_SOYAD",
Kind::Ip => "IP",
Kind::BirthDate => "DOGUM_TARIHI",
Kind::Passport => "PASAPORT",
Kind::Custom => "OZEL",
Kind::Address => "ADRES",
Kind::SocialSecurity => "SGK",
}
}
#[must_use]
pub fn from_label(s: &str) -> Option<Kind> {
let up = s.trim().to_ascii_uppercase();
Kind::ALL.into_iter().find(|k| k.label() == up).or(match up.as_str() {
"TC" | "TC_KIMLIK" => Some(Kind::Tckn),
"PHONE" | "TEL" => Some(Kind::Phone),
"EMAIL" | "MAIL" => Some(Kind::Email),
"CARD" | "KREDI_KARTI" => Some(Kind::Card),
"PLATE" => Some(Kind::Plate),
"NAME" | "AD" | "ISIM" => Some(Kind::Name),
"IP_ADRESI" => Some(Kind::Ip),
"DOGUM" | "BIRTHDATE" | "DOB" => Some(Kind::BirthDate),
"PASSPORT" => Some(Kind::Passport),
"CUSTOM" | "TERIM" => Some(Kind::Custom),
"ADDRESS" | "ADRES" => Some(Kind::Address),
"SSK" | "SGK_NO" | "SOCIAL_SECURITY" => Some(Kind::SocialSecurity),
_ => None,
})
}
pub(crate) const fn bit(self) -> u16 {
1 << (self as u16)
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.label())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Match {
pub kind: Kind,
pub span: Range<usize>,
pub confidence: u8,
}
impl Match {
pub(crate) fn new(kind: Kind, span: Range<usize>, confidence: u8) -> Self {
Self { kind, span, confidence }
}
#[must_use]
pub fn text<'a>(&self, source: &'a str) -> &'a str {
&source[self.span.clone()]
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KindSet(u16);
impl KindSet {
#[must_use]
pub const fn all() -> Self {
KindSet((1 << Kind::ALL.len()) - 1)
}
#[must_use]
pub const fn empty() -> Self {
KindSet(0)
}
#[must_use]
pub fn with(mut self, k: Kind) -> Self {
self.0 |= k.bit();
self
}
#[must_use]
pub fn without(mut self, k: Kind) -> Self {
self.0 &= !k.bit();
self
}
#[must_use]
pub const fn contains(self, k: Kind) -> bool {
self.0 & k.bit() != 0
}
}
impl Default for KindSet {
fn default() -> Self {
Self::all()
}
}
impl FromIterator<Kind> for KindSet {
fn from_iter<I: IntoIterator<Item = Kind>>(iter: I) -> Self {
iter.into_iter().fold(KindSet::empty(), KindSet::with)
}
}