Skip to main content

o_sfu_protocol/signaling/
codec.rs

1use super::{
2    ClientMessage, ClientRequest, ClientResponse, Envelope, EnvelopeDecodeError, RequestId,
3    ServerMessage, ServerRequest, ServerResponse, envelope::EnvelopeRoute,
4};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum ClientEnvelope {
8    Message(ClientMessage),
9    Request {
10        request_id: RequestId,
11        request: ClientRequest,
12    },
13    Response {
14        response_to: RequestId,
15        response: ClientResponse,
16    },
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum ServerEnvelope {
21    Message(ServerMessage),
22    Request {
23        request_id: RequestId,
24        request: ServerRequest,
25    },
26    Response {
27        response_to: RequestId,
28        response: ServerResponse,
29    },
30}
31
32impl ClientEnvelope {
33    /// encode one typed client-side envelope into the websocket wire shape
34    ///
35    /// # Errors
36    ///
37    /// Returns [`serde_json::Error`] when the typed payload cannot be serialized
38    /// into the JSON envelope payload.
39    pub fn into_envelope(self) -> Result<Envelope, serde_json::Error> {
40        match self {
41            Self::Message(message) => message.into_envelope(),
42            Self::Request {
43                request_id,
44                request,
45            } => request.into_envelope(request_id),
46            Self::Response {
47                response_to,
48                response,
49            } => response.into_envelope(response_to),
50        }
51    }
52
53    /// decode a raw websocket envelope into the typed native client contract
54    ///
55    /// # Errors
56    ///
57    /// Returns [`EnvelopeDecodeError::UnknownTag`] when the tag is unknown for
58    /// its route, [`EnvelopeDecodeError::InvalidPayload`] when a required payload
59    /// is missing or malformed and [`EnvelopeDecodeError::UnexpectedPayload`]
60    /// when a payload is supplied for `stoprecording`.
61    pub fn decode(envelope: Envelope) -> Result<Self, EnvelopeDecodeError> {
62        let (tag, payload, route) = envelope.into_parts();
63        match route {
64            EnvelopeRoute::Message => {
65                ClientMessage::decode(&tag, payload).map(ClientEnvelope::Message)
66            }
67            EnvelopeRoute::Request(request_id) => {
68                ClientRequest::decode(&tag, payload).map(|request| Self::Request {
69                    request_id,
70                    request,
71                })
72            }
73            EnvelopeRoute::Response(response_to) => {
74                ClientResponse::decode(&tag, payload).map(|response| Self::Response {
75                    response_to,
76                    response,
77                })
78            }
79        }
80    }
81}
82
83impl ServerEnvelope {
84    /// encode one typed server-side envelope into the websocket wire shape
85    ///
86    /// # Errors
87    ///
88    /// Returns [`serde_json::Error`] when the typed payload cannot be serialized
89    /// into the JSON envelope payload.
90    pub fn into_envelope(self) -> Result<Envelope, serde_json::Error> {
91        match self {
92            Self::Message(message) => message.into_envelope(),
93            Self::Request {
94                request_id,
95                request,
96            } => request.into_envelope(request_id),
97            Self::Response {
98                response_to,
99                response,
100            } => response.into_envelope(response_to),
101        }
102    }
103
104    /// decode a raw websocket envelope into the typed native server contract
105    ///
106    /// # Errors
107    ///
108    /// Returns [`EnvelopeDecodeError::UnknownTag`] when the tag is unknown for
109    /// its route or [`EnvelopeDecodeError::InvalidPayload`] when a required
110    /// payload is missing or malformed.
111    pub fn decode(envelope: Envelope) -> Result<Self, EnvelopeDecodeError> {
112        let (tag, payload, route) = envelope.into_parts();
113        match route {
114            EnvelopeRoute::Message => {
115                ServerMessage::decode(&tag, payload).map(ServerEnvelope::Message)
116            }
117            EnvelopeRoute::Request(request_id) => {
118                ServerRequest::decode(&tag, payload).map(|request| Self::Request {
119                    request_id,
120                    request,
121                })
122            }
123            EnvelopeRoute::Response(response_to) => {
124                ServerResponse::decode(&tag, payload).map(|response| Self::Response {
125                    response_to,
126                    response,
127                })
128            }
129        }
130    }
131}