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