aboutsummaryrefslogtreecommitdiff
path: root/src/structs
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-07-20 16:52:39 +0900
committerIgor Tolmachev <me@igorek.dev>2024-07-20 16:52:39 +0900
commit7bcdc3b4ca460aec2b98fb2dca6165788c562b05 (patch)
tree63f9616fc1b7f9ca6e414a4d32910720e155690c /src/structs
parent5f4ceda88c7299deb317f8d22a99ab2521c5a380 (diff)
downloadarchivator-7bcdc3b4ca460aec2b98fb2dca6165788c562b05.tar.gz
archivator-7bcdc3b4ca460aec2b98fb2dca6165788c562b05.zip
Partial aes implementation and others improvements
Diffstat (limited to 'src/structs')
-rw-r--r--src/structs/de.rs33
-rw-r--r--src/structs/error.rs41
-rw-r--r--src/structs/ser.rs47
3 files changed, 56 insertions, 65 deletions
diff --git a/src/structs/de.rs b/src/structs/de.rs
index edd8011..8b2130e 100644
--- a/src/structs/de.rs
+++ b/src/structs/de.rs
@@ -1,5 +1,4 @@
1use crate::structs::{Settings, StructError, StructResult}; 1use crate::structs::{Settings, StructError, StructResult};
2use crate::ArchiveError;
3use serde::de; 2use serde::de;
4 3
5pub struct ArchiveDeserializer<'de> { 4pub struct ArchiveDeserializer<'de> {
@@ -60,14 +59,14 @@ impl<'a, 'de> EnumDeserializer<'a, 'de> {
60} 59}
61 60
62impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { 61impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
63 type Error = ArchiveError<StructError>; 62 type Error = StructError;
64 63
65 #[inline] 64 #[inline]
66 fn deserialize_any<V>(self, _visitor: V) -> StructResult<V::Value> 65 fn deserialize_any<V>(self, _visitor: V) -> StructResult<V::Value>
67 where 66 where
68 V: de::Visitor<'de>, 67 V: de::Visitor<'de>,
69 { 68 {
70 Err(StructError::DeserializationNotSupported { type_name: "any" }.into()) 69 Err(StructError::DeserializationNotSupported("any"))
71 } 70 }
72 71
73 fn deserialize_bool<V>(self, visitor: V) -> StructResult<V::Value> 72 fn deserialize_bool<V>(self, visitor: V) -> StructResult<V::Value>
@@ -160,7 +159,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
160 where 159 where
161 V: de::Visitor<'de>, 160 V: de::Visitor<'de>,
162 { 161 {
163 Err(StructError::DeserializationNotSupported { type_name: "char" }.into()) 162 Err(StructError::DeserializationNotSupported("char"))
164 } 163 }
165 164
166 #[inline] 165 #[inline]
@@ -168,7 +167,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
168 where 167 where
169 V: de::Visitor<'de>, 168 V: de::Visitor<'de>,
170 { 169 {
171 Err(StructError::DeserializationNotSupported { type_name: "str" }.into()) 170 Err(StructError::DeserializationNotSupported("str"))
172 } 171 }
173 172
174 #[inline] 173 #[inline]
@@ -176,10 +175,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
176 where 175 where
177 V: de::Visitor<'de>, 176 V: de::Visitor<'de>,
178 { 177 {
179 Err(StructError::DeserializationNotSupported { 178 Err(StructError::DeserializationNotSupported("string"))
180 type_name: "string",
181 }
182 .into())
183 } 179 }
184 180
185 #[inline] 181 #[inline]
@@ -187,7 +183,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
187 where 183 where
188 V: de::Visitor<'de>, 184 V: de::Visitor<'de>,
189 { 185 {
190 Err(StructError::DeserializationNotSupported { type_name: "bytes" }.into()) 186 Err(StructError::DeserializationNotSupported("bytes"))
191 } 187 }
192 188
193 #[inline] 189 #[inline]
@@ -195,7 +191,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
195 where 191 where
196 V: de::Visitor<'de>, 192 V: de::Visitor<'de>,
197 { 193 {
198 Err(StructError::DeserializationNotSupported { type_name: "bytes" }.into()) 194 Err(StructError::DeserializationNotSupported("bytes"))
199 } 195 }
200 196
201 #[inline] 197 #[inline]
@@ -203,10 +199,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
203 where 199 where
204 V: de::Visitor<'de>, 200 V: de::Visitor<'de>,
205 { 201 {
206 Err(StructError::DeserializationNotSupported { 202 Err(StructError::DeserializationNotSupported("optional"))
207 type_name: "optional",
208 }
209 .into())
210 } 203 }
211 204
212 #[inline] 205 #[inline]
@@ -242,7 +235,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
242 where 235 where
243 V: de::Visitor<'de>, 236 V: de::Visitor<'de>,
244 { 237 {
245 Err(StructError::DeserializationNotSupported { type_name: "seq" }.into()) 238 Err(StructError::DeserializationNotSupported("seq"))
246 } 239 }
247 240
248 #[inline] 241 #[inline]
@@ -271,7 +264,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
271 where 264 where
272 V: de::Visitor<'de>, 265 V: de::Visitor<'de>,
273 { 266 {
274 Err(StructError::DeserializationNotSupported { type_name: "map" }.into()) 267 Err(StructError::DeserializationNotSupported("map"))
275 } 268 }
276 269
277 #[inline] 270 #[inline]
@@ -322,7 +315,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> {
322} 315}
323 316
324impl<'a, 'de> de::SeqAccess<'de> for SeqDeserializer<'a, 'de> { 317impl<'a, 'de> de::SeqAccess<'de> for SeqDeserializer<'a, 'de> {
325 type Error = ArchiveError<StructError>; 318 type Error = StructError;
326 319
327 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> 320 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
328 where 321 where
@@ -336,7 +329,7 @@ impl<'a, 'de> de::SeqAccess<'de> for SeqDeserializer<'a, 'de> {
336} 329}
337 330
338impl<'a, 'de> de::EnumAccess<'de> for EnumDeserializer<'a, 'de> { 331impl<'a, 'de> de::EnumAccess<'de> for EnumDeserializer<'a, 'de> {
339 type Error = ArchiveError<StructError>; 332 type Error = StructError;
340 type Variant = Self; 333 type Variant = Self;
341 334
342 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> 335 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
@@ -348,7 +341,7 @@ impl<'a, 'de> de::EnumAccess<'de> for EnumDeserializer<'a, 'de> {
348} 341}
349 342
350impl<'a, 'de> de::VariantAccess<'de> for EnumDeserializer<'a, 'de> { 343impl<'a, 'de> de::VariantAccess<'de> for EnumDeserializer<'a, 'de> {
351 type Error = ArchiveError<StructError>; 344 type Error = StructError;
352 345
353 #[inline] 346 #[inline]
354 fn unit_variant(self) -> Result<(), Self::Error> { 347 fn unit_variant(self) -> Result<(), Self::Error> {
diff --git a/src/structs/error.rs b/src/structs/error.rs
index f07163c..deb10b3 100644
--- a/src/structs/error.rs
+++ b/src/structs/error.rs
@@ -1,37 +1,42 @@
1use crate::{ArchiveError, ArchiveResult}; 1use serde::{de, ser};
2use std::error::Error; 2use std::error::Error;
3use std::fmt::Display; 3use std::fmt::Display;
4 4
5pub type StructResult<T> = ArchiveResult<T, StructError>; 5pub type StructResult<T> = Result<T, StructError>;
6 6
7#[derive(Debug)] 7#[derive(Debug, PartialEq, Eq)]
8pub enum StructError { 8pub enum StructError {
9 SerializationNotSupported { type_name: &'static str }, 9 SerializationNotSupported(&'static str),
10 DeserializationNotSupported { type_name: &'static str }, 10 DeserializationNotSupported(&'static str),
11 Serde(String),
11 UnexpectedEOF, 12 UnexpectedEOF,
12} 13}
13 14
14impl From<StructError> for ArchiveError<StructError> {
15 fn from(value: StructError) -> Self {
16 Self::Archivator {
17 module: "Struct serializer",
18 error: value,
19 }
20 }
21}
22
23impl Display for StructError { 15impl Display for StructError {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 match self { 17 match self {
26 Self::SerializationNotSupported { type_name } => { 18 Self::SerializationNotSupported(name) => {
27 writeln!(f, "Serialization for type '{type_name}' not supported") 19 writeln!(f, "Serialization for type '{}' not supported", name)
28 } 20 }
29 Self::DeserializationNotSupported { type_name } => { 21 Self::DeserializationNotSupported(name) => {
30 writeln!(f, "Deserialization for type '{type_name}' not supported") 22 writeln!(f, "Deserialization for type '{}' not supported", name)
31 } 23 }
24 Self::Serde(message) => writeln!(f, "Serde error: {}", message),
32 Self::UnexpectedEOF => writeln!(f, "Unexpected EOF"), 25 Self::UnexpectedEOF => writeln!(f, "Unexpected EOF"),
33 } 26 }
34 } 27 }
35} 28}
36 29
37impl Error for StructError {} 30impl Error for StructError {}
31
32impl ser::Error for StructError {
33 fn custom<T: Display>(msg: T) -> Self {
34 Self::Serde(msg.to_string())
35 }
36}
37
38impl de::Error for StructError {
39 fn custom<T: Display>(msg: T) -> Self {
40 Self::Serde(msg.to_string())
41 }
42}
diff --git a/src/structs/ser.rs b/src/structs/ser.rs
index f7e694a..b633dff 100644
--- a/src/structs/ser.rs
+++ b/src/structs/ser.rs
@@ -1,5 +1,4 @@
1use crate::structs::{Settings, StructError, StructResult}; 1use crate::structs::{Settings, StructError, StructResult};
2use crate::ArchiveError;
3use serde::{ser, Serialize}; 2use serde::{ser, Serialize};
4 3
5pub struct ArchiveSerializer { 4pub struct ArchiveSerializer {
@@ -22,7 +21,7 @@ impl ArchiveSerializer {
22 21
23impl ser::Serializer for &mut ArchiveSerializer { 22impl ser::Serializer for &mut ArchiveSerializer {
24 type Ok = (); 23 type Ok = ();
25 type Error = ArchiveError<StructError>; 24 type Error = StructError;
26 25
27 type SerializeSeq = Self; 26 type SerializeSeq = Self;
28 type SerializeTuple = Self; 27 type SerializeTuple = Self;
@@ -89,25 +88,22 @@ impl ser::Serializer for &mut ArchiveSerializer {
89 88
90 #[inline] 89 #[inline]
91 fn serialize_char(self, _v: char) -> StructResult<()> { 90 fn serialize_char(self, _v: char) -> StructResult<()> {
92 Err(StructError::SerializationNotSupported { type_name: "char" }.into()) 91 Err(StructError::SerializationNotSupported("char"))
93 } 92 }
94 93
95 #[inline] 94 #[inline]
96 fn serialize_str(self, _v: &str) -> StructResult<()> { 95 fn serialize_str(self, _v: &str) -> StructResult<()> {
97 Err(StructError::SerializationNotSupported { type_name: "str" }.into()) 96 Err(StructError::SerializationNotSupported("str"))
98 } 97 }
99 98
100 #[inline] 99 #[inline]
101 fn serialize_bytes(self, _v: &[u8]) -> StructResult<()> { 100 fn serialize_bytes(self, _v: &[u8]) -> StructResult<()> {
102 Err(StructError::SerializationNotSupported { type_name: "bytes" }.into()) 101 Err(StructError::SerializationNotSupported("bytes"))
103 } 102 }
104 103
105 #[inline] 104 #[inline]
106 fn serialize_none(self) -> StructResult<()> { 105 fn serialize_none(self) -> StructResult<()> {
107 Err(StructError::SerializationNotSupported { 106 Err(StructError::SerializationNotSupported("optional"))
108 type_name: "optional",
109 }
110 .into())
111 } 107 }
112 108
113 #[inline] 109 #[inline]
@@ -115,10 +111,7 @@ impl ser::Serializer for &mut ArchiveSerializer {
115 where 111 where
116 T: ?Sized + Serialize, 112 T: ?Sized + Serialize,
117 { 113 {
118 Err(StructError::SerializationNotSupported { 114 Err(StructError::SerializationNotSupported("optional"))
119 type_name: "optional",
120 }
121 .into())
122 } 115 }
123 116
124 #[inline] 117 #[inline]
@@ -175,7 +168,7 @@ impl ser::Serializer for &mut ArchiveSerializer {
175 168
176 #[inline] 169 #[inline]
177 fn serialize_seq(self, _len: Option<usize>) -> StructResult<Self::SerializeSeq> { 170 fn serialize_seq(self, _len: Option<usize>) -> StructResult<Self::SerializeSeq> {
178 Err(StructError::SerializationNotSupported { type_name: "seq" }.into()) 171 Err(StructError::SerializationNotSupported("seq"))
179 } 172 }
180 173
181 #[inline] 174 #[inline]
@@ -210,7 +203,7 @@ impl ser::Serializer for &mut ArchiveSerializer {
210 203
211 #[inline] 204 #[inline]
212 fn serialize_map(self, _len: Option<usize>) -> StructResult<Self::SerializeMap> { 205 fn serialize_map(self, _len: Option<usize>) -> StructResult<Self::SerializeMap> {
213 Err(StructError::SerializationNotSupported { type_name: "map" }.into()) 206 Err(StructError::SerializationNotSupported("map"))
214 } 207 }
215 208
216 #[inline] 209 #[inline]
@@ -241,25 +234,25 @@ impl ser::Serializer for &mut ArchiveSerializer {
241 234
242impl ser::SerializeSeq for &mut ArchiveSerializer { 235impl ser::SerializeSeq for &mut ArchiveSerializer {
243 type Ok = (); 236 type Ok = ();
244 type Error = ArchiveError<StructError>; 237 type Error = StructError;
245 238
246 #[inline] 239 #[inline]
247 fn serialize_element<T>(&mut self, _value: &T) -> StructResult<()> 240 fn serialize_element<T>(&mut self, _value: &T) -> StructResult<()>
248 where 241 where
249 T: ?Sized + Serialize, 242 T: ?Sized + Serialize,
250 { 243 {
251 Err(StructError::SerializationNotSupported { type_name: "seq" }.into()) 244 Err(StructError::SerializationNotSupported("seq"))
252 } 245 }
253 246
254 #[inline] 247 #[inline]
255 fn end(self) -> StructResult<()> { 248 fn end(self) -> StructResult<()> {
256 Err(StructError::SerializationNotSupported { type_name: "seq" }.into()) 249 Err(StructError::SerializationNotSupported("seq"))
257 } 250 }
258} 251}
259 252
260impl ser::SerializeTuple for &mut ArchiveSerializer { 253impl ser::SerializeTuple for &mut ArchiveSerializer {
261 type Ok = (); 254 type Ok = ();
262 type Error = ArchiveError<StructError>; 255 type Error = StructError;
263 256
264 #[inline] 257 #[inline]
265 fn serialize_element<T>(&mut self, value: &T) -> StructResult<()> 258 fn serialize_element<T>(&mut self, value: &T) -> StructResult<()>
@@ -277,7 +270,7 @@ impl ser::SerializeTuple for &mut ArchiveSerializer {
277 270
278impl ser::SerializeTupleStruct for &mut ArchiveSerializer { 271impl ser::SerializeTupleStruct for &mut ArchiveSerializer {
279 type Ok = (); 272 type Ok = ();
280 type Error = ArchiveError<StructError>; 273 type Error = StructError;
281 274
282 #[inline] 275 #[inline]
283 fn serialize_field<T>(&mut self, value: &T) -> StructResult<()> 276 fn serialize_field<T>(&mut self, value: &T) -> StructResult<()>
@@ -295,7 +288,7 @@ impl ser::SerializeTupleStruct for &mut ArchiveSerializer {
295 288
296impl ser::SerializeTupleVariant for &mut ArchiveSerializer { 289impl ser::SerializeTupleVariant for &mut ArchiveSerializer {
297 type Ok = (); 290 type Ok = ();
298 type Error = ArchiveError<StructError>; 291 type Error = StructError;
299 292
300 #[inline] 293 #[inline]
301 fn serialize_field<T>(&mut self, value: &T) -> StructResult<()> 294 fn serialize_field<T>(&mut self, value: &T) -> StructResult<()>
@@ -313,14 +306,14 @@ impl ser::SerializeTupleVariant for &mut ArchiveSerializer {
313 306
314impl ser::SerializeMap for &mut ArchiveSerializer { 307impl ser::SerializeMap for &mut ArchiveSerializer {
315 type Ok = (); 308 type Ok = ();
316 type Error = ArchiveError<StructError>; 309 type Error = StructError;
317 310
318 #[inline] 311 #[inline]
319 fn serialize_key<T>(&mut self, _key: &T) -> StructResult<()> 312 fn serialize_key<T>(&mut self, _key: &T) -> StructResult<()>
320 where 313 where
321 T: ?Sized + Serialize, 314 T: ?Sized + Serialize,
322 { 315 {
323 Err(StructError::SerializationNotSupported { type_name: "map" }.into()) 316 Err(StructError::SerializationNotSupported("map"))
324 } 317 }
325 318
326 #[inline] 319 #[inline]
@@ -328,18 +321,18 @@ impl ser::SerializeMap for &mut ArchiveSerializer {
328 where 321 where
329 T: ?Sized + Serialize, 322 T: ?Sized + Serialize,
330 { 323 {
331 Err(StructError::SerializationNotSupported { type_name: "map" }.into()) 324 Err(StructError::SerializationNotSupported("map"))
332 } 325 }
333 326
334 #[inline] 327 #[inline]
335 fn end(self) -> StructResult<()> { 328 fn end(self) -> StructResult<()> {
336 Err(StructError::SerializationNotSupported { type_name: "map" }.into()) 329 Err(StructError::SerializationNotSupported("map"))
337 } 330 }
338} 331}
339 332
340impl ser::SerializeStruct for &mut ArchiveSerializer { 333impl ser::SerializeStruct for &mut ArchiveSerializer {
341 type Ok = (); 334 type Ok = ();
342 type Error = ArchiveError<StructError>; 335 type Error = StructError;
343 336
344 #[inline] 337 #[inline]
345 fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> StructResult<()> 338 fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> StructResult<()>
@@ -357,7 +350,7 @@ impl ser::SerializeStruct for &mut ArchiveSerializer {
357 350
358impl ser::SerializeStructVariant for &mut ArchiveSerializer { 351impl ser::SerializeStructVariant for &mut ArchiveSerializer {
359 type Ok = (); 352 type Ok = ();
360 type Error = ArchiveError<StructError>; 353 type Error = StructError;
361 354
362 #[inline] 355 #[inline]
363 fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> StructResult<()> 356 fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> StructResult<()>