Skip to main content

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/// Main struct to handle ENS name normalization including
7/// tokenization, validation, beautification and normalization
8#[derive(Default)]
9pub struct EnsNameNormalizer {
10    specs: CodePointsSpecs,
11}
12
13/// Result of processing an ENS name.
14/// Contains tokenized name as intermediate processing result and validated labels.
15/// Validated labels can be normalized and beautified.
16#[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    /// Tokenize the input string, return a `TokenizedName` object with `Vec<EnsNameToken>` inside
28    pub fn tokenize(&self, input: impl AsRef<str>) -> Result<TokenizedName, ProcessError> {
29        TokenizedName::from_input(input.as_ref(), &self.specs, true)
30    }
31
32    /// Process the input string, return a `ProcessedName` object with `Vec<ValidatedLabel>` inside
33    /// This function will tokenize and validate the name. Processed name can be normalized and beautified.
34    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    /// Normalize the input string, return a normalized version of ENS name
42    pub fn normalize(&self, input: impl AsRef<str>) -> Result<String, ProcessError> {
43        self.process(input).map(|processed| processed.normalize())
44    }
45
46    /// Beautify the input string, return a beautified version of ENS name/// Beautify the input string, return a beautified version of ENS name
47    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
62/// `no-cache` version of [`EnsNameNormalizer::tokenize`]
63pub fn tokenize(input: impl AsRef<str>) -> Result<TokenizedName, ProcessError> {
64    EnsNameNormalizer::default().tokenize(input)
65}
66
67/// `no-cache` version of [`EnsNameNormalizer::process`]
68pub fn process(input: impl AsRef<str>) -> Result<ProcessedName, ProcessError> {
69    EnsNameNormalizer::default().process(input)
70}
71
72/// `no-cache` version of [`EnsNameNormalizer::normalize`]
73pub fn normalize(input: impl AsRef<str>) -> Result<String, ProcessError> {
74    EnsNameNormalizer::default().normalize(input)
75}
76
77/// `no-cache` version of [`EnsNameNormalizer::beautify`]
78pub fn beautify(input: impl AsRef<str>) -> Result<String, ProcessError> {
79    EnsNameNormalizer::default().beautify(input)
80}