Skip to content

Commit eb54935

Browse files
committed
docs: add more documentation
1 parent 44d341e commit eb54935

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

src/digests.rs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::errors::{DecodeError, DecodeOwnedError};
88
use crate::hashes::Code;
99
use crate::storage::Storage;
1010

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.
1213
#[derive(Clone)]
1314
pub struct Multihash {
1415
storage: Storage,
@@ -35,7 +36,22 @@ impl hash::Hash for Multihash {
3536
}
3637

3738
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+
/// ```
3955
pub fn from_bytes(bytes: Vec<u8>) -> Result<Multihash, DecodeOwnedError> {
4056
if let Err(err) = MultihashRef::from_slice(&bytes) {
4157
return Err(DecodeOwnedError {
@@ -70,7 +86,16 @@ impl Multihash {
7086
}
7187
}
7288

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+
/// ```
7499
pub fn algorithm(&self) -> Code {
75100
self.as_ref().algorithm()
76101
}
@@ -140,7 +165,22 @@ pub struct MultihashRef<'a> {
140165
}
141166

142167
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+
/// ```
144184
pub fn from_slice(input: &'a [u8]) -> Result<Self, DecodeError> {
145185
if input.is_empty() {
146186
return Err(DecodeError::BadInputLength);
@@ -157,14 +197,37 @@ impl<'a> MultihashRef<'a> {
157197
Ok(MultihashRef { bytes: input })
158198
}
159199

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+
/// ```
161213
pub fn algorithm(&self) -> Code {
162214
let (code, _bytes) =
163215
varint_decode::u64(&self.bytes).expect("multihash is known to be valid algorithm");
164216
Code::from_u64(code)
165217
}
166218

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+
/// ```
168231
pub fn digest(&self) -> &'a [u8] {
169232
let (_code, bytes) =
170233
varint_decode::u64(&self.bytes).expect("multihash is known to be valid digest");
@@ -225,6 +288,17 @@ pub trait MultihashDigest {
225288
///
226289
/// The size of the hash is determoned by the size of the input hash. If it should be truncated
227290
/// 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+
/// ```
228302
pub fn wrap(code: Code, data: &[u8]) -> Multihash {
229303
let mut code_buf = varint_encode::u64_buffer();
230304
let code = varint_encode::u64(code.to_u64(), &mut code_buf);

0 commit comments

Comments
 (0)