diff options
Diffstat (limited to 'src/structs/de.rs')
| -rw-r--r-- | src/structs/de.rs | 365 |
1 files changed, 365 insertions, 0 deletions
diff --git a/src/structs/de.rs b/src/structs/de.rs index e69de29..9158d90 100644 --- a/src/structs/de.rs +++ b/src/structs/de.rs | |||
| @@ -0,0 +1,365 @@ | |||
| 1 | use crate::structs::{Settings, StructError, StructResult}; | ||
| 2 | use crate::ArchiveError; | ||
| 3 | use serde::de; | ||
| 4 | |||
| 5 | pub struct ArchiveDeserializer<'de> { | ||
| 6 | bytes: &'de [u8], | ||
| 7 | pointer: usize, | ||
| 8 | settings: Settings, | ||
| 9 | } | ||
| 10 | |||
| 11 | struct SeqDeserializer<'a, 'de: 'a> { | ||
| 12 | de: &'a mut ArchiveDeserializer<'de>, | ||
| 13 | len: usize, | ||
| 14 | } | ||
| 15 | |||
| 16 | struct EnumDeserializer<'a, 'de: 'a> { | ||
| 17 | de: &'a mut ArchiveDeserializer<'de>, | ||
| 18 | } | ||
| 19 | |||
| 20 | impl<'de> ArchiveDeserializer<'de> { | ||
| 21 | pub fn new(bytes: &'de [u8], settings: Settings) -> Self { | ||
| 22 | Self { | ||
| 23 | bytes, | ||
| 24 | pointer: 0, | ||
| 25 | settings, | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | fn take<const S: usize>(&mut self) -> StructResult<[u8; S]> { | ||
| 30 | let bytes = self | ||
| 31 | .bytes | ||
| 32 | .get(self.pointer..self.pointer + S) | ||
| 33 | .ok_or(StructError::UnexpectedEOF)? | ||
| 34 | .try_into() | ||
| 35 | .unwrap(); | ||
| 36 | self.pointer += S; | ||
| 37 | Ok(bytes) | ||
| 38 | } | ||
| 39 | |||
| 40 | fn take_byte(&mut self) -> StructResult<u8> { | ||
| 41 | let byte = self | ||
| 42 | .bytes | ||
| 43 | .get(self.pointer) | ||
| 44 | .ok_or(StructError::UnexpectedEOF)?; | ||
| 45 | self.pointer += 1; | ||
| 46 | Ok(*byte) | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | impl<'a, 'de> SeqDeserializer<'a, 'de> { | ||
| 51 | pub fn new(de: &'a mut ArchiveDeserializer<'de>, len: usize) -> Self { | ||
| 52 | Self { de, len } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | impl<'a, 'de> EnumDeserializer<'a, 'de> { | ||
| 57 | pub fn new(de: &'a mut ArchiveDeserializer<'de>) -> Self { | ||
| 58 | Self { de } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { | ||
| 63 | type Error = ArchiveError<StructError>; | ||
| 64 | |||
| 65 | fn deserialize_any<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 66 | where | ||
| 67 | V: de::Visitor<'de>, | ||
| 68 | { | ||
| 69 | Err(StructError::DeserializationNotSupported { type_name: "any" }.into()) | ||
| 70 | } | ||
| 71 | |||
| 72 | fn deserialize_bool<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 73 | where | ||
| 74 | V: de::Visitor<'de>, | ||
| 75 | { | ||
| 76 | visitor.visit_bool(self.take_byte()? != 0) | ||
| 77 | } | ||
| 78 | |||
| 79 | fn deserialize_i8<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 80 | where | ||
| 81 | V: de::Visitor<'de>, | ||
| 82 | { | ||
| 83 | visitor.visit_i8(self.take_byte()? as i8) | ||
| 84 | } | ||
| 85 | |||
| 86 | fn deserialize_i16<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 87 | where | ||
| 88 | V: de::Visitor<'de>, | ||
| 89 | { | ||
| 90 | let bytes = self.take()?; | ||
| 91 | visitor.visit_i16(self.settings.byte_order.to_i16(bytes)) | ||
| 92 | } | ||
| 93 | |||
| 94 | fn deserialize_i32<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 95 | where | ||
| 96 | V: de::Visitor<'de>, | ||
| 97 | { | ||
| 98 | let bytes = self.take()?; | ||
| 99 | visitor.visit_i32(self.settings.byte_order.to_i32(bytes)) | ||
| 100 | } | ||
| 101 | |||
| 102 | fn deserialize_i64<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 103 | where | ||
| 104 | V: de::Visitor<'de>, | ||
| 105 | { | ||
| 106 | let bytes = self.take()?; | ||
| 107 | visitor.visit_i64(self.settings.byte_order.to_i64(bytes)) | ||
| 108 | } | ||
| 109 | |||
| 110 | fn deserialize_u8<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 111 | where | ||
| 112 | V: de::Visitor<'de>, | ||
| 113 | { | ||
| 114 | visitor.visit_u8(self.take_byte()?) | ||
| 115 | } | ||
| 116 | |||
| 117 | fn deserialize_u16<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 118 | where | ||
| 119 | V: de::Visitor<'de>, | ||
| 120 | { | ||
| 121 | let bytes = self.take()?; | ||
| 122 | visitor.visit_u16(self.settings.byte_order.to_u16(bytes)) | ||
| 123 | } | ||
| 124 | |||
| 125 | fn deserialize_u32<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 126 | where | ||
| 127 | V: de::Visitor<'de>, | ||
| 128 | { | ||
| 129 | let bytes = self.take()?; | ||
| 130 | visitor.visit_u32(self.settings.byte_order.to_u32(bytes)) | ||
| 131 | } | ||
| 132 | |||
| 133 | fn deserialize_u64<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 134 | where | ||
| 135 | V: de::Visitor<'de>, | ||
| 136 | { | ||
| 137 | let bytes = self.take()?; | ||
| 138 | visitor.visit_u64(self.settings.byte_order.to_u64(bytes)) | ||
| 139 | } | ||
| 140 | |||
| 141 | fn deserialize_f32<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 142 | where | ||
| 143 | V: de::Visitor<'de>, | ||
| 144 | { | ||
| 145 | let bytes = self.take()?; | ||
| 146 | visitor.visit_f32(self.settings.byte_order.to_f32(bytes)) | ||
| 147 | } | ||
| 148 | |||
| 149 | fn deserialize_f64<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 150 | where | ||
| 151 | V: de::Visitor<'de>, | ||
| 152 | { | ||
| 153 | let bytes = self.take()?; | ||
| 154 | visitor.visit_f64(self.settings.byte_order.to_f64(bytes)) | ||
| 155 | } | ||
| 156 | |||
| 157 | fn deserialize_char<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 158 | where | ||
| 159 | V: de::Visitor<'de>, | ||
| 160 | { | ||
| 161 | Err(StructError::DeserializationNotSupported { type_name: "char" }.into()) | ||
| 162 | } | ||
| 163 | |||
| 164 | fn deserialize_str<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 165 | where | ||
| 166 | V: de::Visitor<'de>, | ||
| 167 | { | ||
| 168 | Err(StructError::DeserializationNotSupported { type_name: "str" }.into()) | ||
| 169 | } | ||
| 170 | |||
| 171 | fn deserialize_string<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 172 | where | ||
| 173 | V: de::Visitor<'de>, | ||
| 174 | { | ||
| 175 | Err(StructError::DeserializationNotSupported { | ||
| 176 | type_name: "string", | ||
| 177 | } | ||
| 178 | .into()) | ||
| 179 | } | ||
| 180 | |||
| 181 | fn deserialize_bytes<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 182 | where | ||
| 183 | V: de::Visitor<'de>, | ||
| 184 | { | ||
| 185 | Err(StructError::DeserializationNotSupported { type_name: "bytes" }.into()) | ||
| 186 | } | ||
| 187 | |||
| 188 | fn deserialize_byte_buf<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 189 | where | ||
| 190 | V: de::Visitor<'de>, | ||
| 191 | { | ||
| 192 | Err(StructError::DeserializationNotSupported { type_name: "bytes" }.into()) | ||
| 193 | } | ||
| 194 | |||
| 195 | fn deserialize_option<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 196 | where | ||
| 197 | V: de::Visitor<'de>, | ||
| 198 | { | ||
| 199 | Err(StructError::DeserializationNotSupported { | ||
| 200 | type_name: "optional", | ||
| 201 | } | ||
| 202 | .into()) | ||
| 203 | } | ||
| 204 | |||
| 205 | fn deserialize_unit<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 206 | where | ||
| 207 | V: de::Visitor<'de>, | ||
| 208 | { | ||
| 209 | visitor.visit_unit() | ||
| 210 | } | ||
| 211 | |||
| 212 | fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> StructResult<V::Value> | ||
| 213 | where | ||
| 214 | V: de::Visitor<'de>, | ||
| 215 | { | ||
| 216 | self.deserialize_unit(visitor) | ||
| 217 | } | ||
| 218 | |||
| 219 | fn deserialize_newtype_struct<V>( | ||
| 220 | self, | ||
| 221 | _name: &'static str, | ||
| 222 | visitor: V, | ||
| 223 | ) -> StructResult<V::Value> | ||
| 224 | where | ||
| 225 | V: de::Visitor<'de>, | ||
| 226 | { | ||
| 227 | visitor.visit_newtype_struct(self) | ||
| 228 | } | ||
| 229 | |||
| 230 | fn deserialize_seq<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 231 | where | ||
| 232 | V: de::Visitor<'de>, | ||
| 233 | { | ||
| 234 | Err(StructError::DeserializationNotSupported { type_name: "seq" }.into()) | ||
| 235 | } | ||
| 236 | |||
| 237 | fn deserialize_tuple<V>(self, len: usize, visitor: V) -> StructResult<V::Value> | ||
| 238 | where | ||
| 239 | V: de::Visitor<'de>, | ||
| 240 | { | ||
| 241 | visitor.visit_seq(SeqDeserializer::new(self, len)) | ||
| 242 | } | ||
| 243 | |||
| 244 | fn deserialize_tuple_struct<V>( | ||
| 245 | self, | ||
| 246 | _name: &'static str, | ||
| 247 | len: usize, | ||
| 248 | visitor: V, | ||
| 249 | ) -> StructResult<V::Value> | ||
| 250 | where | ||
| 251 | V: de::Visitor<'de>, | ||
| 252 | { | ||
| 253 | self.deserialize_tuple(len, visitor) | ||
| 254 | } | ||
| 255 | |||
| 256 | fn deserialize_map<V>(self, _visitor: V) -> StructResult<V::Value> | ||
| 257 | where | ||
| 258 | V: de::Visitor<'de>, | ||
| 259 | { | ||
| 260 | Err(StructError::DeserializationNotSupported { type_name: "map" }.into()) | ||
| 261 | } | ||
| 262 | |||
| 263 | fn deserialize_struct<V>( | ||
| 264 | self, | ||
| 265 | _name: &'static str, | ||
| 266 | fields: &'static [&'static str], | ||
| 267 | visitor: V, | ||
| 268 | ) -> StructResult<V::Value> | ||
| 269 | where | ||
| 270 | V: de::Visitor<'de>, | ||
| 271 | { | ||
| 272 | self.deserialize_tuple(fields.len(), visitor) | ||
| 273 | } | ||
| 274 | |||
| 275 | fn deserialize_enum<V>( | ||
| 276 | self, | ||
| 277 | _name: &'static str, | ||
| 278 | _variants: &'static [&'static str], | ||
| 279 | visitor: V, | ||
| 280 | ) -> StructResult<V::Value> | ||
| 281 | where | ||
| 282 | V: de::Visitor<'de>, | ||
| 283 | { | ||
| 284 | visitor.visit_enum(EnumDeserializer::new(self)) | ||
| 285 | } | ||
| 286 | |||
| 287 | fn deserialize_identifier<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 288 | where | ||
| 289 | V: de::Visitor<'de>, | ||
| 290 | { | ||
| 291 | match self.settings.variant_index_type { | ||
| 292 | super::VariantIndexType::U8 => self.deserialize_u8(visitor), | ||
| 293 | super::VariantIndexType::U16 => self.deserialize_u16(visitor), | ||
| 294 | super::VariantIndexType::U32 => self.deserialize_u32(visitor), | ||
| 295 | super::VariantIndexType::U64 => self.deserialize_u64(visitor), | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | #[inline] | ||
| 300 | fn deserialize_ignored_any<V>(self, visitor: V) -> StructResult<V::Value> | ||
| 301 | where | ||
| 302 | V: de::Visitor<'de>, | ||
| 303 | { | ||
| 304 | self.deserialize_any(visitor) | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | impl<'a, 'de> de::SeqAccess<'de> for SeqDeserializer<'a, 'de> { | ||
| 309 | type Error = ArchiveError<StructError>; | ||
| 310 | |||
| 311 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> | ||
| 312 | where | ||
| 313 | T: de::DeserializeSeed<'de>, | ||
| 314 | { | ||
| 315 | if self.len == 0 { | ||
| 316 | return Ok(None); | ||
| 317 | } | ||
| 318 | seed.deserialize(&mut *self.de).map(Some) | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | impl<'a, 'de> de::EnumAccess<'de> for EnumDeserializer<'a, 'de> { | ||
| 323 | type Error = ArchiveError<StructError>; | ||
| 324 | type Variant = Self; | ||
| 325 | |||
| 326 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> | ||
| 327 | where | ||
| 328 | V: de::DeserializeSeed<'de>, | ||
| 329 | { | ||
| 330 | Ok((seed.deserialize(&mut *self.de)?, self)) | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | impl<'a, 'de> de::VariantAccess<'de> for EnumDeserializer<'a, 'de> { | ||
| 335 | type Error = ArchiveError<StructError>; | ||
| 336 | |||
| 337 | fn unit_variant(self) -> Result<(), Self::Error> { | ||
| 338 | Ok(()) | ||
| 339 | } | ||
| 340 | |||
| 341 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> | ||
| 342 | where | ||
| 343 | T: de::DeserializeSeed<'de>, | ||
| 344 | { | ||
| 345 | seed.deserialize(&mut *self.de) | ||
| 346 | } | ||
| 347 | |||
| 348 | fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> | ||
| 349 | where | ||
| 350 | V: de::Visitor<'de>, | ||
| 351 | { | ||
| 352 | de::Deserializer::deserialize_tuple(self.de, len, visitor) | ||
| 353 | } | ||
| 354 | |||
| 355 | fn struct_variant<V>( | ||
| 356 | self, | ||
| 357 | fields: &'static [&'static str], | ||
| 358 | visitor: V, | ||
| 359 | ) -> Result<V::Value, Self::Error> | ||
| 360 | where | ||
| 361 | V: de::Visitor<'de>, | ||
| 362 | { | ||
| 363 | de::Deserializer::deserialize_tuple(self.de, fields.len(), visitor) | ||
| 364 | } | ||
| 365 | } | ||
