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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::error::Error as StdError;
use std::{self, fmt, io};
macro_rules! werr(
($($arg:tt)*) => ({
use std::io::{Write, stderr};
write!(&mut stderr(), $($arg)*).unwrap();
})
);
pub type Result<T> = std::result::Result<T, PError>;
#[derive(Debug)]
pub enum ErrorKind {
Generate,
Sign,
Verify,
Io,
Misc,
Hash,
KDF,
RNG,
Encoding,
}
#[derive(Debug)]
pub struct PError {
kind: ErrorKind,
err: Box<dyn StdError + Send + Sync>,
}
impl PError {
pub fn exit(&self) -> ! {
werr!("{}\n", self);
::std::process::exit(1)
}
pub fn new<E>(kind: ErrorKind, err: E) -> PError
where
E: Into<Box<dyn StdError + Send + Sync>>,
{
PError {
kind,
err: err.into(),
}
}
}
impl fmt::Display for PError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
ErrorKind::Generate => write!(f, "{}", self.err),
ErrorKind::Sign => write!(f, "{}", self.err),
ErrorKind::Verify => write!(f, "{}", self.err),
ErrorKind::Misc => write!(f, "{}", self.err),
ErrorKind::Io => write!(f, "{}", self.err),
ErrorKind::Hash => write!(f, "{}", self.err),
ErrorKind::KDF => write!(f, "{}", self.err),
ErrorKind::RNG => write!(f, "{}", self.err),
ErrorKind::Encoding => write!(f, "{}", self.err),
}
}
}
impl StdError for PError {
fn description(&self) -> &str {
match self.kind {
ErrorKind::Generate => "generate error",
ErrorKind::Sign => "sign error",
ErrorKind::Verify => "verify error",
ErrorKind::Misc => "misc error",
ErrorKind::Io => "io error",
ErrorKind::Hash => "hash error",
ErrorKind::KDF => "key derivation error",
ErrorKind::RNG => "random number generator error",
ErrorKind::Encoding => "encoding error",
}
}
}
impl From<io::Error> for PError {
fn from(err: io::Error) -> PError {
PError::new(ErrorKind::Io, err)
}
}
impl From<fmt::Error> for PError {
fn from(err: fmt::Error) -> PError {
PError::new(ErrorKind::Io, err)
}
}
impl From<base64::DecodeError> for PError {
fn from(err: base64::DecodeError) -> PError {
PError::new(ErrorKind::Encoding, err)
}
}
impl From<std::string::FromUtf8Error> for PError {
fn from(err: std::string::FromUtf8Error) -> PError {
PError::new(ErrorKind::Encoding, err)
}
}
impl From<scrypt::errors::InvalidParams> for PError {
fn from(err: scrypt::errors::InvalidParams) -> PError {
PError::new(ErrorKind::KDF, err)
}
}
impl From<scrypt::errors::InvalidOutputLen> for PError {
fn from(err: scrypt::errors::InvalidOutputLen) -> PError {
PError::new(ErrorKind::KDF, err)
}
}
impl From<getrandom::Error> for PError {
fn from(err: getrandom::Error) -> PError {
PError::new(ErrorKind::RNG, format!("{}", err))
}
}