1- //! A bunch of specific hashes.
2-
3- use std:: str:: FromStr ;
4-
5- use catalyst_types:: hashes:: { Blake2b224Hash , Blake2b256Hash , Blake2bHashError } ;
6- use pallas_crypto:: hash:: Hash ;
7-
8- /// Defines a new type wrapper for the given hash type.
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]
921macro_rules! define_hashes {
10- ( $( $( #[ $docs: meta] ) * ( $name: ident, $inner: ty) ) ,+) => {
22+ ( $( $( #[ $docs: meta] ) * ( $name: ident, $inner: ty) ) ,+ $ ( , ) ? ) => {
1123 $(
1224 $( #[ $docs] ) *
1325 #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -34,26 +46,26 @@ macro_rules! define_hashes {
3446 }
3547
3648 impl TryFrom <& [ u8 ] > for $name {
37- type Error = Blake2bHashError ;
49+ type Error = $crate :: hashes :: Blake2bHashError ;
3850
3951 fn try_from( value: & [ u8 ] ) -> Result <Self , Self :: Error > {
4052 Ok ( Self ( <$inner>:: try_from( value) ?) )
4153 }
4254 }
4355
4456 impl TryFrom <Vec <u8 >> for $name {
45- type Error = Blake2bHashError ;
57+ type Error = $crate :: hashes :: Blake2bHashError ;
4658
4759 fn try_from( value: Vec <u8 >) -> Result <Self , Self :: Error > {
4860 value. as_slice( ) . try_into( )
4961 }
5062 }
5163
52- impl FromStr for $name {
53- type Err = Blake2bHashError ;
64+ impl std :: str :: FromStr for $name {
65+ type Err = $crate :: hashes :: Blake2bHashError ;
5466
5567 fn from_str( s: & str ) -> Result <Self , Self :: Err > {
56- let hash: $inner = s. parse( ) . map_err( Blake2bHashError :: from) ?;
68+ let hash: $inner = s. parse( ) . map_err( $crate :: hashes :: Blake2bHashError :: from) ?;
5769 Ok ( Self ( hash) )
5870 }
5971 }
@@ -78,40 +90,38 @@ macro_rules! define_hashes {
7890 } ;
7991}
8092
81- define_hashes ! (
82- /// A transaction hash - Blake2b-256 hash of a transaction.
83- ( TransactionHash , Blake2b256Hash ) ,
84- /// A public key hash - raw Blake2b-224 hash of an Ed25519 public key (has no discriminator, just the hash).
85- ( PubKeyHash , Blake2b224Hash )
86- ) ;
87-
88- impl From < Hash < 32 > > for TransactionHash {
89- fn from ( hash : Hash < 32 > ) -> Self {
90- Self ( Blake2b256Hash :: from ( hash) )
91- }
92- }
93-
94- impl From < Hash < 28 > > for PubKeyHash {
95- fn from ( hash : Hash < 28 > ) -> Self {
96- Self ( Blake2b224Hash :: from ( hash) )
97- }
98- }
99-
10093#[ cfg( test) ]
10194mod tests {
102- use super :: * ;
95+ use crate :: hashes:: Blake2b128Hash ;
96+
97+ // Define one type without a trailing comma.
98+ define_hashes ! ( ( H1 , Blake2b128Hash ) ) ;
99+ // Define one type with a trailing comma and a doc-comment.
100+ define_hashes ! (
101+ /// Some documentation.
102+ ( H2 , Blake2b128Hash ) ,
103+ ) ;
104+ // Define multiple types at once.
105+ define_hashes ! (
106+ /// Documentation.
107+ ( H3 , Blake2b128Hash ) ,
108+ // No documentation.
109+ ( H4 , Blake2b128Hash ) ,
110+ /// More documentation.
111+ ( H5 , Blake2b128Hash ) ,
112+ ) ;
103113
104114 // There is little reason to check the conversion itself, it is mostly a demonstration
105115 // that the methods defined by the macro are working.
106116 #[ test]
107- fn roundtrip ( ) {
108- let hash = TransactionHash :: new ( & [ ] ) ;
117+ fn hash_wrapper ( ) {
118+ let hash = H1 :: new ( & [ 1 , 2 , 3 , 4 , 5 ] ) ;
109119
110120 let v = Vec :: from ( hash) ;
111- let from_slice = TransactionHash :: try_from ( v. as_slice ( ) ) . unwrap ( ) ;
121+ let from_slice = H1 :: try_from ( v. as_slice ( ) ) . unwrap ( ) ;
112122 assert_eq ! ( hash, from_slice) ;
113123
114- let from_vec = TransactionHash :: try_from ( v) . unwrap ( ) ;
124+ let from_vec = H1 :: try_from ( v) . unwrap ( ) ;
115125 assert_eq ! ( hash, from_vec) ;
116126 }
117127}
0 commit comments