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
use std::{fmt, io, result, string};
use std::error::Error as StdError;
use base64;
#[derive(Debug)]
pub struct Error {
pub(crate) kind: ErrorKind,
}
impl Error {
pub(crate) fn with_kind(kind: ErrorKind) -> Error {
Error { kind: kind }
}
}
#[derive(Debug)]
pub(crate) enum ErrorKind {
Io(io::Error),
Decode(base64::DecodeError),
Utf8Error(string::FromUtf8Error),
InvalidCertType(u32),
InvalidFormat,
UnexpectedEof,
NotCertificate,
KeyTypeMismatch,
UnknownKeyType(String),
UnknownCurve(String),
}
pub type Result<T> = result::Result<T, Error>;
impl From<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error {
kind: ErrorKind::Io(error),
}
}
}
impl From<base64::DecodeError> for Error {
fn from(error: base64::DecodeError) -> Error {
Error {
kind: ErrorKind::Decode(error),
}
}
}
impl From<string::FromUtf8Error> for Error {
fn from(error: string::FromUtf8Error) -> Error {
Error {
kind: ErrorKind::Utf8Error(error),
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self.kind {
ErrorKind::Io(ref e) => e.source(),
ErrorKind::Decode(ref e) => e.source(),
ErrorKind::Utf8Error(ref e) => e.source(),
ErrorKind::InvalidCertType(_) |
ErrorKind::InvalidFormat |
ErrorKind::UnexpectedEof |
ErrorKind::NotCertificate |
ErrorKind::KeyTypeMismatch |
ErrorKind::UnknownCurve(_) |
ErrorKind::UnknownKeyType(_) => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
ErrorKind::Io(ref err) => err.fmt(f),
ErrorKind::Decode(ref err) => err.fmt(f),
ErrorKind::Utf8Error(ref err) => err.fmt(f),
ErrorKind::InvalidFormat => write!(f, "Invalid format"),
ErrorKind::InvalidCertType(v) => write!(f, "Invalid certificate type with value {}", v),
ErrorKind::UnexpectedEof => write!(f, "Unexpected EOF reached while reading data"),
ErrorKind::UnknownKeyType(ref v) => write!(f, "Unknown key type {}", v),
ErrorKind::NotCertificate => write!(f, "Not a certificate"),
ErrorKind::KeyTypeMismatch => write!(f, "Key type mismatch"),
ErrorKind::UnknownCurve(ref v) => write!(f, "Unknown curve {}", v),
}
}
}