use std::{marker::PhantomData, ops::Deref};
use bytes::Bytes;
use crate::{
dds::adapters::*, messages::submessages::submessages::RepresentationIdentifier, Keyed,
};
pub(crate) struct NoKeyWrapper<D> {
pub(crate) d: D,
}
impl<D> From<D> for NoKeyWrapper<D> {
fn from(d: D) -> Self {
Self { d }
}
}
impl<D> Deref for NoKeyWrapper<D> {
type Target = D;
fn deref(&self) -> &Self::Target {
&self.d
}
}
impl<D> Keyed for NoKeyWrapper<D> {
type K = ();
fn key(&self) {}
}
pub struct SAWrapper<SA> {
no_key: PhantomData<SA>,
}
impl<D, SA> no_key::SerializerAdapter<NoKeyWrapper<D>> for SAWrapper<SA>
where
SA: no_key::SerializerAdapter<D>,
{
type Error = SA::Error;
fn output_encoding() -> RepresentationIdentifier {
SA::output_encoding()
}
fn to_bytes(value: &NoKeyWrapper<D>) -> Result<Bytes, SA::Error> {
SA::to_bytes(&value.d)
}
}
impl<D, SA> with_key::SerializerAdapter<NoKeyWrapper<D>> for SAWrapper<SA>
where
SA: no_key::SerializerAdapter<D>,
{
fn key_to_bytes(_value: &()) -> Result<Bytes, SA::Error> {
Ok(Bytes::new())
}
}
pub struct DAWrapper<DA> {
no_key: PhantomData<DA>,
}
impl<D, DA> no_key::DeserializerAdapter<NoKeyWrapper<D>> for DAWrapper<DA>
where
DA: no_key::DeserializerAdapter<D>,
{
type Error = DA::Error;
fn supported_encodings() -> &'static [RepresentationIdentifier] {
DA::supported_encodings()
}
fn from_bytes(
input_bytes: &[u8],
encoding: RepresentationIdentifier,
) -> Result<NoKeyWrapper<D>, DA::Error> {
DA::from_bytes(input_bytes, encoding).map(|d| NoKeyWrapper::<D> { d })
}
}
impl<D, DA> with_key::DeserializerAdapter<NoKeyWrapper<D>> for DAWrapper<DA>
where
DA: no_key::DeserializerAdapter<D>,
{
fn key_from_bytes(
_input_bytes: &[u8],
_encoding: RepresentationIdentifier,
) -> Result<<NoKeyWrapper<D> as Keyed>::K, DA::Error> {
Ok(())
}
}