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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! # Keystore files (UTC / JSON) module errors

use super::core;
use std::{error, fmt};

/// Keystore file errors
#[derive(Debug)]
pub enum Error {
    /// An unsupported cipher
    UnsupportedCipher(String),

    /// An unsupported key derivation function
    UnsupportedKdf(String),

    /// An unsupported pseudo-random function
    UnsupportedPrf(String),

    /// `keccak256_mac` field validation failed
    FailedMacValidation,

    /// Core module error wrapper
    CoreFault(core::Error),

    /// Invalid Kdf depth value
    InvalidKdfDepth(String),

    /// Invalid crypto type
    InvalidCrypto(String),
}

impl From<core::Error> for Error {
    fn from(err: core::Error) -> Self {
        Error::CoreFault(err)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::UnsupportedCipher(ref str) => write!(f, "Unsupported cipher: {}", str),
            Error::UnsupportedKdf(ref str) => {
                write!(f, "Unsupported key derivation function: {}", str)
            }
            Error::UnsupportedPrf(ref str) => {
                write!(f, "Unsupported pseudo-random function: {}", str)
            }
            Error::FailedMacValidation => write!(f, "Message authentication code failed"),
            Error::CoreFault(ref err) => f.write_str(&err.to_string()),
            Error::InvalidKdfDepth(ref str) => write!(f, "Invalid security level: {}", str),
            Error::InvalidCrypto(ref str) => write!(f, "Invalid crypto section: {}", str),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        "Keystore file error"
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            Error::CoreFault(ref err) => Some(err),
            _ => None,
        }
    }
}