1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::{BinarySerializer, Encoding, Error, Result, StructuredSerializer};
use crate::event::{EventBinarySerializer, EventStructuredSerializer};
use crate::Event;

/// Deserializer trait for a Message that can be encoded as structured mode.
pub trait StructuredDeserializer
where
    Self: Sized,
{
    /// Deserialize the message to [`StructuredSerializer`].
    fn deserialize_structured<R: Sized, V: StructuredSerializer<R>>(
        self,
        serializer: V,
    ) -> Result<R>;

    /// Convert this Message to [`Event`].
    fn into_event(self) -> Result<Event> {
        self.deserialize_structured(EventStructuredSerializer {})
    }
}

/// Deserializer trait for a Message that can be encoded as binary mode.
pub trait BinaryDeserializer
where
    Self: Sized,
{
    /// Deserialize the message to [`BinarySerializer`].
    fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(self, serializer: V) -> Result<R>;

    /// Convert this Message to [`Event`].
    fn into_event(self) -> Result<Event> {
        self.deserialize_binary(EventBinarySerializer::new())
    }
}

/// Deserializer trait for a Message that can be encoded both in structured mode or binary mode.
pub trait MessageDeserializer
where
    Self: StructuredDeserializer + BinaryDeserializer + Sized,
{
    /// Get this message [`Encoding`].
    fn encoding(&self) -> Encoding;

    /// Convert this Message to [`Event`].
    fn into_event(self) -> Result<Event> {
        match self.encoding() {
            Encoding::BINARY => BinaryDeserializer::into_event(self),
            Encoding::STRUCTURED => StructuredDeserializer::into_event(self),
            _ => Err(Error::WrongEncoding {}),
        }
    }

    /// Deserialize the message to [`BinarySerializer`].
    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)
    }

    /// Deserialize the message to [`StructuredSerializer`].
    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)
    }

    /// Deserialize the message to a serializer, depending on the message encoding.
    /// You can use this method to transcode this message directly to another serializer, without going through [`Event`].
    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)
        }
    }
}