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