From 7bcdc3b4ca460aec2b98fb2dca6165788c562b05 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Sat, 20 Jul 2024 16:52:39 +0900 Subject: Partial aes implementation and others improvements --- src/structs/de.rs | 33 +++++++++++++-------------------- src/structs/error.rs | 41 +++++++++++++++++++++++------------------ src/structs/ser.rs | 47 ++++++++++++++++++++--------------------------- 3 files changed, 56 insertions(+), 65 deletions(-) (limited to 'src/structs') 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 @@ use crate::structs::{Settings, StructError, StructResult}; -use crate::ArchiveError; use serde::de; pub struct ArchiveDeserializer<'de> { @@ -60,14 +59,14 @@ impl<'a, 'de> EnumDeserializer<'a, 'de> { } impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { - type Error = ArchiveError; + type Error = StructError; #[inline] fn deserialize_any(self, _visitor: V) -> StructResult where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { type_name: "any" }.into()) + Err(StructError::DeserializationNotSupported("any")) } fn deserialize_bool(self, visitor: V) -> StructResult @@ -160,7 +159,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { type_name: "char" }.into()) + Err(StructError::DeserializationNotSupported("char")) } #[inline] @@ -168,7 +167,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { type_name: "str" }.into()) + Err(StructError::DeserializationNotSupported("str")) } #[inline] @@ -176,10 +175,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { - type_name: "string", - } - .into()) + Err(StructError::DeserializationNotSupported("string")) } #[inline] @@ -187,7 +183,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { type_name: "bytes" }.into()) + Err(StructError::DeserializationNotSupported("bytes")) } #[inline] @@ -195,7 +191,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { type_name: "bytes" }.into()) + Err(StructError::DeserializationNotSupported("bytes")) } #[inline] @@ -203,10 +199,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { - type_name: "optional", - } - .into()) + Err(StructError::DeserializationNotSupported("optional")) } #[inline] @@ -242,7 +235,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { type_name: "seq" }.into()) + Err(StructError::DeserializationNotSupported("seq")) } #[inline] @@ -271,7 +264,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { where V: de::Visitor<'de>, { - Err(StructError::DeserializationNotSupported { type_name: "map" }.into()) + Err(StructError::DeserializationNotSupported("map")) } #[inline] @@ -322,7 +315,7 @@ impl<'de> de::Deserializer<'de> for &mut ArchiveDeserializer<'de> { } impl<'a, 'de> de::SeqAccess<'de> for SeqDeserializer<'a, 'de> { - type Error = ArchiveError; + type Error = StructError; fn next_element_seed(&mut self, seed: T) -> Result, Self::Error> where @@ -336,7 +329,7 @@ impl<'a, 'de> de::SeqAccess<'de> for SeqDeserializer<'a, 'de> { } impl<'a, 'de> de::EnumAccess<'de> for EnumDeserializer<'a, 'de> { - type Error = ArchiveError; + type Error = StructError; type Variant = Self; fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> @@ -348,7 +341,7 @@ impl<'a, 'de> de::EnumAccess<'de> for EnumDeserializer<'a, 'de> { } impl<'a, 'de> de::VariantAccess<'de> for EnumDeserializer<'a, 'de> { - type Error = ArchiveError; + type Error = StructError; #[inline] 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 @@ -use crate::{ArchiveError, ArchiveResult}; +use serde::{de, ser}; use std::error::Error; use std::fmt::Display; -pub type StructResult = ArchiveResult; +pub type StructResult = Result; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub enum StructError { - SerializationNotSupported { type_name: &'static str }, - DeserializationNotSupported { type_name: &'static str }, + SerializationNotSupported(&'static str), + DeserializationNotSupported(&'static str), + Serde(String), UnexpectedEOF, } -impl From for ArchiveError { - fn from(value: StructError) -> Self { - Self::Archivator { - module: "Struct serializer", - error: value, - } - } -} - impl Display for StructError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::SerializationNotSupported { type_name } => { - writeln!(f, "Serialization for type '{type_name}' not supported") + Self::SerializationNotSupported(name) => { + writeln!(f, "Serialization for type '{}' not supported", name) } - Self::DeserializationNotSupported { type_name } => { - writeln!(f, "Deserialization for type '{type_name}' not supported") + Self::DeserializationNotSupported(name) => { + writeln!(f, "Deserialization for type '{}' not supported", name) } + Self::Serde(message) => writeln!(f, "Serde error: {}", message), Self::UnexpectedEOF => writeln!(f, "Unexpected EOF"), } } } impl Error for StructError {} + +impl ser::Error for StructError { + fn custom(msg: T) -> Self { + Self::Serde(msg.to_string()) + } +} + +impl de::Error for StructError { + fn custom(msg: T) -> Self { + Self::Serde(msg.to_string()) + } +} 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 @@ use crate::structs::{Settings, StructError, StructResult}; -use crate::ArchiveError; use serde::{ser, Serialize}; pub struct ArchiveSerializer { @@ -22,7 +21,7 @@ impl ArchiveSerializer { impl ser::Serializer for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; type SerializeSeq = Self; type SerializeTuple = Self; @@ -89,25 +88,22 @@ impl ser::Serializer for &mut ArchiveSerializer { #[inline] fn serialize_char(self, _v: char) -> StructResult<()> { - Err(StructError::SerializationNotSupported { type_name: "char" }.into()) + Err(StructError::SerializationNotSupported("char")) } #[inline] fn serialize_str(self, _v: &str) -> StructResult<()> { - Err(StructError::SerializationNotSupported { type_name: "str" }.into()) + Err(StructError::SerializationNotSupported("str")) } #[inline] fn serialize_bytes(self, _v: &[u8]) -> StructResult<()> { - Err(StructError::SerializationNotSupported { type_name: "bytes" }.into()) + Err(StructError::SerializationNotSupported("bytes")) } #[inline] fn serialize_none(self) -> StructResult<()> { - Err(StructError::SerializationNotSupported { - type_name: "optional", - } - .into()) + Err(StructError::SerializationNotSupported("optional")) } #[inline] @@ -115,10 +111,7 @@ impl ser::Serializer for &mut ArchiveSerializer { where T: ?Sized + Serialize, { - Err(StructError::SerializationNotSupported { - type_name: "optional", - } - .into()) + Err(StructError::SerializationNotSupported("optional")) } #[inline] @@ -175,7 +168,7 @@ impl ser::Serializer for &mut ArchiveSerializer { #[inline] fn serialize_seq(self, _len: Option) -> StructResult { - Err(StructError::SerializationNotSupported { type_name: "seq" }.into()) + Err(StructError::SerializationNotSupported("seq")) } #[inline] @@ -210,7 +203,7 @@ impl ser::Serializer for &mut ArchiveSerializer { #[inline] fn serialize_map(self, _len: Option) -> StructResult { - Err(StructError::SerializationNotSupported { type_name: "map" }.into()) + Err(StructError::SerializationNotSupported("map")) } #[inline] @@ -241,25 +234,25 @@ impl ser::Serializer for &mut ArchiveSerializer { impl ser::SerializeSeq for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; #[inline] fn serialize_element(&mut self, _value: &T) -> StructResult<()> where T: ?Sized + Serialize, { - Err(StructError::SerializationNotSupported { type_name: "seq" }.into()) + Err(StructError::SerializationNotSupported("seq")) } #[inline] fn end(self) -> StructResult<()> { - Err(StructError::SerializationNotSupported { type_name: "seq" }.into()) + Err(StructError::SerializationNotSupported("seq")) } } impl ser::SerializeTuple for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; #[inline] fn serialize_element(&mut self, value: &T) -> StructResult<()> @@ -277,7 +270,7 @@ impl ser::SerializeTuple for &mut ArchiveSerializer { impl ser::SerializeTupleStruct for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; #[inline] fn serialize_field(&mut self, value: &T) -> StructResult<()> @@ -295,7 +288,7 @@ impl ser::SerializeTupleStruct for &mut ArchiveSerializer { impl ser::SerializeTupleVariant for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; #[inline] fn serialize_field(&mut self, value: &T) -> StructResult<()> @@ -313,14 +306,14 @@ impl ser::SerializeTupleVariant for &mut ArchiveSerializer { impl ser::SerializeMap for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; #[inline] fn serialize_key(&mut self, _key: &T) -> StructResult<()> where T: ?Sized + Serialize, { - Err(StructError::SerializationNotSupported { type_name: "map" }.into()) + Err(StructError::SerializationNotSupported("map")) } #[inline] @@ -328,18 +321,18 @@ impl ser::SerializeMap for &mut ArchiveSerializer { where T: ?Sized + Serialize, { - Err(StructError::SerializationNotSupported { type_name: "map" }.into()) + Err(StructError::SerializationNotSupported("map")) } #[inline] fn end(self) -> StructResult<()> { - Err(StructError::SerializationNotSupported { type_name: "map" }.into()) + Err(StructError::SerializationNotSupported("map")) } } impl ser::SerializeStruct for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; #[inline] fn serialize_field(&mut self, _key: &'static str, value: &T) -> StructResult<()> @@ -357,7 +350,7 @@ impl ser::SerializeStruct for &mut ArchiveSerializer { impl ser::SerializeStructVariant for &mut ArchiveSerializer { type Ok = (); - type Error = ArchiveError; + type Error = StructError; #[inline] fn serialize_field(&mut self, _key: &'static str, value: &T) -> StructResult<()> -- cgit v1.2.3