iota_stardust_types/block/address/
nft.rs

1// Copyright (c) 2026 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use core::str::FromStr;
5
6use derive_more::{AsRef, Deref, From};
7
8use crate::block::{
9    Error,
10    output::{NftId, OutputId},
11};
12
13/// An NFT address.
14#[derive(
15    Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable,
16)]
17#[as_ref(forward)]
18pub struct NftAddress(NftId);
19
20impl NftAddress {
21    /// The [`Address`](crate::block::address::Address) kind of an NFT
22    /// address.
23    pub const KIND: u8 = 16;
24    /// The length of a [`NftAddress`].
25    pub const LENGTH: usize = NftId::LENGTH;
26
27    /// Creates a new [`NftAddress`].
28    #[inline(always)]
29    pub fn new(id: NftId) -> Self {
30        Self::from(id)
31    }
32
33    /// Returns the [`NftId`] of an [`NftAddress`].
34    #[inline(always)]
35    pub fn nft_id(&self) -> &NftId {
36        &self.0
37    }
38
39    /// Consumes an [`NftAddress`] and returns its [`NftId`].
40    #[inline(always)]
41    pub fn into_nft_id(self) -> NftId {
42        self.0
43    }
44}
45
46#[cfg(feature = "serde")]
47string_serde_impl!(NftAddress);
48
49impl FromStr for NftAddress {
50    type Err = Error;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        Ok(Self::from(NftId::from_str(s)?))
54    }
55}
56
57impl From<&OutputId> for NftAddress {
58    fn from(output_id: &OutputId) -> Self {
59        Self(NftId::from(output_id))
60    }
61}
62
63impl core::fmt::Display for NftAddress {
64    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
65        write!(f, "{}", self.0)
66    }
67}
68
69impl core::fmt::Debug for NftAddress {
70    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71        write!(f, "NftAddress({self})")
72    }
73}