ens_normalize_rs/
normalizer.rs

1use crate::{
2    beautify::beautify_labels, join::join_labels, validate::validate_name, CodePointsSpecs,
3    ProcessError, TokenizedName, ValidatedLabel,
4};
5
6#[derive(Default)]
7pub struct EnsNameNormalizer {
8    specs: CodePointsSpecs,
9}
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct ProcessedName {
13    pub labels: Vec<ValidatedLabel>,
14    pub tokenized: TokenizedName,
15}
16
17impl EnsNameNormalizer {
18    pub fn new(specs: CodePointsSpecs) -> Self {
19        Self { specs }
20    }
21
22    pub fn tokenize(&self, input: impl AsRef<str>) -> Result<TokenizedName, ProcessError> {
23        TokenizedName::from_input(input.as_ref(), &self.specs, true)
24    }
25
26    pub fn process(&self, input: impl AsRef<str>) -> Result<ProcessedName, ProcessError> {
27        let input = input.as_ref();
28        let tokenized = self.tokenize(input)?;
29        let labels = validate_name(&tokenized, &self.specs)?;
30        Ok(ProcessedName { tokenized, labels })
31    }
32
33    pub fn normalize(&self, input: impl AsRef<str>) -> Result<String, ProcessError> {
34        self.process(input).map(|processed| processed.normalize())
35    }
36
37    pub fn beautify(&self, input: impl AsRef<str>) -> Result<String, ProcessError> {
38        self.process(input).map(|processed| processed.beautify())
39    }
40}
41
42impl ProcessedName {
43    pub fn normalize(&self) -> String {
44        join_labels(&self.labels)
45    }
46
47    pub fn beautify(&self) -> String {
48        beautify_labels(&self.labels)
49    }
50}
51
52pub fn tokenize(input: impl AsRef<str>) -> Result<TokenizedName, ProcessError> {
53    EnsNameNormalizer::default().tokenize(input)
54}
55
56pub fn process(input: impl AsRef<str>) -> Result<ProcessedName, ProcessError> {
57    EnsNameNormalizer::default().process(input)
58}
59
60pub fn normalize(input: impl AsRef<str>) -> Result<String, ProcessError> {
61    EnsNameNormalizer::default().normalize(input)
62}
63
64pub fn beautify(input: impl AsRef<str>) -> Result<String, ProcessError> {
65    EnsNameNormalizer::default().beautify(input)
66}