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
use crate::Version;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum ErrorKind {
IOError(std::io::Error),
Serialization(String),
VersionParse,
VersionMismatch((Version, Version)),
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}
impl Error {
pub fn new(kind: ErrorKind) -> Self {
Self { kind }
}
}
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
match &self.kind {
ErrorKind::IOError(err) => write!(fmt, "io error: {}", err),
ErrorKind::Serialization(err) => write!(fmt, "serialization error: {}", err),
ErrorKind::VersionParse => write!(fmt, "failed to parse version"),
ErrorKind::VersionMismatch((expected, actual)) => {
write!(fmt, "expected version {} got {}", expected, actual)
}
}
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(other: std::io::Error) -> Self {
Self::new(ErrorKind::IOError(other))
}
}
impl std::convert::From<rmps::encode::Error> for Error {
fn from(other: rmps::encode::Error) -> Self {
Error::new(ErrorKind::Serialization(other.to_string()))
}
}
impl std::convert::From<rmps::decode::Error> for Error {
fn from(other: rmps::decode::Error) -> Self {
Error::new(ErrorKind::Serialization(other.to_string()))
}
}