xdr_rs_serialize/
error.rs1use std::fmt;
2
3#[derive(Clone, Debug, PartialEq)]
4pub enum ErrorKind {
5 BoolBadFormat,
6 IntegerBadFormat,
7 UnsignedIntegerBadFormat,
8 HyperBadFormat,
9 UnsignedHyperBadFormat,
10 FloatBadFormat,
11 DoubleBadFormat,
12 StringBadFormat,
13
14 FixedArrayWrongSize,
15 VarArrayWrongSize,
16 InvalidEnumValue,
17
18 BadArraySize,
19 InvalidPadding,
20
21 InvalidJson,
22
23 Utf8Error(std::str::Utf8Error),
24 IOError(std::io::ErrorKind),
25}
26
27#[derive(Debug, PartialEq)]
28pub struct Error {
29 kind: ErrorKind,
30}
31
32impl fmt::Display for Error {
33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34 match self.kind() {
35 _ => write!(f, "{:?}", self),
36 }
37 }
38}
39
40impl Error {
41 fn from_kind(kind: ErrorKind) -> Self {
42 Error { kind }
43 }
44
45 fn kind(&self) -> &ErrorKind {
46 &self.kind
47 }
48
49 pub fn bool_bad_format() -> Self {
50 Error {
51 kind: ErrorKind::BoolBadFormat,
52 }
53 }
54
55 pub fn integer_bad_format() -> Self {
56 Error {
57 kind: ErrorKind::IntegerBadFormat,
58 }
59 }
60
61 pub fn unsigned_integer_bad_format() -> Self {
62 Error {
63 kind: ErrorKind::UnsignedIntegerBadFormat,
64 }
65 }
66
67 pub fn hyper_bad_format() -> Self {
68 Error {
69 kind: ErrorKind::HyperBadFormat,
70 }
71 }
72
73 pub fn unsigned_hyper_bad_format() -> Self {
74 Error {
75 kind: ErrorKind::UnsignedHyperBadFormat,
76 }
77 }
78
79 pub fn float_bad_format() -> Self {
80 Error {
81 kind: ErrorKind::FloatBadFormat,
82 }
83 }
84
85 pub fn double_bad_format() -> Self {
86 Error {
87 kind: ErrorKind::DoubleBadFormat,
88 }
89 }
90
91 pub fn string_bad_format() -> Self {
92 Error {
93 kind: ErrorKind::StringBadFormat,
94 }
95 }
96
97 pub fn fixed_array_wrong_size() -> Self {
98 Error {
99 kind: ErrorKind::FixedArrayWrongSize,
100 }
101 }
102
103 pub fn var_array_wrong_size() -> Self {
104 Error {
105 kind: ErrorKind::VarArrayWrongSize,
106 }
107 }
108
109 pub fn invalid_enum_value() -> Self {
110 Error {
111 kind: ErrorKind::InvalidEnumValue,
112 }
113 }
114
115 pub fn bad_array_size() -> Self {
116 Error {
117 kind: ErrorKind::BadArraySize,
118 }
119 }
120
121 pub fn invalid_padding() -> Self {
122 Error {
123 kind: ErrorKind::InvalidPadding,
124 }
125 }
126
127 pub fn invalid_json() -> Self {
128 Error {
129 kind: ErrorKind::InvalidJson,
130 }
131 }
132}
133
134impl From<std::str::Utf8Error> for Error {
135 fn from(utf_err: std::str::Utf8Error) -> Self {
136 Error::from_kind(ErrorKind::Utf8Error(utf_err))
137 }
138}
139
140impl From<std::io::Error> for Error {
143 fn from(io_err: std::io::Error) -> Self {
144 Error::from_kind(ErrorKind::IOError(io_err.kind()))
145 }
146}