iota_sdk/types/block/output/
chain_id.rs

1// Copyright 2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use derive_more::From;
5
6use crate::types::block::output::{AliasId, FoundryId, NftId, OutputId};
7
8///
9#[derive(Clone, Copy, Eq, Hash, PartialEq, Ord, PartialOrd, From)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub enum ChainId {
12    ///
13    Alias(AliasId),
14    ///
15    Foundry(FoundryId),
16    ///
17    Nft(NftId),
18}
19
20impl core::fmt::Debug for ChainId {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        let mut formatter = f.debug_tuple("ChainId");
23        match self {
24            Self::Alias(id) => formatter.field(id),
25            Self::Foundry(id) => formatter.field(id),
26            Self::Nft(id) => formatter.field(id),
27        };
28        formatter.finish()
29    }
30}
31
32impl ChainId {
33    ///
34    pub fn is_null(&self) -> bool {
35        match self {
36            Self::Alias(alias_id) => alias_id.is_null(),
37            Self::Foundry(foundry_id) => foundry_id.is_null(),
38            Self::Nft(nft_id) => nft_id.is_null(),
39        }
40    }
41
42    ///
43    pub fn or_from_output_id(self, output_id: &OutputId) -> Self {
44        if !self.is_null() {
45            return self;
46        }
47
48        match self {
49            Self::Alias(_) => Self::Alias(AliasId::from(output_id)),
50            Self::Foundry(_) => self,
51            Self::Nft(_) => Self::Nft(NftId::from(output_id)),
52        }
53    }
54}
55
56impl core::fmt::Display for ChainId {
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        match self {
59            Self::Alias(id) => write!(f, "{id}"),
60            Self::Foundry(id) => write!(f, "{id}"),
61            Self::Nft(id) => write!(f, "{id}"),
62        }
63    }
64}