webrtc/peer_connection/policy/
ice_transport_policy.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5/// ICETransportPolicy defines the ICE candidate policy surface the
6/// permitted candidates. Only these candidates are used for connectivity checks.
7///
8/// ## Specifications
9///
10/// * [W3C]
11///
12/// [W3C]: https://w3c.github.io/webrtc-pc/#rtcicetransportpolicy-enum
13#[derive(Default, Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)]
14pub enum RTCIceTransportPolicy {
15    #[default]
16    Unspecified = 0,
17
18    /// ICETransportPolicyAll indicates any type of candidate is used.
19    #[serde(rename = "all")]
20    All = 1,
21
22    /// ICETransportPolicyRelay indicates only media relay candidates such
23    /// as candidates passing through a TURN server are used.
24    #[serde(rename = "relay")]
25    Relay = 2,
26}
27
28/// ICEGatherPolicy is the ORTC equivalent of ICETransportPolicy
29pub type ICEGatherPolicy = RTCIceTransportPolicy;
30
31const ICE_TRANSPORT_POLICY_RELAY_STR: &str = "relay";
32const ICE_TRANSPORT_POLICY_ALL_STR: &str = "all";
33
34/// takes a string and converts it to ICETransportPolicy
35impl From<&str> for RTCIceTransportPolicy {
36    fn from(raw: &str) -> Self {
37        match raw {
38            ICE_TRANSPORT_POLICY_RELAY_STR => RTCIceTransportPolicy::Relay,
39            ICE_TRANSPORT_POLICY_ALL_STR => RTCIceTransportPolicy::All,
40            _ => RTCIceTransportPolicy::Unspecified,
41        }
42    }
43}
44
45impl fmt::Display for RTCIceTransportPolicy {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        let s = match *self {
48            RTCIceTransportPolicy::Relay => ICE_TRANSPORT_POLICY_RELAY_STR,
49            RTCIceTransportPolicy::All => ICE_TRANSPORT_POLICY_ALL_STR,
50            RTCIceTransportPolicy::Unspecified => crate::UNSPECIFIED_STR,
51        };
52        write!(f, "{s}")
53    }
54}
55
56#[cfg(test)]
57mod test {
58    use super::*;
59
60    #[test]
61    fn test_new_ice_transport_policy() {
62        let tests = vec![
63            ("relay", RTCIceTransportPolicy::Relay),
64            ("all", RTCIceTransportPolicy::All),
65        ];
66
67        for (policy_string, expected_policy) in tests {
68            assert_eq!(RTCIceTransportPolicy::from(policy_string), expected_policy);
69        }
70    }
71
72    #[test]
73    fn test_ice_transport_policy_string() {
74        let tests = vec![
75            (RTCIceTransportPolicy::Relay, "relay"),
76            (RTCIceTransportPolicy::All, "all"),
77        ];
78
79        for (policy, expected_string) in tests {
80            assert_eq!(policy.to_string(), expected_string);
81        }
82    }
83}