webrtc/ice_transport/
ice_role.rs1use std::fmt;
2
3#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
14pub enum RTCIceRole {
15 #[default]
16 Unspecified,
17
18 Controlling,
23
24 Controlled,
27}
28
29const ICE_ROLE_CONTROLLING_STR: &str = "controlling";
30const ICE_ROLE_CONTROLLED_STR: &str = "controlled";
31
32impl From<&str> for RTCIceRole {
33 fn from(raw: &str) -> Self {
34 match raw {
35 ICE_ROLE_CONTROLLING_STR => RTCIceRole::Controlling,
36 ICE_ROLE_CONTROLLED_STR => RTCIceRole::Controlled,
37 _ => RTCIceRole::Unspecified,
38 }
39 }
40}
41
42impl fmt::Display for RTCIceRole {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 match *self {
45 RTCIceRole::Controlling => write!(f, "{ICE_ROLE_CONTROLLING_STR}"),
46 RTCIceRole::Controlled => write!(f, "{ICE_ROLE_CONTROLLED_STR}"),
47 _ => write!(f, "{}", crate::UNSPECIFIED_STR),
48 }
49 }
50}
51
52#[cfg(test)]
53mod test {
54 use super::*;
55
56 #[test]
57 fn test_new_ice_role() {
58 let tests = vec![
59 ("Unspecified", RTCIceRole::Unspecified),
60 ("controlling", RTCIceRole::Controlling),
61 ("controlled", RTCIceRole::Controlled),
62 ];
63
64 for (role_string, expected_role) in tests {
65 assert_eq!(RTCIceRole::from(role_string), expected_role);
66 }
67 }
68
69 #[test]
70 fn test_ice_role_string() {
71 let tests = vec![
72 (RTCIceRole::Unspecified, "Unspecified"),
73 (RTCIceRole::Controlling, "controlling"),
74 (RTCIceRole::Controlled, "controlled"),
75 ];
76
77 for (proto, expected_string) in tests {
78 assert_eq!(proto.to_string(), expected_string);
79 }
80 }
81}