1use crate::{BufferTooSmall, UnexpectedEndOfBits};
2
3#[derive(Debug, thiserror::Error, PartialEq, Eq)]
4pub enum ReadErrorCause {
5 #[error("Got invalid discriminant {got} while deserializing enum {ty}")]
6 InvalidDiscriminant { ty: &'static str, got: usize },
7 #[error("Could not deserialize primitive while deserializing {ty}")]
8 NotEnoughInput {
9 ty: &'static str,
10 #[source]
11 cause: UnexpectedEndOfBits,
12 },
13 #[error("Read error in manual AbstractBits implementation for {ty}")]
14 Custom {
15 ty: &'static str,
16 #[source]
17 cause: UnexpectedEndOfBits,
18 },
19}
20
21#[derive(Debug, thiserror::Error, PartialEq, Eq)]
22pub enum FromBytesError {
23 #[error(
24 "Could not skip over specified bit padding \
25 in {struct_name} while serializing"
26 )]
27 SkipPadding {
28 struct_name: &'static str,
29 #[source]
30 cause: ReadErrorCause,
31 },
32 #[error("Could not read {field_name} in struct {struct_name}")]
33 ReadField {
34 field_name: &'static str,
35 struct_name: &'static str,
36 #[source]
37 cause: ReadErrorCause,
38 },
39 #[error("Could not read Option {field_name} in struct {struct_name}")]
40 ReadOption {
41 field_name: &'static str,
42 struct_name: &'static str,
43 #[source]
44 cause: ReadErrorCause,
45 },
46 #[error(
47 "Could not read field controlling option: {controlled_option_field} \
48 in struct {struct_name}"
49 )]
50 ReadOptionController {
51 controlled_option_field: &'static str,
52 struct_name: &'static str,
53 #[source]
54 cause: ReadErrorCause,
55 },
56 #[error("Could not read length for list {field_name} in struct {struct_name}")]
57 ReadListLength {
58 field_name: &'static str,
59 struct_name: &'static str,
60 #[source]
61 cause: ReadErrorCause,
62 },
63 #[error(
64 "Could not read {list_len} items into list {field_name}
65 in struct {struct_name}"
66 )]
67 ReadList {
68 list_len: usize,
69 field_name: &'static str,
70 struct_name: &'static str,
71 #[source]
72 cause: ReadErrorCause,
73 },
74 #[error(
75 "Could not read {array_len} items into array {field_name}
76 in struct {struct_name}"
77 )]
78 ReadArray {
79 array_len: usize,
80 field_name: &'static str,
81 struct_name: &'static str,
82 #[source]
83 cause: ReadErrorCause,
84 },
85 #[error("Could not read enum {enum_name}")]
86 ReadEnum {
87 enum_name: &'static str,
88 #[source]
89 cause: ReadErrorCause,
90 },
91 #[error(transparent)]
92 ReadPrimitive(ReadErrorCause),
93}
94
95impl FromBytesError {
96 pub fn skip_padding(self, struct_name: &'static str) -> Self {
97 if let Self::ReadPrimitive(cause) = self {
98 Self::SkipPadding { struct_name, cause }
99 } else {
100 self
101 }
102 }
103 pub fn read_field(self, struct_name: &'static str, field_name: &'static str) -> Self {
104 if let Self::ReadPrimitive(cause) = self {
105 Self::ReadField {
106 field_name,
107 struct_name,
108 cause,
109 }
110 } else {
111 self
112 }
113 }
114 pub fn read_option(
115 self,
116 struct_name: &'static str,
117 field_name: &'static str,
118 ) -> Self {
119 if let Self::ReadPrimitive(cause) = self {
120 Self::ReadOption {
121 field_name,
122 struct_name,
123 cause,
124 }
125 } else {
126 self
127 }
128 }
129 pub fn read_option_controller(
130 self,
131 struct_name: &'static str,
132 controlled_option_field: &'static str,
133 ) -> Self {
134 if let Self::ReadPrimitive(cause) = self {
135 Self::ReadOptionController {
136 controlled_option_field,
137 struct_name,
138 cause,
139 }
140 } else {
141 self
142 }
143 }
144 pub fn read_list_length(
145 self,
146 struct_name: &'static str,
147 field_name: &'static str,
148 ) -> Self {
149 if let Self::ReadPrimitive(cause) = self {
150 Self::ReadListLength {
151 field_name,
152 struct_name,
153 cause,
154 }
155 } else {
156 self
157 }
158 }
159 pub fn read_list(
160 self,
161 struct_name: &'static str,
162 field_name: &'static str,
163 list_len: usize,
164 ) -> Self {
165 if let Self::ReadPrimitive(cause) = self {
166 Self::ReadList {
167 list_len,
168 field_name,
169 struct_name,
170 cause,
171 }
172 } else {
173 self
174 }
175 }
176 pub fn read_array(
177 self,
178 struct_name: &'static str,
179 field_name: &'static str,
180 array_len: usize,
181 ) -> Self {
182 if let Self::ReadPrimitive(cause) = self {
183 Self::ReadArray {
184 array_len,
185 field_name,
186 struct_name,
187 cause,
188 }
189 } else {
190 self
191 }
192 }
193}
194
195#[derive(Debug, thiserror::Error, PartialEq, Eq)]
196pub enum ToBytesError {
197 #[error("List too long to fit. Max length {max}, got: {got}")]
198 ListTooLong { max: usize, got: usize },
199 #[error("Buffer is too small to serialize {ty} into")]
200 BufferTooSmall {
201 ty: &'static str,
202 #[source]
203 cause: BufferTooSmall,
204 },
205 #[error("Buffer is too small to add bit padding specified in {struct_name}")]
206 AddPadding {
207 struct_name: &'static str,
208 #[source]
209 cause: BufferTooSmall,
210 },
211}