webrtc/ice_transport/
ice_role.rs

1use std::fmt;
2
3/// ICERole describes the role ice.Agent is playing in selecting the
4/// preferred the candidate pair.
5///
6/// ## Specifications
7///
8/// * [MDN]
9/// * [W3C]
10///
11/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/API/RTCIceTransport/role
12/// [W3C]: https://w3c.github.io/webrtc-pc/#dom-rtcicerole
13#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
14pub enum RTCIceRole {
15    #[default]
16    Unspecified,
17
18    /// ICERoleControlling indicates that the ICE agent that is responsible
19    /// for selecting the final choice of candidate pairs and signaling them
20    /// through STUN and an updated offer, if needed. In any session, one agent
21    /// is always controlling. The other is the controlled agent.
22    Controlling,
23
24    /// ICERoleControlled indicates that an ICE agent that waits for the
25    /// controlling agent to select the final choice of candidate pairs.
26    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}