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
//! The [`Digest`] used by Leaf types in this crate.

use std::str::FromStr;

use iroh_base::hash::Hash;

/// Wrapper around an Iroh [`Hash`] that implements [`BorshSerialize`] and [`BorshDeserialize`].
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Digest(pub Hash);
impl From<Digest> for Hash {
    fn from(val: Digest) -> Self {
        val.0
    }
}
impl Digest {
    pub fn new(bytes: &[u8]) -> Self {
        Self(Hash::new(bytes))
    }
    pub fn from_bytes(bytes: [u8; 32]) -> Self {
        Self(Hash::from_bytes(bytes))
    }
}
impl std::fmt::Debug for Digest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Digest")
            .field(&format_args!("{}", self.0))
            .finish()
    }
}
impl FromStr for Digest {
    type Err = <Hash as FromStr>::Err;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(FromStr::from_str(s)?))
    }
}
impl std::fmt::Display for Digest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}
impl From<Hash> for Digest {
    fn from(value: Hash) -> Self {
        Self(value)
    }
}
impl borsh::BorshSerialize for Digest {
    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
        <[u8; 32] as borsh::BorshSerialize>::serialize(self.0.as_bytes(), writer)
    }
}
impl borsh::BorshDeserialize for Digest {
    fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
        let bytes = <[u8; 32] as borsh::BorshDeserialize>::deserialize_reader(reader)?;
        Ok(Digest(Hash::from_bytes(bytes)))
    }
}
impl std::ops::Deref for Digest {
    type Target = Hash;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl std::ops::DerefMut for Digest {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}