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