use super::{BinarySerializer, Encoding, Error, Result, StructuredSerializer};
use crate::event::{EventBinarySerializer, EventStructuredSerializer};
use crate::Event;
pub trait StructuredDeserializer
where
Self: Sized,
{
fn deserialize_structured<R: Sized, V: StructuredSerializer<R>>(
self,
serializer: V,
) -> Result<R>;
fn into_event(self) -> Result<Event> {
self.deserialize_structured(EventStructuredSerializer {})
}
}
pub trait BinaryDeserializer
where
Self: Sized,
{
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(self, serializer: V) -> Result<R>;
fn into_event(self) -> Result<Event> {
self.deserialize_binary(EventBinarySerializer::new())
}
}
pub trait MessageDeserializer
where
Self: StructuredDeserializer + BinaryDeserializer + Sized,
{
fn encoding(&self) -> Encoding;
fn into_event(self) -> Result<Event> {
match self.encoding() {
Encoding::BINARY => BinaryDeserializer::into_event(self),
Encoding::STRUCTURED => StructuredDeserializer::into_event(self),
_ => Err(Error::WrongEncoding {}),
}
}
fn deserialize_to_binary<R: Sized, T: BinarySerializer<R>>(self, serializer: T) -> Result<R> {
if self.encoding() == Encoding::BINARY {
return self.deserialize_binary(serializer);
}
MessageDeserializer::into_event(self)?.deserialize_binary(serializer)
}
fn deserialize_to_structured<R: Sized, T: StructuredSerializer<R>>(
self,
serializer: T,
) -> Result<R> {
if self.encoding() == Encoding::STRUCTURED {
return self.deserialize_structured(serializer);
}
MessageDeserializer::into_event(self)?.deserialize_structured(serializer)
}
fn deserialize_to<R: Sized, T: BinarySerializer<R> + StructuredSerializer<R>>(
self,
serializer: T,
) -> Result<R> {
if self.encoding() == Encoding::STRUCTURED {
self.deserialize_structured(serializer)
} else {
self.deserialize_binary(serializer)
}
}
}