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
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::Error;

/// A Merkle root of a list of hashes.
#[derive(Clone, Copy, Eq, PartialEq, packable::Packable, derive_more::From, derive_more::AsRef)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MerkleRoot([u8; Self::LENGTH]);

impl MerkleRoot {
    /// Length of a merkle root.
    pub const LENGTH: usize = 32;

    /// Creates a new [`MerkleRoot`].
    pub fn new(bytes: [u8; Self::LENGTH]) -> Self {
        Self::from(bytes)
    }

    /// Creates a null [`MerkleRoot`].
    pub fn null() -> Self {
        Self::from([0u8; Self::LENGTH])
    }
}

impl core::ops::Deref for MerkleRoot {
    type Target = [u8; Self::LENGTH];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl core::fmt::Display for MerkleRoot {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "{}", prefix_hex::encode(self.0))
    }
}

impl core::fmt::Debug for MerkleRoot {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "MerkleRoot({})", self)
    }
}

impl core::str::FromStr for MerkleRoot {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(
            prefix_hex::decode::<[u8; Self::LENGTH]>(s).map_err(Error::HexError)?,
        ))
    }
}