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
use std::{str, fmt};
use failure::{Backtrace, Context, Fail};
use nom::{error::ErrorKind, Err};
use std::convert::From;
use std::io::Error as IoError;
use std::io::ErrorKind as IoErrorKind;
#[derive(Debug)]
pub struct GetPayloadError {
ctx: Context<GetPayloadErrorKind>,
}
impl GetPayloadError {
pub fn kind(&self) -> &GetPayloadErrorKind {
self.ctx.get_context()
}
pub(crate) fn decrypt() -> GetPayloadError {
GetPayloadError::from(GetPayloadErrorKind::Decrypt)
}
pub(crate) fn deserialize(e: Err<(&[u8], ErrorKind)>, payload: Vec<u8>) -> GetPayloadError {
GetPayloadError::from(GetPayloadErrorKind::Deserialize { error: e.to_owned(), payload })
}
}
impl Fail for GetPayloadError {
fn cause(&self) -> Option<&dyn Fail> {
self.ctx.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.ctx.backtrace()
}
}
impl fmt::Display for GetPayloadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.ctx.fmt(f)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Fail)]
pub enum GetPayloadErrorKind {
#[fail(display = "Decrypt payload error")]
Decrypt,
#[fail(display = "Deserialize payload error: {:?}, data: {:?}", error, payload)]
Deserialize {
error: nom::Err<(Vec<u8>, ErrorKind)>,
payload: Vec<u8>,
}
}
impl From<GetPayloadErrorKind> for GetPayloadError {
fn from(kind: GetPayloadErrorKind) -> GetPayloadError {
GetPayloadError::from(Context::new(kind))
}
}
impl From<Context<GetPayloadErrorKind>> for GetPayloadError {
fn from(ctx: Context<GetPayloadErrorKind>) -> GetPayloadError {
GetPayloadError { ctx }
}
}
impl From<GetPayloadError> for IoError {
fn from(item: GetPayloadError) -> Self {
IoError::new(IoErrorKind::Other, format!("GetPayloadError occured. error: {:?}", item))
}
}
#[cfg(test)]
mod tests {
use super::*;
use nom::Needed;
#[test]
fn get_payload_error() {
let error = GetPayloadError::deserialize(Err::Error((&[], ErrorKind::Eof)), vec![1, 2, 3, 4]);
assert!(error.cause().is_none());
assert!(error.backtrace().is_some());
assert_eq!(format!("{}", error), "Deserialize payload error: Error(([], Eof)), data: [1, 2, 3, 4]".to_owned());
}
#[test]
fn get_payload_error_kind() {
let decrypt = GetPayloadErrorKind::Decrypt;
assert_eq!(format!("{}", decrypt), "Decrypt payload error".to_owned());
let incomplete = GetPayloadErrorKind::Deserialize { error: Err::Incomplete(Needed::Size(5)), payload: vec![1, 2, 3, 4] };
assert_eq!(format!("{}", incomplete), "Deserialize payload error: Incomplete(Size(5)), data: [1, 2, 3, 4]".to_owned());
let deserialize = GetPayloadErrorKind::Deserialize { error: Err::Error((vec![], ErrorKind::Eof)), payload: vec![1, 2, 3, 4] };
assert_eq!(format!("{}", deserialize), "Deserialize payload error: Error(([], Eof)), data: [1, 2, 3, 4]".to_owned());
}
}