|
33 | 33 | //! let hash = Code::Foo.digest(b"hello world!");
|
34 | 34 | //! println!("{:02x?}", hash);
|
35 | 35 | //! ```
|
36 |
| -extern crate proc_macro; |
37 | 36 |
|
38 |
| -mod multihash; |
39 |
| -mod utils; |
| 37 | +mod hasher; |
40 | 38 |
|
41 |
| -use proc_macro::TokenStream; |
42 |
| -use proc_macro_error::proc_macro_error; |
43 |
| -use synstructure::macros::{parse, DeriveInput}; |
44 |
| -use synstructure::{MacroResult, Structure}; |
| 39 | +use std::convert::TryFrom; |
| 40 | +use std::fmt; |
45 | 41 |
|
46 |
| -#[proc_macro_derive(Multihash, attributes(mh))] |
47 |
| -#[allow(non_snake_case)] |
48 |
| -#[proc_macro_error] |
49 |
| -#[deprecated(since = "0.8.1", note = "Use `MultihashDigest` derive instead.")] |
50 |
| -pub fn Multihash(i: TokenStream) -> TokenStream { |
51 |
| - match parse::<DeriveInput>(i) { |
52 |
| - Ok(p) => match Structure::try_new(&p) { |
53 |
| - Ok(s) => multihash::multihash(s).into_stream(), |
54 |
| - Err(e) => e.to_compile_error().into(), |
55 |
| - }, |
56 |
| - Err(e) => e.to_compile_error().into(), |
| 42 | +pub use hasher::Hasher; |
| 43 | +pub use multihash_derive_impl::MultihashDigest; |
| 44 | + |
| 45 | +pub use multihash::*; |
| 46 | + |
| 47 | +/// The given code is not supported by this codetable. |
| 48 | +#[derive(Debug)] |
| 49 | +pub struct UnsupportedCode(pub u64); |
| 50 | + |
| 51 | +impl fmt::Display for UnsupportedCode { |
| 52 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 53 | + write!(f, "the code {} is not supported by this codetable", self.0) |
57 | 54 | }
|
58 | 55 | }
|
59 | 56 |
|
60 |
| -#[proc_macro_derive(MultihashDigest, attributes(mh))] |
61 |
| -#[allow(non_snake_case)] |
62 |
| -#[proc_macro_error] |
63 |
| -pub fn MultihashDigest(i: TokenStream) -> TokenStream { |
64 |
| - #[allow(deprecated)] |
65 |
| - Multihash(i) |
| 57 | +impl std::error::Error for UnsupportedCode {} |
| 58 | + |
| 59 | +/// Trait that implements hashing. |
| 60 | +/// |
| 61 | +/// It is usually implemented by a custom code table enum that derives the [`Multihash` derive]. |
| 62 | +/// |
| 63 | +/// [`Multihash` derive]: crate::derive |
| 64 | +pub trait MultihashDigest<const S: usize>: |
| 65 | + TryFrom<u64, Error = UnsupportedCode> |
| 66 | + + Into<u64> |
| 67 | + + Send |
| 68 | + + Sync |
| 69 | + + Unpin |
| 70 | + + Copy |
| 71 | + + Eq |
| 72 | + + fmt::Debug |
| 73 | + + 'static |
| 74 | +{ |
| 75 | + /// Calculate the hash of some input data. |
| 76 | + /// |
| 77 | + /// # Example |
| 78 | + /// |
| 79 | + /// ``` |
| 80 | + /// // `Code` implements `MultihashDigest` |
| 81 | + /// use multihash::{Code, MultihashDigest}; |
| 82 | + /// |
| 83 | + /// let hash = Code::Sha3_256.digest(b"Hello world!"); |
| 84 | + /// println!("{:02x?}", hash); |
| 85 | + /// ``` |
| 86 | + fn digest(&self, input: &[u8]) -> Multihash<S>; |
| 87 | + |
| 88 | + /// Create a multihash from an existing multihash digest. |
| 89 | + /// |
| 90 | + /// # Example |
| 91 | + /// |
| 92 | + /// ``` |
| 93 | + /// use multihash::{Code, Hasher, MultihashDigest, Sha3_256}; |
| 94 | + /// |
| 95 | + /// let mut hasher = Sha3_256::default(); |
| 96 | + /// hasher.update(b"Hello world!"); |
| 97 | + /// let hash = Code::Sha3_256.wrap(&hasher.finalize()).unwrap(); |
| 98 | + /// println!("{:02x?}", hash); |
| 99 | + /// ``` |
| 100 | + fn wrap(&self, digest: &[u8]) -> std::result::Result<Multihash<S>, multihash::Error>; |
66 | 101 | }
|
0 commit comments