@@ -8,7 +8,8 @@ use crate::errors::{DecodeError, DecodeOwnedError};
8
8
use crate :: hashes:: Code ;
9
9
use crate :: storage:: Storage ;
10
10
11
- /// Represents a valid multihash.
11
+ /// Representation of a valid multihash. This enforces validity on construction,
12
+ /// so it can be assumed this is always a valid multihash.
12
13
#[ derive( Clone ) ]
13
14
pub struct Multihash {
14
15
storage : Storage ,
@@ -35,7 +36,22 @@ impl hash::Hash for Multihash {
35
36
}
36
37
37
38
impl Multihash {
38
- /// Verifies whether `bytes` contains a valid multihash, and if so returns a `Multihash`.
39
+ /// Creates a new `Multihash` from a `Vec<u8>`, consuming it.
40
+ /// If the input data is not a valid multihash an error is returned.
41
+ ///
42
+ /// # Example
43
+ ///
44
+ /// ```
45
+ /// use multihash::{Sha2_256, Multihash};
46
+ ///
47
+ /// let mh = Sha2_256::digest(b"hello world");
48
+ ///
49
+ /// // valid multihash
50
+ /// let mh2 = Multihash::from_bytes(mh.into_bytes()).unwrap();
51
+ ///
52
+ /// // invalid multihash
53
+ /// assert!(Multihash::from_bytes(vec![1,2,3]).is_err());
54
+ /// ```
39
55
pub fn from_bytes ( bytes : Vec < u8 > ) -> Result < Multihash , DecodeOwnedError > {
40
56
if let Err ( err) = MultihashRef :: from_slice ( & bytes) {
41
57
return Err ( DecodeOwnedError {
@@ -70,7 +86,16 @@ impl Multihash {
70
86
}
71
87
}
72
88
73
- /// Returns which hashing algorithm is used in this multihash.
89
+ /// Returns the algorithm used in this multihash.
90
+ ///
91
+ /// # Example
92
+ ///
93
+ /// ```
94
+ /// use multihash::{Code, Sha2_256};
95
+ ///
96
+ /// let mh = Sha2_256::digest(b"hello world");
97
+ /// assert_eq!(mh.algorithm(), Code::Sha2_256);
98
+ /// ```
74
99
pub fn algorithm ( & self ) -> Code {
75
100
self . as_ref ( ) . algorithm ( )
76
101
}
@@ -140,7 +165,22 @@ pub struct MultihashRef<'a> {
140
165
}
141
166
142
167
impl < ' a > MultihashRef < ' a > {
143
- /// Creates a `MultihashRef` from the given `input`.
168
+ /// Creates a new `MultihashRef` from a `&[u8]`.
169
+ /// If the input data is not a valid multihash an error is returned.
170
+ ///
171
+ /// # Example
172
+ ///
173
+ /// ```
174
+ /// use multihash::{Sha2_256, Multihash, MultihashRef};
175
+ ///
176
+ /// let mh = Sha2_256::digest(b"hello world");
177
+ ///
178
+ /// // valid multihash
179
+ /// let mh2 = MultihashRef::from_slice(&mh).unwrap();
180
+ ///
181
+ /// // invalid multihash
182
+ /// assert!(MultihashRef::from_slice(&vec![1,2,3]).is_err());
183
+ /// ```
144
184
pub fn from_slice ( input : & ' a [ u8 ] ) -> Result < Self , DecodeError > {
145
185
if input. is_empty ( ) {
146
186
return Err ( DecodeError :: BadInputLength ) ;
@@ -157,14 +197,37 @@ impl<'a> MultihashRef<'a> {
157
197
Ok ( MultihashRef { bytes : input } )
158
198
}
159
199
160
- /// Returns which hashing algorithm is used in this multihash.
200
+ /// Returns the algorithm used in this multihash.
201
+ ///
202
+ /// # Example
203
+ ///
204
+ /// ```
205
+ /// use multihash::{Code, Sha2_256, MultihashRef};
206
+ ///
207
+ /// let mh = Sha2_256::digest(b"hello world");
208
+ ///
209
+ /// // valid multihash
210
+ /// let mh2 = MultihashRef::from_slice(&mh).unwrap();
211
+ /// assert_eq!(mh2.algorithm(), Code::Sha2_256);
212
+ /// ```
161
213
pub fn algorithm ( & self ) -> Code {
162
214
let ( code, _bytes) =
163
215
varint_decode:: u64 ( & self . bytes ) . expect ( "multihash is known to be valid algorithm" ) ;
164
216
Code :: from_u64 ( code)
165
217
}
166
218
167
- /// Returns the hashed data.
219
+ /// Returns the hash digest.
220
+ ///
221
+ /// # Example
222
+ ///
223
+ /// ```
224
+ /// use multihash::{wrap, Code, Sha2_256};
225
+ ///
226
+ /// let mh = Sha2_256::digest(b"hello world");
227
+ /// let digest = mh.digest();
228
+ /// let wrapped = wrap(Code::Sha2_256, &digest);
229
+ /// assert_eq!(wrapped.digest(), digest);
230
+ /// ```
168
231
pub fn digest ( & self ) -> & ' a [ u8 ] {
169
232
let ( _code, bytes) =
170
233
varint_decode:: u64 ( & self . bytes ) . expect ( "multihash is known to be valid digest" ) ;
@@ -225,6 +288,17 @@ pub trait MultihashDigest {
225
288
///
226
289
/// The size of the hash is determoned by the size of the input hash. If it should be truncated
227
290
/// the input data must already be the truncated hash.
291
+ ///
292
+ /// # Example
293
+ ///
294
+ /// ```
295
+ /// use multihash::{wrap, Code, Sha2_256};
296
+ ///
297
+ /// let mh = Sha2_256::digest(b"hello world");
298
+ /// let digest = mh.digest();
299
+ /// let wrapped = wrap(Code::Sha2_256, &digest);
300
+ /// assert_eq!(wrapped.digest(), digest);
301
+ /// ```
228
302
pub fn wrap ( code : Code , data : & [ u8 ] ) -> Multihash {
229
303
let mut code_buf = varint_encode:: u64_buffer ( ) ;
230
304
let code = varint_encode:: u64 ( code. to_u64 ( ) , & mut code_buf) ;
0 commit comments