bee_block/output/
nft_id.rs

1// Copyright 2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::output::OutputId;
5
6impl_id!(pub NftId, 32, "TODO.");
7
8#[cfg(feature = "serde")]
9string_serde_impl!(NftId);
10
11impl From<OutputId> for NftId {
12    fn from(output_id: OutputId) -> Self {
13        Self::from(output_id.hash())
14    }
15}
16
17impl NftId {
18    ///
19    pub fn or_from_output_id(self, output_id: OutputId) -> Self {
20        if self.is_null() { Self::from(output_id) } else { self }
21    }
22}
23
24#[cfg(feature = "dto")]
25#[allow(missing_docs)]
26pub mod dto {
27    use serde::{Deserialize, Serialize};
28
29    use super::*;
30    use crate::error::dto::DtoError;
31
32    #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
33    pub struct NftIdDto(pub String);
34
35    impl From<&NftId> for NftIdDto {
36        fn from(value: &NftId) -> Self {
37            Self(value.to_string())
38        }
39    }
40
41    impl TryFrom<&NftIdDto> for NftId {
42        type Error = DtoError;
43
44        fn try_from(value: &NftIdDto) -> Result<Self, Self::Error> {
45            value.0.parse::<NftId>().map_err(|_| DtoError::InvalidField("NFT id"))
46        }
47    }
48}