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
49
50
/// An error type for anything that goes wrong in this crate
#[derive(Debug)]
pub enum Error {
    /// For errors that occur in AEAD algorithms
    EncryptionError(&'static str),
    /// For errors that occur in Diffie-Hellman key agreement
    DHError(&'static str),
    /// For errors that occur in signature algorithms
    SignatureError(&'static str),
    /// For errors encountered during (de)serialization
    SerdeError(String),
    /// For when we need randomness and there's none left
    OutOfEntropy,
}

// The only IO done in molasses is via serde, so this is a natural conversion
impl<'a> std::convert::From<std::io::Error> for Error {
    fn from(other: std::io::Error) -> Error {
        use std::error::Error;
        crate::error::Error::SerdeError(other.description().to_string())
    }
}

// Serde requires that any Serializer's error type implement std::error::Error
impl std::error::Error for Error {
    fn description(&self) -> &str {
        match self {
            Error::EncryptionError(e) => e,
            Error::DHError(e) => e,
            Error::SignatureError(e) => e,
            Error::SerdeError(e) => e.as_str(),
            Error::OutOfEntropy => "Out of Entropy",
        }
    }
}

// Serde also requires that any Serializer's error type implement std::fmt::Display
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        use std::error::Error;
        f.write_str(self.description())
    }
}

// Finally, serde requires that any Serializer's error type implement serde::ser::Error
impl serde::ser::Error for Error {
    fn custom<T: std::fmt::Display>(msg: T) -> Self {
        Error::SerdeError(format!("{}", msg))
    }
}