iota_sdk/types/block/output/
chain_id.rs1use derive_more::From;
5
6use crate::types::block::output::{AliasId, FoundryId, NftId, OutputId};
7
8#[derive(Clone, Copy, Eq, Hash, PartialEq, Ord, PartialOrd, From)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub enum ChainId {
12 Alias(AliasId),
14 Foundry(FoundryId),
16 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 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 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}