1 use std::fmt; 2 3 /// PeerConnectionState indicates the state of the PeerConnection. 4 #[derive(Default, Debug, Copy, Clone, PartialEq, Eq)] 5 pub enum RTCPeerConnectionState { 6 #[default] 7 Unspecified, 8 9 /// PeerConnectionStateNew indicates that any of the ICETransports or 10 /// DTLSTransports are in the "new" state and none of the transports are 11 /// in the "connecting", "checking", "failed" or "disconnected" state, or 12 /// all transports are in the "closed" state, or there are no transports. 13 New, 14 15 /// PeerConnectionStateConnecting indicates that any of the 16 /// ICETransports or DTLSTransports are in the "connecting" or 17 /// "checking" state and none of them is in the "failed" state. 18 Connecting, 19 20 /// PeerConnectionStateConnected indicates that all ICETransports and 21 /// DTLSTransports are in the "connected", "completed" or "closed" state 22 /// and at least one of them is in the "connected" or "completed" state. 23 Connected, 24 25 /// PeerConnectionStateDisconnected indicates that any of the 26 /// ICETransports or DTLSTransports are in the "disconnected" state 27 /// and none of them are in the "failed" or "connecting" or "checking" state. 28 Disconnected, 29 30 /// PeerConnectionStateFailed indicates that any of the ICETransports 31 /// or DTLSTransports are in a "failed" state. 32 Failed, 33 34 /// PeerConnectionStateClosed indicates the peer connection is closed 35 /// and the isClosed member variable of PeerConnection is true. 36 Closed, 37 } 38 39 const PEER_CONNECTION_STATE_NEW_STR: &str = "new"; 40 const PEER_CONNECTION_STATE_CONNECTING_STR: &str = "connecting"; 41 const PEER_CONNECTION_STATE_CONNECTED_STR: &str = "connected"; 42 const PEER_CONNECTION_STATE_DISCONNECTED_STR: &str = "disconnected"; 43 const PEER_CONNECTION_STATE_FAILED_STR: &str = "failed"; 44 const PEER_CONNECTION_STATE_CLOSED_STR: &str = "closed"; 45 46 impl From<&str> for RTCPeerConnectionState { from(raw: &str) -> Self47 fn from(raw: &str) -> Self { 48 match raw { 49 PEER_CONNECTION_STATE_NEW_STR => RTCPeerConnectionState::New, 50 PEER_CONNECTION_STATE_CONNECTING_STR => RTCPeerConnectionState::Connecting, 51 PEER_CONNECTION_STATE_CONNECTED_STR => RTCPeerConnectionState::Connected, 52 PEER_CONNECTION_STATE_DISCONNECTED_STR => RTCPeerConnectionState::Disconnected, 53 PEER_CONNECTION_STATE_FAILED_STR => RTCPeerConnectionState::Failed, 54 PEER_CONNECTION_STATE_CLOSED_STR => RTCPeerConnectionState::Closed, 55 _ => RTCPeerConnectionState::Unspecified, 56 } 57 } 58 } 59 60 impl From<u8> for RTCPeerConnectionState { from(v: u8) -> Self61 fn from(v: u8) -> Self { 62 match v { 63 1 => RTCPeerConnectionState::New, 64 2 => RTCPeerConnectionState::Connecting, 65 3 => RTCPeerConnectionState::Connected, 66 4 => RTCPeerConnectionState::Disconnected, 67 5 => RTCPeerConnectionState::Failed, 68 6 => RTCPeerConnectionState::Closed, 69 _ => RTCPeerConnectionState::Unspecified, 70 } 71 } 72 } 73 74 impl fmt::Display for RTCPeerConnectionState { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 76 let s = match *self { 77 RTCPeerConnectionState::New => PEER_CONNECTION_STATE_NEW_STR, 78 RTCPeerConnectionState::Connecting => PEER_CONNECTION_STATE_CONNECTING_STR, 79 RTCPeerConnectionState::Connected => PEER_CONNECTION_STATE_CONNECTED_STR, 80 RTCPeerConnectionState::Disconnected => PEER_CONNECTION_STATE_DISCONNECTED_STR, 81 RTCPeerConnectionState::Failed => PEER_CONNECTION_STATE_FAILED_STR, 82 RTCPeerConnectionState::Closed => PEER_CONNECTION_STATE_CLOSED_STR, 83 RTCPeerConnectionState::Unspecified => crate::UNSPECIFIED_STR, 84 }; 85 write!(f, "{s}") 86 } 87 } 88 89 #[derive(Default, Debug, Copy, Clone, PartialEq)] 90 pub(crate) enum NegotiationNeededState { 91 /// NegotiationNeededStateEmpty not running and queue is empty 92 #[default] 93 Empty, 94 /// NegotiationNeededStateEmpty running and queue is empty 95 Run, 96 /// NegotiationNeededStateEmpty running and queue 97 Queue, 98 } 99 100 impl From<u8> for NegotiationNeededState { from(v: u8) -> Self101 fn from(v: u8) -> Self { 102 match v { 103 1 => NegotiationNeededState::Run, 104 2 => NegotiationNeededState::Queue, 105 _ => NegotiationNeededState::Empty, 106 } 107 } 108 } 109 110 #[cfg(test)] 111 mod test { 112 use super::*; 113 114 #[test] test_new_peer_connection_state()115 fn test_new_peer_connection_state() { 116 let tests = vec![ 117 (crate::UNSPECIFIED_STR, RTCPeerConnectionState::Unspecified), 118 ("new", RTCPeerConnectionState::New), 119 ("connecting", RTCPeerConnectionState::Connecting), 120 ("connected", RTCPeerConnectionState::Connected), 121 ("disconnected", RTCPeerConnectionState::Disconnected), 122 ("failed", RTCPeerConnectionState::Failed), 123 ("closed", RTCPeerConnectionState::Closed), 124 ]; 125 126 for (state_string, expected_state) in tests { 127 assert_eq!( 128 RTCPeerConnectionState::from(state_string), 129 expected_state, 130 "testCase: {expected_state}", 131 ); 132 } 133 } 134 135 #[test] test_peer_connection_state_string()136 fn test_peer_connection_state_string() { 137 let tests = vec![ 138 (RTCPeerConnectionState::Unspecified, crate::UNSPECIFIED_STR), 139 (RTCPeerConnectionState::New, "new"), 140 (RTCPeerConnectionState::Connecting, "connecting"), 141 (RTCPeerConnectionState::Connected, "connected"), 142 (RTCPeerConnectionState::Disconnected, "disconnected"), 143 (RTCPeerConnectionState::Failed, "failed"), 144 (RTCPeerConnectionState::Closed, "closed"), 145 ]; 146 147 for (state, expected_string) in tests { 148 assert_eq!(state.to_string(), expected_string) 149 } 150 } 151 } 152