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());
}
}