|
| 1 | +use regex::Regex; |
| 2 | +use std::fmt; |
| 3 | + |
| 4 | +/// Regular expression for the definition of alias |
| 5 | +const REGEXP: &str = "^[-.0-9@_a-z]{4,30}$"; |
| 6 | +/// Regular expression for the definition of alias with prefix |
| 7 | +const REGEXP_WITH_PREFIX: &str = "^alias:[A-Z]{1}:[-.0-9@_a-z]{4,30}$"; |
| 8 | + |
| 9 | +/// List of errors in processing [`Alias`] |
| 10 | +#[derive(Debug, PartialEq, Eq)] |
| 11 | +pub enum AliasError { |
| 12 | + InvalidAlias, |
| 13 | + RegexError, |
| 14 | +} |
| 15 | + |
| 16 | +/// The [`Alias`] type on working with aliases in the Waves blockchain and presenting it in a format with or without the network prefix |
| 17 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 18 | +pub struct Alias(String); |
| 19 | + |
| 20 | +impl Alias { |
| 21 | + /// Create an [`Alias`] from the string |
| 22 | + pub fn new(alias: &str) -> Result<Alias, AliasError> { |
| 23 | + if Self::is_valid(REGEXP, alias)? { |
| 24 | + Ok(Alias(format!("{}", alias))) |
| 25 | + } else if Self::is_valid(REGEXP_WITH_PREFIX, alias)? { |
| 26 | + let value = Self::replace_prefix(alias)?; |
| 27 | + Ok(Alias(value)) |
| 28 | + } else { |
| 29 | + Err(AliasError::InvalidAlias) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// Representing [`Alias`] as a string with a prefix |
| 34 | + pub fn to_string_with_prefix(&self, chain_id: u8) -> String { |
| 35 | + format!("alias:{}:{}", chain_id as char, self.0) |
| 36 | + } |
| 37 | + |
| 38 | + /// Validation of alias using regular expressions |
| 39 | + fn is_valid(regexp: &str, alias: &str) -> Result<bool, AliasError> { |
| 40 | + match Regex::new(regexp) { |
| 41 | + Ok(r) => Ok(r.is_match(alias)), |
| 42 | + Err(_) => Err(AliasError::RegexError), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Removing the prefix with regular expressions |
| 47 | + fn replace_prefix(alias: &str) -> Result<String, AliasError> { |
| 48 | + match Regex::new("^alias:[A-Z]{1}:") { |
| 49 | + Ok(r) => Ok(r.replace(alias, "").to_string()), |
| 50 | + Err(_) => Err(AliasError::RegexError), |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl fmt::Display for Alias { |
| 56 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 57 | + write!(f, "{}", self.0) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + use crate::account::TESTNET; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn test_alias() { |
| 68 | + let result = Alias::new("test"); |
| 69 | + assert!(result.is_ok()); |
| 70 | + |
| 71 | + let alias = result.unwrap(); |
| 72 | + |
| 73 | + assert_eq!(alias.to_string(), "test"); |
| 74 | + assert_eq!(alias.to_string_with_prefix(TESTNET), "alias:T:test"); |
| 75 | + |
| 76 | + let result = Alias::new("a"); |
| 77 | + assert_eq!(result, Err(AliasError::InvalidAlias)); |
| 78 | + |
| 79 | + let result = Alias::new("3MzGEv9wnaqrYFYujAXSH5RQfHaVKNQvx3D"); |
| 80 | + assert_eq!(result, Err(AliasError::InvalidAlias)); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_alias_with_prefix() { |
| 85 | + let result = Alias::new("alias:T:test"); |
| 86 | + assert!(result.is_ok()); |
| 87 | + |
| 88 | + let alias = result.unwrap(); |
| 89 | + |
| 90 | + assert_eq!(alias.to_string(), "test"); |
| 91 | + assert_eq!(alias.to_string_with_prefix(TESTNET), "alias:T:test"); |
| 92 | + } |
| 93 | +} |
0 commit comments