1use {
2 failure::{Compat, Fail},
3 serde::ser,
4 std::{
5 error,
6 fmt::{self, Display, Formatter},
7 io, result,
8 },
9};
10
11#[derive(Debug, Fail)]
13pub enum SerializationError {
14 #[fail(display = "custom error message: {}", message)]
16 Custom {
17 message: String,
19 },
20
21 #[fail(display = "failed to serialize {}", what)]
23 Failure {
24 what: String,
26 #[cause]
28 cause: Box<CompatSerializationError>,
29 },
30
31 #[fail(display = "IO error while serializing {}: {}", what, cause)]
33 IoError {
34 what: String,
36 #[cause]
38 cause: io::Error,
39 },
40
41 #[fail(display = "XDR does not support a map type")]
43 MapIsNotSupported,
44
45 #[fail(display = "opaque data is too long: {} bytes", length)]
47 OpaqueDataIsTooLong {
48 length: usize,
51 },
52
53 #[fail(display = "fatal failure while serializing {}", type_name)]
57 SequenceOrTupleFatalError {
58 type_name: String,
60 },
61
62 #[fail(display = "fatal failure while serializing struct: {}", name)]
66 StructFatalError {
67 name: String,
69 },
70
71 #[fail(display = "sequence is too long to be serialized: {}", length)]
73 SequenceTooLong {
74 length: usize,
77 },
78
79 #[fail(display = "can't serialize sequence with unknown length")]
81 SequenceWithUnknownLength,
82
83 #[fail(display = "string is not ASCII encoded: {}", string)]
85 StringIsNotAscii {
86 string: String,
88 },
89
90 #[fail(display = "string is too long: {}", string)]
92 StringIsTooLong {
93 string: String,
96 },
97}
98
99#[derive(Debug)]
103pub struct CompatSerializationError(Compat<SerializationError>);
104
105impl From<SerializationError> for CompatSerializationError {
106 fn from(error: SerializationError) -> Self {
107 CompatSerializationError(error.compat())
108 }
109}
110
111impl Display for CompatSerializationError {
112 fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
113 self.0.fmt(formatter)
114 }
115}
116
117impl error::Error for CompatSerializationError {
118 fn description(&self) -> &str {
119 self.0.description()
120 }
121}
122
123impl ser::Error for CompatSerializationError {
124 fn custom<T: Display>(message: T) -> Self {
125 let error = SerializationError::Custom {
126 message: message.to_string(),
127 };
128
129 CompatSerializationError(error.compat())
130 }
131}
132
133impl From<CompatSerializationError> for SerializationError {
134 fn from(wrapped_error: CompatSerializationError) -> Self {
135 match wrapped_error {
136 CompatSerializationError(error) => error.into_inner(),
137 }
138 }
139}
140
141pub type Result<T> = result::Result<T, CompatSerializationError>;