use core::str::FromStr;
use derive_more::{AsRef, Deref, From};
use crate::block::{output::NftId, Error};
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
#[as_ref(forward)]
pub struct NftAddress(NftId);
#[allow(clippy::len_without_is_empty)]
impl NftAddress {
pub const KIND: u8 = 16;
pub const LENGTH: usize = NftId::LENGTH;
#[inline(always)]
pub fn new(id: NftId) -> Self {
Self::from(id)
}
#[inline(always)]
pub fn nft_id(&self) -> &NftId {
&self.0
}
#[inline(always)]
pub fn into_nft_id(self) -> NftId {
self.0
}
}
#[cfg(feature = "serde")]
string_serde_impl!(NftAddress);
impl FromStr for NftAddress {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(NftAddress::from(NftId::from_str(s)?))
}
}
impl core::fmt::Display for NftAddress {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
impl core::fmt::Debug for NftAddress {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "NftAddress({self})")
}
}
#[cfg(feature = "dto")]
#[allow(missing_docs)]
pub mod dto {
use serde::{Deserialize, Serialize};
use super::*;
use crate::block::error::dto::DtoError;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct NftAddressDto {
#[serde(rename = "type")]
pub kind: u8,
#[serde(rename = "nftId")]
pub nft_id: String,
}
impl From<&NftAddress> for NftAddressDto {
fn from(value: &NftAddress) -> Self {
Self {
kind: NftAddress::KIND,
nft_id: value.to_string(),
}
}
}
impl TryFrom<&NftAddressDto> for NftAddress {
type Error = DtoError;
fn try_from(value: &NftAddressDto) -> Result<Self, Self::Error> {
value
.nft_id
.parse::<NftAddress>()
.map_err(|_| DtoError::InvalidField("NFT address"))
}
}
}