• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / catalyst-libs / 14608039879

23 Apr 2025 01:36AM UTC coverage: 65.024% (-0.06%) from 65.088%
14608039879

Pull #291

github

web-flow
Merge 7dc3bc87d into 155010ebf
Pull Request #291: fix(rust/cardano-chain-follower): Fixing `ProtectedLiveChainBlockList::get_block` method, correctly process fuzzy point

0 of 8 new or added lines in 1 file covered. (0.0%)

7 existing lines in 3 files now uncovered.

10660 of 16394 relevant lines covered (65.02%)

2686.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.83
/rust/catalyst-types/src/hash_wrapper.rs
1
//! A macro for defining a new type wrappers for the given hash types.
2

3
/// Defines a new type wrapper for the given hash types.
4
///
5
/// # Examples
6
///
7
/// ```
8
/// use catalyst_types::{define_hashes, hashes::Blake2b128Hash};
9
///
10
/// define_hashes!(
11
///     /// You can document the declared types...
12
///     (SomeHash, Blake2b128Hash),
13
///     // ...or not.
14
///     (AnotherHash, Blake2b128Hash),
15
/// );
16
///
17
/// let hash = SomeHash::new(&[1, 2, 3]);
18
/// println!("{hash:?}");
19
/// ```
20
#[macro_export]
21
macro_rules! define_hashes {
22
    ($($(#[$docs:meta])* ($name:ident, $inner:ty)),+ $(,)?) => {
23
        $(
24
            $(#[$docs])*
25
            #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
26
            pub struct $name($inner);
27

28
            impl $name {
29
                /// Creates a new instance from the given bytes by hashing them.
30
                #[must_use]
31
                pub fn new(input_bytes: &[u8]) -> Self {
2✔
32
                    Self(<$inner>::new(input_bytes))
2✔
33
                }
2✔
34
            }
35

36
            impl std::fmt::Display for $name {
37
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1✔
38
                    f.write_str(&format!("0x{}", self.0))
1✔
39
                }
1✔
40
            }
41

42
            impl From<$name> for Vec<u8> {
43
                fn from(value: $name) -> Self {
1✔
44
                    value.0.into()
1✔
45
                }
1✔
46
            }
47

48
            impl From<$inner> for $name {
UNCOV
49
                fn from(value: $inner) -> Self {
×
UNCOV
50
                    Self(value)
×
UNCOV
51
                }
×
52
            }
53

54
            impl TryFrom<&[u8]> for $name {
55
                type Error = $crate::hashes::Blake2bHashError;
56

57
                fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
2✔
58
                    Ok(Self(<$inner>::try_from(value)?))
2✔
59
                }
2✔
60
            }
61

62
            impl TryFrom<Vec<u8>> for $name {
63
                type Error = $crate::hashes::Blake2bHashError;
64

65
                fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
1✔
66
                    value.as_slice().try_into()
1✔
67
                }
1✔
68
            }
69

70
            impl std::str::FromStr for $name {
71
                type Err = $crate::hashes::Blake2bHashError;
72

73
                fn from_str(s: &str) -> Result<Self, Self::Err> {
22✔
74
                    let hash: $inner = s.parse().map_err($crate::hashes::Blake2bHashError::from)?;
22✔
75
                    Ok(Self(hash))
22✔
76
                }
22✔
77
            }
78

79
            impl<C> minicbor::Encode<C> for $name {
80
                fn encode<W: minicbor::encode::Write>(
×
81
                    &self, e: &mut minicbor::Encoder<W>, ctx: &mut C,
×
82
                ) -> Result<(), minicbor::encode::Error<W::Error>> {
×
83
                    self.0.encode(e, ctx)
×
84
                }
×
85
            }
86

87
            impl<'a, C> minicbor::Decode<'a, C> for $name {
88
                fn decode(
×
89
                    d: &mut minicbor::Decoder<'a>, ctx: &mut C,
×
90
                ) -> Result<Self, minicbor::decode::Error> {
×
91
                    let hash = <$inner>::decode(d, ctx)?;
×
92
                    Ok(Self(hash))
×
93
                }
×
94
            }
95
        )+
96
    };
97
}
98

99
#[cfg(test)]
100
mod tests {
101
    use crate::hashes::Blake2b128Hash;
102

103
    // Define one type without a trailing comma.
104
    define_hashes!((H1, Blake2b128Hash));
105
    // Define one type with a trailing comma and a doc-comment.
106
    define_hashes!(
107
        /// Some documentation.
108
        (H2, Blake2b128Hash),
109
    );
110
    // Define multiple types at once.
111
    define_hashes!(
112
        /// Documentation.
113
        (H3, Blake2b128Hash),
114
        // No documentation.
115
        (H4, Blake2b128Hash),
116
        /// More documentation.
117
        (H5, Blake2b128Hash),
118
    );
119

120
    // There is little reason to check the conversion itself, it is mostly a demonstration
121
    // that the methods defined by the macro are working.
122
    #[test]
123
    fn hash_wrapper() {
1✔
124
        let hash = H1::new(&[1, 2, 3, 4, 5]);
1✔
125

1✔
126
        let v = Vec::from(hash);
1✔
127
        let from_slice = H1::try_from(v.as_slice()).unwrap();
1✔
128
        assert_eq!(hash, from_slice);
1✔
129

130
        let from_vec = H1::try_from(v).unwrap();
1✔
131
        assert_eq!(hash, from_vec);
1✔
132
    }
1✔
133

134
    // The display implementation is used to get user-friendly representation and must be
135
    // equal to `hex::encode(<underlying bytes>)`.
136
    #[test]
137
    fn display() {
1✔
138
        let hash = H1::new(&[1, 2, 3, 4, 5]);
1✔
139
        let display = format!("{hash}");
1✔
140
        let expected = "0x2a6ad53c3c6986406e1d6c7cfd06b69a";
1✔
141
        assert_eq!(expected, display);
1✔
142
    }
1✔
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc