diff --git a/hashes/src/error.rs b/hashes/src/error.rs new file mode 100644 index 000000000..a2eaa67a7 --- /dev/null +++ b/hashes/src/error.rs @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: CC0-1.0 + +//! Error code for the `hashes` crate. + +use core::fmt; + +/// Attempted to create a hash from an invalid length slice. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FromSliceError { + pub(crate) expected: usize, + pub(crate) got: usize, +} + +impl FromSliceError { + /// Returns the expected slice length. + pub fn expected_length(&self) -> usize { self.expected } + + /// Returns the invalid slice length. + pub fn invalid_length(&self) -> usize { self.got } +} + +impl fmt::Display for FromSliceError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "invalid slice length {} (expected {})", self.got, self.expected) + } +} + +#[cfg(feature = "std")] +impl std::error::Error for FromSliceError {} diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 00ca7fd70..c3dda3892 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -96,6 +96,7 @@ pub mod _export { mod internal_macros; pub mod cmp; +pub mod error; pub mod hash160; pub mod hkdf; pub mod hmac; @@ -127,6 +128,7 @@ use core::{convert, fmt, hash}; #[rustfmt::skip] // Keep public re-exports separate. #[doc(inline)] pub use self::{ + error::FromSliceError, hkdf::Hkdf, hmac::{Hmac, HmacEngine}, }; @@ -317,30 +319,6 @@ fn incomplete_block_len(eng: &H) -> usize { (eng.n_bytes_hashed() % block_size) as usize } -/// Attempted to create a hash from an invalid length slice. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct FromSliceError { - expected: usize, - got: usize, -} - -impl FromSliceError { - /// Returns the expected slice length. - pub fn expected_length(&self) -> usize { self.expected } - - /// Returns the invalid slice length. - pub fn invalid_length(&self) -> usize { self.got } -} - -impl fmt::Display for FromSliceError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "invalid slice length {} (expected {})", self.got, self.expected) - } -} - -#[cfg(feature = "std")] -impl std::error::Error for FromSliceError {} - #[cfg(test)] mod tests { use super::*;