Skip to main content

rtc/data_channel/
state.rs

1use std::fmt;
2
3use crate::peer_connection::configuration::UNSPECIFIED_STR;
4use serde::{Deserialize, Serialize};
5
6/// DataChannelState indicates the state of a data channel.
7///
8/// ## Specifications
9///
10/// * [MDN]
11/// * [W3C]
12///
13/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/readyState
14/// [W3C]: https://www.w3.org/TR/webrtc/#dom-rtcdatachannelstate
15#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
16#[non_exhaustive]
17pub enum RTCDataChannelState {
18    /// The state is unspecified.
19    #[serde(rename = "unspecified")]
20    #[default]
21    Unspecified = 0,
22
23    /// DataChannelStateConnecting indicates that the data channel is being
24    /// established. This is the initial state of DataChannel, whether created
25    /// with create_data_channel, or dispatched as a part of an DataChannelEvent.
26    #[serde(rename = "connecting")]
27    Connecting,
28
29    /// DataChannelStateOpen indicates that the underlying data transport is
30    /// established and communication is possible.
31    #[serde(rename = "open")]
32    Open,
33
34    /// DataChannelStateClosing indicates that the procedure to close down the
35    /// underlying data transport has started.
36    #[serde(rename = "closing")]
37    Closing,
38
39    /// DataChannelStateClosed indicates that the underlying data transport
40    /// has been closed or could not be established.
41    #[serde(rename = "closed")]
42    Closed,
43}
44
45const DATA_CHANNEL_STATE_CONNECTING_STR: &str = "connecting";
46const DATA_CHANNEL_STATE_OPEN_STR: &str = "open";
47const DATA_CHANNEL_STATE_CLOSING_STR: &str = "closing";
48const DATA_CHANNEL_STATE_CLOSED_STR: &str = "closed";
49
50impl From<u8> for RTCDataChannelState {
51    fn from(v: u8) -> Self {
52        match v {
53            1 => RTCDataChannelState::Connecting,
54            2 => RTCDataChannelState::Open,
55            3 => RTCDataChannelState::Closing,
56            4 => RTCDataChannelState::Closed,
57            _ => RTCDataChannelState::Unspecified,
58        }
59    }
60}
61
62impl From<&str> for RTCDataChannelState {
63    fn from(raw: &str) -> Self {
64        match raw {
65            DATA_CHANNEL_STATE_CONNECTING_STR => RTCDataChannelState::Connecting,
66            DATA_CHANNEL_STATE_OPEN_STR => RTCDataChannelState::Open,
67            DATA_CHANNEL_STATE_CLOSING_STR => RTCDataChannelState::Closing,
68            DATA_CHANNEL_STATE_CLOSED_STR => RTCDataChannelState::Closed,
69            _ => RTCDataChannelState::Unspecified,
70        }
71    }
72}
73
74impl fmt::Display for RTCDataChannelState {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        let s = match *self {
77            RTCDataChannelState::Connecting => DATA_CHANNEL_STATE_CONNECTING_STR,
78            RTCDataChannelState::Open => DATA_CHANNEL_STATE_OPEN_STR,
79            RTCDataChannelState::Closing => DATA_CHANNEL_STATE_CLOSING_STR,
80            RTCDataChannelState::Closed => DATA_CHANNEL_STATE_CLOSED_STR,
81            RTCDataChannelState::Unspecified => UNSPECIFIED_STR,
82        };
83        write!(f, "{s}")
84    }
85}
86
87#[cfg(test)]
88mod test {
89    use super::*;
90
91    #[test]
92    fn test_new_data_channel_state() {
93        let tests = vec![
94            (UNSPECIFIED_STR, RTCDataChannelState::Unspecified),
95            ("connecting", RTCDataChannelState::Connecting),
96            ("open", RTCDataChannelState::Open),
97            ("closing", RTCDataChannelState::Closing),
98            ("closed", RTCDataChannelState::Closed),
99        ];
100
101        for (state_string, expected_state) in tests {
102            assert_eq!(
103                RTCDataChannelState::from(state_string),
104                expected_state,
105                "testCase: {expected_state}",
106            );
107        }
108    }
109
110    #[test]
111    fn test_data_channel_state_string() {
112        let tests = vec![
113            (RTCDataChannelState::Unspecified, UNSPECIFIED_STR),
114            (RTCDataChannelState::Connecting, "connecting"),
115            (RTCDataChannelState::Open, "open"),
116            (RTCDataChannelState::Closing, "closing"),
117            (RTCDataChannelState::Closed, "closed"),
118        ];
119
120        for (state, expected_string) in tests {
121            assert_eq!(state.to_string(), expected_string)
122        }
123    }
124}