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
65
66
67
68
69
70
71
72
73
use std::convert::TryFrom;

use crate::error::{Error, Result};

macro_rules! build_codec_enum {
    {$( #[$attr:meta] $code:expr => $codec:ident, )*} => {
        /// List of types currently supported in the multicodec spec.
        #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash)]
        pub enum Codec {
            $( #[$attr] $codec, )*
        }

        impl TryFrom<u64> for Codec {
            type Error = Error;

            /// Convert a number to the matching codec, or `Error` if unknown codec is matching.
            fn try_from(raw: u64) -> Result<Codec> {
                match raw {
                    $( $code => Ok(Self::$codec), )*
                    _ => Err(Error::UnknownCodec),
                }
            }
        }

        impl From<Codec> for u64 {
            /// Convert to the matching integer code
            fn from(codec: Codec) -> u64 {
                match codec {
                    $( Codec::$codec => $code, )*
                }
            }
        }
    }
}

build_codec_enum! {
    /// Raw binary
    0x55 => Raw,
    /// MerkleDAG protobuf
    0x70 => DagProtobuf,
    /// MerkleDAG cbor
    0x71 => DagCBOR,
    /// Raw Git object
    0x78 => GitRaw,
    /// Ethereum Block (RLP)
    0x90 => EthereumBlock,
    /// Ethereum Block List (RLP)
    0x91 => EthereumBlockList,
    /// Ethereum Transaction Trie (Eth-Trie)
    0x92 => EthereumTxTrie,
    /// Ethereum Transaction (RLP)
    0x93 => EthereumTx,
    /// Ethereum Transaction Receipt Trie (Eth-Trie)
    0x94 => EthereumTxReceiptTrie,
    /// Ethereum Transaction Receipt (RLP)
    0x95 => EthereumTxReceipt,
    /// Ethereum State Trie (Eth-Secure-Trie)
    0x96 => EthereumStateTrie,
    /// Ethereum Account Snapshot (RLP)
    0x97 => EthereumAccountSnapshot,
    /// Ethereum Contract Storage Trie (Eth-Secure-Trie)
    0x98 => EthereumStorageTrie,
    /// Bitcoin Block
    0xb0 => BitcoinBlock,
    /// Bitcoin Transaction
    0xb1 => BitcoinTx,
    /// Zcash Block
    0xc0 => ZcashBlock,
    /// Zcash Transaction
    0xc1 => ZcashTx,
    /// MerkleDAG json
    0x0129 => DagJSON,
}