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

// Adapted from https://github.com/paritytech/parity-ethereum/blob/master/ethkey/src/error.rs
use std::{fmt, error};
use rustc_hex::*;

#[derive(Debug)]
pub enum Error {
	InvalidSecretKey,
	InvalidPublicKey,
	InvalidBufferLength,
	Io(::std::io::Error),
	Custom(String),
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		let msg = match *self {
			Error::InvalidSecretKey => "Invalid secret key".into(),
			Error::InvalidPublicKey => "Invalid public key".into(),
			Error::InvalidBufferLength => "Invalid buffer length".into(),
			Error::Io(ref err) => format!("I/O error: {}", err),
			Error::Custom(ref s) => s.clone(),
		};

		f.write_fmt(format_args!("Crypto error ({})", msg))
	}
}

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

impl Into<String> for Error {
	fn into(self) -> String {
		format!("{}", self)
	}
}

impl From<::std::io::Error> for Error {
	fn from(err: ::std::io::Error) -> Error {
		Error::Io(err)
	}
}