1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::fmt;

use serde::{Deserialize, Serialize};

/// ICECredentialType indicates the type of credentials used to connect to
/// an ICE server.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum RTCIceCredentialType {
    #[default]
    Unspecified,

    /// ICECredential::Password describes username and password based
    /// credentials as described in <https://tools.ietf.org/html/rfc5389>.
    Password,

    /// ICECredential::Oauth describes token based credential as described
    /// in <https://tools.ietf.org/html/rfc7635>.
    /// Not supported in WebRTC 1.0 spec
    Oauth,
}

const ICE_CREDENTIAL_TYPE_PASSWORD_STR: &str = "password";
const ICE_CREDENTIAL_TYPE_OAUTH_STR: &str = "oauth";

impl From<&str> for RTCIceCredentialType {
    fn from(raw: &str) -> Self {
        match raw {
            ICE_CREDENTIAL_TYPE_PASSWORD_STR => RTCIceCredentialType::Password,
            ICE_CREDENTIAL_TYPE_OAUTH_STR => RTCIceCredentialType::Oauth,
            _ => RTCIceCredentialType::Unspecified,
        }
    }
}

impl fmt::Display for RTCIceCredentialType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            RTCIceCredentialType::Password => write!(f, "{ICE_CREDENTIAL_TYPE_PASSWORD_STR}"),
            RTCIceCredentialType::Oauth => write!(f, "{ICE_CREDENTIAL_TYPE_OAUTH_STR}"),
            _ => write!(f, "{}", crate::UNSPECIFIED_STR),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_new_ice_credential_type() {
        let tests = vec![
            ("Unspecified", RTCIceCredentialType::Unspecified),
            ("password", RTCIceCredentialType::Password),
            ("oauth", RTCIceCredentialType::Oauth),
        ];

        for (ct_str, expected_ct) in tests {
            assert_eq!(RTCIceCredentialType::from(ct_str), expected_ct);
        }
    }

    #[test]
    fn test_ice_credential_type_string() {
        let tests = vec![
            (RTCIceCredentialType::Unspecified, "Unspecified"),
            (RTCIceCredentialType::Password, "password"),
            (RTCIceCredentialType::Oauth, "oauth"),
        ];

        for (ct, expected_string) in tests {
            assert_eq!(ct.to_string(), expected_string);
        }
    }
}