1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use core::fmt;

/// Keccak result type alias.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Keccak errors.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
    /// Generic failure.
    Fail,
    /// The provided output buffer is too small.
    OutputTooSmall,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Display for Error {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Error::Fail => "generic failure",
            Error::OutputTooSmall => "output buffer too small",
        })
    }
}

impl Error {
    /// Converts a raw [`HashReturn`](ffi::HashReturn) value into a [`Result`].
    #[inline]
    pub fn from_raw(raw: ffi::HashReturn) -> Result<()> {
        match raw {
            ffi::HashReturn::KECCAK_SUCCESS => Ok(()),
            ffi::HashReturn::KECCAK_FAIL => Err(Error::OutputTooSmall),
            ffi::HashReturn::KECCAK_BAD_HASHLEN => Err(Error::OutputTooSmall),
        }
    }

    /// Converts a raw integer return value into a [`Result`].
    #[inline]
    pub fn from_int(raw: i32) -> Result<()> {
        if raw == 0 {
            Ok(())
        } else {
            Err(Error::Fail)
        }
    }
}