Skip to main content

modern_crypto_toolkit/
error.rs

1use std::fmt;
2
3/// Errors returned by the crypto toolkit.
4#[derive(Debug)]
5pub enum Error {
6    /// An unsupported or insecure algorithm was requested.
7    UnsupportedAlgorithm(String),
8    /// A key has an incorrect length or is otherwise invalid.
9    InvalidKey(String),
10    /// Decryption or authentication failed.
11    Decryption(String),
12    /// Signature creation or verification failed.
13    Signature(String),
14    /// A password hash has an invalid format.
15    InvalidHashFormat(String),
16    /// Base64 decoding failed.
17    Base64(String),
18    /// Hex decoding failed.
19    Hex(String),
20    /// Failed to read random data.
21    Random(String),
22    /// A parameter was invalid.
23    InvalidParameter(String),
24    /// A wrapped underlying error.
25    Internal(String),
26}
27
28impl fmt::Display for Error {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Error::UnsupportedAlgorithm(msg) => write!(f, "unsupported or insecure algorithm: {msg}"),
32            Error::InvalidKey(msg) => write!(f, "invalid key: {msg}"),
33            Error::Decryption(msg) => write!(f, "decryption failed: {msg}"),
34            Error::Signature(msg) => write!(f, "signature failed: {msg}"),
35            Error::InvalidHashFormat(msg) => write!(f, "invalid hash format: {msg}"),
36            Error::Base64(msg) => write!(f, "base64 error: {msg}"),
37            Error::Hex(msg) => write!(f, "hex error: {msg}"),
38            Error::Random(msg) => write!(f, "random error: {msg}"),
39            Error::InvalidParameter(msg) => write!(f, "invalid parameter: {msg}"),
40            Error::Internal(msg) => write!(f, "internal error: {msg}"),
41        }
42    }
43}
44
45impl std::error::Error for Error {}
46
47/// Result type for the crypto toolkit.
48pub type Result<T> = std::result::Result<T, Error>;