1use core::fmt::{self, Display};
2use failure::{Backtrace, Context, Fail};
3
4#[derive(Debug)]
5pub struct EncodeError {
6 inner: Context<String>,
7}
8
9impl Fail for EncodeError {
10 fn cause(&self) -> Option<&dyn Fail> {
11 self.inner.cause()
12 }
13
14 fn backtrace(&self) -> Option<&Backtrace> {
15 self.inner.backtrace()
16 }
17}
18
19impl Display for EncodeError {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 Display::fmt(&self.inner, f)
22 }
23}
24
25impl From<&'static str> for EncodeError {
26 fn from(msg: &'static str) -> Self {
27 EncodeError {
28 inner: Context::new(msg.into()),
29 }
30 }
31}
32
33impl From<String> for EncodeError {
34 fn from(msg: String) -> Self {
35 EncodeError {
36 inner: Context::new(msg),
37 }
38 }
39}
40
41impl From<Context<String>> for EncodeError {
42 fn from(inner: Context<String>) -> Self {
43 EncodeError { inner }
44 }
45}
46
47impl From<Context<&'static str>> for EncodeError {
48 fn from(inner: Context<&'static str>) -> Self {
49 EncodeError {
50 inner: inner.map(|s| s.to_string()),
51 }
52 }
53}
54
55#[derive(Debug)]
56pub struct DecodeError {
57 inner: Context<String>,
58}
59
60impl Fail for DecodeError {
61 fn cause(&self) -> Option<&dyn Fail> {
62 self.inner.cause()
63 }
64
65 fn backtrace(&self) -> Option<&Backtrace> {
66 self.inner.backtrace()
67 }
68}
69
70impl Display for DecodeError {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 Display::fmt(&self.inner, f)
73 }
74}
75
76impl From<&'static str> for DecodeError {
77 fn from(msg: &'static str) -> Self {
78 DecodeError {
79 inner: Context::new(msg.into()),
80 }
81 }
82}
83
84impl From<String> for DecodeError {
85 fn from(msg: String) -> Self {
86 DecodeError {
87 inner: Context::new(msg),
88 }
89 }
90}
91
92impl From<Context<String>> for DecodeError {
93 fn from(inner: Context<String>) -> Self {
94 DecodeError { inner }
95 }
96}
97
98impl From<Context<&'static str>> for DecodeError {
99 fn from(inner: Context<&'static str>) -> Self {
100 DecodeError {
101 inner: inner.map(|s| s.to_string()),
102 }
103 }
104}