@@ -8,7 +8,7 @@ use move_core_types::{ident_str, identifier::IdentStr, language_storage::StructT
88use serde:: { Deserialize , Serialize } ;
99
1010use crate :: {
11- config:: { ACCEPTED_SEPARATORS , DEFAULT_TLD , IOTA_NEW_FORMAT_SEPARATOR } ,
11+ config:: { ACCEPTED_SEPARATORS , DEFAULT_TLD , IOTA_AT_FORMAT_SEPARATOR } ,
1212 error:: IotaNamesError ,
1313} ;
1414
@@ -18,13 +18,6 @@ pub struct Domain {
1818 labels : Vec < String > ,
1919}
2020
21- // impl std::fmt::Display for Domain {
22- // fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23- // let labels = self.labels.iter().rev().cloned().collect::<Vec<_>>();
24- // write!(f, "{}", labels.join("."))
25- // }
26- // }
27-
2821// impl FromStr for Domain {
2922// type Err = anyhow::Error;
3023
@@ -54,7 +47,7 @@ impl FromStr for Domain {
5447 }
5548 let separator = separator ( s) ?;
5649
57- let formatted_string = convert_from_new_format ( s, & separator) ?;
50+ let formatted_string = convert_from_at_format ( s, & separator) ?;
5851
5952 let labels = formatted_string
6053 . split ( separator)
@@ -140,6 +133,9 @@ impl Domain {
140133 /// Returns the number of labels including TLD.
141134 ///
142135 /// ```
136+ /// use std::str::FromStr;
137+ ///
138+ /// use iota_names::domain::Domain;
143139 /// assert_eq!(
144140 /// Domain::from_str("test.example.iota").unwrap().num_labels(),
145141 /// 3
@@ -176,7 +172,7 @@ impl Domain {
176172 let _tld = labels. pop ( ) ;
177173 let sld = labels. pop ( ) . unwrap ( ) ;
178174
179- format ! ( "{}{}{ }" , labels. join( sep) , IOTA_NEW_FORMAT_SEPARATOR , sld )
175+ format ! ( "{}{IOTA_AT_FORMAT_SEPARATOR}{sld }" , labels. join( sep) )
180176 }
181177}
182178
@@ -212,8 +208,8 @@ fn separator(s: &str) -> Result<char, IotaNamesError> {
212208/// Converts @label ending to label{separator}iota ending.
213209///
214210/// E.g. `@example` -> `example.iota` | `test@example` -> `test.example.iota`
215- fn convert_from_new_format ( s : & str , separator : & char ) -> Result < String , IotaNamesError > {
216- let mut splits = s. split ( IOTA_NEW_FORMAT_SEPARATOR ) ;
211+ fn convert_from_at_format ( s : & str , separator : & char ) -> Result < String , IotaNamesError > {
212+ let mut splits = s. split ( IOTA_AT_FORMAT_SEPARATOR ) ;
217213
218214 let Some ( before) = splits. next ( ) else {
219215 return Err ( IotaNamesError :: InvalidSeparator ) ;
@@ -239,9 +235,14 @@ fn convert_from_new_format(s: &str, separator: &char) -> Result<String, IotaName
239235 Ok ( parts. join ( & separator. to_string ( ) ) )
240236}
241237
238+ /// Checks the validity of a label according to these rules:
239+ /// - length must be in [MIN_LABEL_LENGTH..MAX_LABEL_LENGTH]
240+ /// - must contain only '0'..'9', 'a'..'z' and '-'
241+ /// - must not start or end with '-'
242242pub fn validate_label ( label : & str ) -> Result < & str , IotaNamesError > {
243243 const MIN_LABEL_LENGTH : usize = 1 ;
244244 const MAX_LABEL_LENGTH : usize = 63 ;
245+
245246 let bytes = label. as_bytes ( ) ;
246247 let len = bytes. len ( ) ;
247248
@@ -254,39 +255,16 @@ pub fn validate_label(label: &str) -> Result<&str, IotaNamesError> {
254255 }
255256
256257 for ( i, character) in bytes. iter ( ) . enumerate ( ) {
257- let is_valid_character = match character {
258- b'a' ..=b'z' => true ,
259- b'0' ..=b'9' => true ,
260- b'-' if i != 0 && i != len - 1 => true ,
261- _ => false ,
262- } ;
263-
264- if !is_valid_character {
265- match character {
266- b'-' => return Err ( IotaNamesError :: InvalidHyphens ) ,
267- _ => return Err ( IotaNamesError :: InvalidUnderscore ) ,
268- }
258+ match character {
259+ b'a' ..=b'z' | b'0' ..=b'9' => continue ,
260+ b'-' if i == 0 || i == len - 1 => return Err ( IotaNamesError :: HyphensAsFirstOrLastChar ) ,
261+ _ => return Err ( IotaNamesError :: InvalidLabelChar ) ,
269262 } ;
270263 }
264+
271265 Ok ( label)
272266}
273267
274- // fn parse_domain_label(label: &str) -> anyhow::Result<String> {
275- // anyhow::ensure!(
276- // label.len() >= MIN_LABEL_LEN && label.len() <= MAX_LABEL_LEN,
277- // "label length outside allowed range
278- // [{MIN_LABEL_LEN}..{MAX_LABEL_LEN}]: {}", label.len()
279- // );
280- // let regex =
281- // regex::Regex::new("^[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]$").unwrap();
282-
283- // anyhow::ensure!(
284- // regex.is_match(label),
285- // "invalid characters in domain: {label}"
286- // );
287- // Ok(label.to_owned())
288- // }
289-
290268#[ cfg( test) ]
291269mod tests {
292270 use super :: * ;
0 commit comments