use std::fmt;
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub enum RTCIceConnectionState {
#[default]
Unspecified,
New,
Checking,
Connected,
Completed,
Disconnected,
Failed,
Closed,
}
const ICE_CONNECTION_STATE_NEW_STR: &str = "new";
const ICE_CONNECTION_STATE_CHECKING_STR: &str = "checking";
const ICE_CONNECTION_STATE_CONNECTED_STR: &str = "connected";
const ICE_CONNECTION_STATE_COMPLETED_STR: &str = "completed";
const ICE_CONNECTION_STATE_DISCONNECTED_STR: &str = "disconnected";
const ICE_CONNECTION_STATE_FAILED_STR: &str = "failed";
const ICE_CONNECTION_STATE_CLOSED_STR: &str = "closed";
impl From<&str> for RTCIceConnectionState {
fn from(raw: &str) -> Self {
match raw {
ICE_CONNECTION_STATE_NEW_STR => RTCIceConnectionState::New,
ICE_CONNECTION_STATE_CHECKING_STR => RTCIceConnectionState::Checking,
ICE_CONNECTION_STATE_CONNECTED_STR => RTCIceConnectionState::Connected,
ICE_CONNECTION_STATE_COMPLETED_STR => RTCIceConnectionState::Completed,
ICE_CONNECTION_STATE_DISCONNECTED_STR => RTCIceConnectionState::Disconnected,
ICE_CONNECTION_STATE_FAILED_STR => RTCIceConnectionState::Failed,
ICE_CONNECTION_STATE_CLOSED_STR => RTCIceConnectionState::Closed,
_ => RTCIceConnectionState::Unspecified,
}
}
}
impl From<u8> for RTCIceConnectionState {
fn from(v: u8) -> Self {
match v {
1 => RTCIceConnectionState::New,
2 => RTCIceConnectionState::Checking,
3 => RTCIceConnectionState::Connected,
4 => RTCIceConnectionState::Completed,
5 => RTCIceConnectionState::Disconnected,
6 => RTCIceConnectionState::Failed,
7 => RTCIceConnectionState::Closed,
_ => RTCIceConnectionState::Unspecified,
}
}
}
impl fmt::Display for RTCIceConnectionState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match *self {
RTCIceConnectionState::New => ICE_CONNECTION_STATE_NEW_STR,
RTCIceConnectionState::Checking => ICE_CONNECTION_STATE_CHECKING_STR,
RTCIceConnectionState::Connected => ICE_CONNECTION_STATE_CONNECTED_STR,
RTCIceConnectionState::Completed => ICE_CONNECTION_STATE_COMPLETED_STR,
RTCIceConnectionState::Disconnected => ICE_CONNECTION_STATE_DISCONNECTED_STR,
RTCIceConnectionState::Failed => ICE_CONNECTION_STATE_FAILED_STR,
RTCIceConnectionState::Closed => ICE_CONNECTION_STATE_CLOSED_STR,
RTCIceConnectionState::Unspecified => crate::UNSPECIFIED_STR,
};
write!(f, "{s}")
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_new_ice_connection_state() {
let tests = vec![
(crate::UNSPECIFIED_STR, RTCIceConnectionState::Unspecified),
("new", RTCIceConnectionState::New),
("checking", RTCIceConnectionState::Checking),
("connected", RTCIceConnectionState::Connected),
("completed", RTCIceConnectionState::Completed),
("disconnected", RTCIceConnectionState::Disconnected),
("failed", RTCIceConnectionState::Failed),
("closed", RTCIceConnectionState::Closed),
];
for (state_string, expected_state) in tests {
assert_eq!(
RTCIceConnectionState::from(state_string),
expected_state,
"testCase: {expected_state}",
);
}
}
#[test]
fn test_ice_connection_state_string() {
let tests = vec![
(RTCIceConnectionState::Unspecified, crate::UNSPECIFIED_STR),
(RTCIceConnectionState::New, "new"),
(RTCIceConnectionState::Checking, "checking"),
(RTCIceConnectionState::Connected, "connected"),
(RTCIceConnectionState::Completed, "completed"),
(RTCIceConnectionState::Disconnected, "disconnected"),
(RTCIceConnectionState::Failed, "failed"),
(RTCIceConnectionState::Closed, "closed"),
];
for (state, expected_string) in tests {
assert_eq!(state.to_string(), expected_string)
}
}
}