1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use derive_more::From;

use crate::types::block::output::{AliasId, FoundryId, NftId, OutputId};

///
#[derive(Clone, Copy, Eq, Hash, PartialEq, Ord, PartialOrd, From)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ChainId {
    ///
    Alias(AliasId),
    ///
    Foundry(FoundryId),
    ///
    Nft(NftId),
}

impl core::fmt::Debug for ChainId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut formatter = f.debug_tuple("ChainId");
        match self {
            Self::Alias(id) => formatter.field(id),
            Self::Foundry(id) => formatter.field(id),
            Self::Nft(id) => formatter.field(id),
        };
        formatter.finish()
    }
}

impl ChainId {
    ///
    pub fn is_null(&self) -> bool {
        match self {
            Self::Alias(alias_id) => alias_id.is_null(),
            Self::Foundry(foundry_id) => foundry_id.is_null(),
            Self::Nft(nft_id) => nft_id.is_null(),
        }
    }

    ///
    pub fn or_from_output_id(self, output_id: &OutputId) -> Self {
        if !self.is_null() {
            return self;
        }

        match self {
            Self::Alias(_) => Self::Alias(AliasId::from(output_id)),
            Self::Foundry(_) => self,
            Self::Nft(_) => Self::Nft(NftId::from(output_id)),
        }
    }
}

impl core::fmt::Display for ChainId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Alias(id) => write!(f, "{id}"),
            Self::Foundry(id) => write!(f, "{id}"),
            Self::Nft(id) => write!(f, "{id}"),
        }
    }
}