Add validation for serialization format

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent a6c8ad8795
commit 3cb1aa0496
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -15,10 +15,23 @@ pub trait HydrusSerializable: DeserializeOwned {
fn type_id() -> u64;
}
pub trait ConstNumberTrait {
fn value() -> u64;
}
impl<T> ConstNumberTrait for T
where
T: HydrusSerializable,
{
fn value() -> u64 {
T::type_id()
}
}
#[derive(Clone, Debug)]
pub struct SerializableId<T: HydrusSerializable>(u64, PhantomData<T>);
pub struct SerializableId<T: ConstNumberTrait>(u64, PhantomData<T>);
impl<'de, T: HydrusSerializable> Deserialize<'de> for SerializableId<T> {
impl<'de, T: ConstNumberTrait> Deserialize<'de> for SerializableId<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
@ -29,11 +42,11 @@ impl<'de, T: HydrusSerializable> Deserialize<'de> for SerializableId<T> {
struct SerIdVisitor<T>(PhantomData<T>);
impl<'de, T: HydrusSerializable> Visitor<'de> for SerIdVisitor<T> {
impl<'de, T: ConstNumberTrait> Visitor<'de> for SerIdVisitor<T> {
type Value = SerializableId<T>;
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
write!(formatter, "an unsigned integer equal to {}", T::type_id())
write!(formatter, "an unsigned integer equal to {}", T::value())
}
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
@ -61,9 +74,9 @@ impl<'de, T: HydrusSerializable> Visitor<'de> for SerIdVisitor<T> {
where
E: Error,
{
let expected_value = T::type_id();
let expected_value = T::value();
if v != expected_value {
Err(E::custom(format!("type not equal to {}", expected_value)))
Err(E::custom(format!("value not equal to {}", expected_value)))
} else {
Ok(SerializableId(expected_value, PhantomData))
}

@ -1,10 +1,21 @@
use crate::hydrus_serializable::{HydrusSerializable, SerializableId};
use crate::hydrus_serializable::{ConstNumberTrait, HydrusSerializable, SerializableId};
use serde::Deserialize;
#[derive(Clone, Debug, Deserialize)]
pub struct VersionOne;
impl ConstNumberTrait for VersionOne {
fn value() -> u64 {
1
}
}
#[derive(Clone, Debug, Deserialize)]
#[serde(bound = "")]
pub struct HydrusSerWrapper<T: HydrusSerializable> {
#[allow(unused)]
pub type_id: SerializableId<T>,
pub version: u8,
#[allow(unused)]
pub version: SerializableId<VersionOne>,
pub inner: T,
}

Loading…
Cancel
Save