use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RTCIceProtocol {
#[default]
Unspecified,
#[serde(rename = "udp")]
Udp,
#[serde(rename = "tcp")]
Tcp,
}
const ICE_PROTOCOL_UDP_STR: &str = "udp";
const ICE_PROTOCOL_TCP_STR: &str = "tcp";
impl From<&str> for RTCIceProtocol {
fn from(raw: &str) -> Self {
if raw.to_uppercase() == ICE_PROTOCOL_UDP_STR.to_uppercase() {
RTCIceProtocol::Udp
} else if raw.to_uppercase() == ICE_PROTOCOL_TCP_STR.to_uppercase() {
RTCIceProtocol::Tcp
} else {
RTCIceProtocol::Unspecified
}
}
}
impl fmt::Display for RTCIceProtocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
RTCIceProtocol::Udp => write!(f, "{ICE_PROTOCOL_UDP_STR}"),
RTCIceProtocol::Tcp => write!(f, "{ICE_PROTOCOL_TCP_STR}"),
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_new_ice_protocol() {
let tests = vec![
("Unspecified", RTCIceProtocol::Unspecified),
("udp", RTCIceProtocol::Udp),
("tcp", RTCIceProtocol::Tcp),
("UDP", RTCIceProtocol::Udp),
("TCP", RTCIceProtocol::Tcp),
];
for (proto_string, expected_proto) in tests {
let actual = RTCIceProtocol::from(proto_string);
assert_eq!(actual, expected_proto);
}
}
#[test]
fn test_ice_protocol_string() {
let tests = vec![
(RTCIceProtocol::Unspecified, "Unspecified"),
(RTCIceProtocol::Udp, "udp"),
(RTCIceProtocol::Tcp, "tcp"),
];
for (proto, expected_string) in tests {
assert_eq!(proto.to_string(), expected_string);
}
}
}