Skip to main content

normalizer_tr/
resources.rs

1use crate::NormalizeError;
2use regex::Regex;
3use std::sync::{Arc, OnceLock};
4
5/// Diagnostic identity of this built-in normalizer, not a selectable behavior profile.
6pub const NORMALIZER_ID: &str = concat!("normalizer-tr/", env!("CARGO_PKG_VERSION"));
7
8/// Immutable compiled patterns. All lexical/phonological data is typed in domain::lexicon.
9pub(crate) struct Resources {
10    pub(crate) tokens: Regex,
11    pub(crate) phone_like: Regex,
12    pub(crate) quoted_email: Regex,
13}
14
15impl Resources {
16    pub(crate) fn shared() -> Result<Arc<Self>, NormalizeError> {
17        static INSTANCE: OnceLock<Result<Arc<Resources>, NormalizeError>> = OnceLock::new();
18        INSTANCE.get_or_init(|| Self::load().map(Arc::new)).clone()
19    }
20    pub(crate) fn load() -> Result<Self, NormalizeError> {
21        let compile =
22            |pattern| Regex::new(pattern).map_err(|_| NormalizeError::InvalidConfiguration);
23        Ok(Self {
24            tokens: compile(r"\S+")?,
25            phone_like: compile(r"(?:\+?[0-9]{1,3})(?:[ \t]+(?:\([0-9]{2,}\)|[0-9]{2,})){2,}")?,
26            quoted_email: compile(r#""[^"\r\n]{0,128}"@[^\s]+"#)?,
27        })
28    }
29}