iota_types/block/address/
alias.rs1use core::str::FromStr;
5
6use derive_more::{AsRef, Deref, From};
7
8use crate::block::{output::AliasId, Error};
9
10#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
12#[as_ref(forward)]
13pub struct AliasAddress(AliasId);
14
15impl AliasAddress {
16 pub const KIND: u8 = 8;
18 pub const LENGTH: usize = AliasId::LENGTH;
20
21 #[inline(always)]
23 pub fn new(id: AliasId) -> Self {
24 Self::from(id)
25 }
26
27 #[inline(always)]
29 pub fn alias_id(&self) -> &AliasId {
30 &self.0
31 }
32
33 #[inline(always)]
35 pub fn into_alias_id(self) -> AliasId {
36 self.0
37 }
38}
39
40#[cfg(feature = "serde")]
41string_serde_impl!(AliasAddress);
42
43impl FromStr for AliasAddress {
44 type Err = Error;
45
46 fn from_str(s: &str) -> Result<Self, Self::Err> {
47 Ok(Self::new(AliasId::from_str(s)?))
48 }
49}
50
51impl core::fmt::Display for AliasAddress {
52 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53 write!(f, "{}", self.0)
54 }
55}
56
57impl core::fmt::Debug for AliasAddress {
58 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
59 write!(f, "AliasAddress({self})")
60 }
61}
62
63#[cfg(feature = "dto")]
64#[allow(missing_docs)]
65pub mod dto {
66 use serde::{Deserialize, Serialize};
67
68 use super::*;
69 use crate::block::error::dto::DtoError;
70
71 #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
73 pub struct AliasAddressDto {
74 #[serde(rename = "type")]
75 pub kind: u8,
76 #[serde(rename = "aliasId")]
77 pub alias_id: String,
78 }
79
80 impl From<&AliasAddress> for AliasAddressDto {
81 fn from(value: &AliasAddress) -> Self {
82 Self {
83 kind: AliasAddress::KIND,
84 alias_id: value.to_string(),
85 }
86 }
87 }
88
89 impl TryFrom<&AliasAddressDto> for AliasAddress {
90 type Error = DtoError;
91
92 fn try_from(value: &AliasAddressDto) -> Result<Self, Self::Error> {
93 value
94 .alias_id
95 .parse::<Self>()
96 .map_err(|_| DtoError::InvalidField("aliasId"))
97 }
98 }
99}