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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::fmt;

use ice::state::ConnectionState;

/// ICETransportState represents the current state of the ICE transport.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub enum RTCIceTransportState {
    #[default]
    Unspecified,

    /// ICETransportStateNew indicates the ICETransport is waiting
    /// for remote candidates to be supplied.
    New,

    /// ICETransportStateChecking indicates the ICETransport has
    /// received at least one remote candidate, and a local and remote
    /// ICECandidateComplete dictionary was not added as the last candidate.
    Checking,

    /// ICETransportStateConnected indicates the ICETransport has
    /// received a response to an outgoing connectivity check, or has
    /// received incoming DTLS/media after a successful response to an
    /// incoming connectivity check, but is still checking other candidate
    /// pairs to see if there is a better connection.
    Connected,

    /// ICETransportStateCompleted indicates the ICETransport tested
    /// all appropriate candidate pairs and at least one functioning
    /// candidate pair has been found.
    Completed,

    /// ICETransportStateFailed indicates the ICETransport the last
    /// candidate was added and all appropriate candidate pairs have either
    /// failed connectivity checks or have lost consent.
    Failed,

    /// ICETransportStateDisconnected indicates the ICETransport has received
    /// at least one local and remote candidate, but the final candidate was
    /// received yet and all appropriate candidate pairs thus far have been
    /// tested and failed.
    Disconnected,

    /// ICETransportStateClosed indicates the ICETransport has shut down
    /// and is no longer responding to STUN requests.
    Closed,
}

const ICE_TRANSPORT_STATE_NEW_STR: &str = "new";
const ICE_TRANSPORT_STATE_CHECKING_STR: &str = "checking";
const ICE_TRANSPORT_STATE_CONNECTED_STR: &str = "connected";
const ICE_TRANSPORT_STATE_COMPLETED_STR: &str = "completed";
const ICE_TRANSPORT_STATE_FAILED_STR: &str = "failed";
const ICE_TRANSPORT_STATE_DISCONNECTED_STR: &str = "disconnected";
const ICE_TRANSPORT_STATE_CLOSED_STR: &str = "closed";

impl From<&str> for RTCIceTransportState {
    fn from(raw: &str) -> Self {
        match raw {
            ICE_TRANSPORT_STATE_NEW_STR => RTCIceTransportState::New,
            ICE_TRANSPORT_STATE_CHECKING_STR => RTCIceTransportState::Checking,
            ICE_TRANSPORT_STATE_CONNECTED_STR => RTCIceTransportState::Connected,
            ICE_TRANSPORT_STATE_COMPLETED_STR => RTCIceTransportState::Completed,
            ICE_TRANSPORT_STATE_FAILED_STR => RTCIceTransportState::Failed,
            ICE_TRANSPORT_STATE_DISCONNECTED_STR => RTCIceTransportState::Disconnected,
            ICE_TRANSPORT_STATE_CLOSED_STR => RTCIceTransportState::Closed,
            _ => RTCIceTransportState::Unspecified,
        }
    }
}

impl From<u8> for RTCIceTransportState {
    fn from(v: u8) -> Self {
        match v {
            1 => Self::New,
            2 => Self::Checking,
            3 => Self::Connected,
            4 => Self::Completed,
            5 => Self::Failed,
            6 => Self::Disconnected,
            7 => Self::Closed,
            _ => Self::Unspecified,
        }
    }
}

impl fmt::Display for RTCIceTransportState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            RTCIceTransportState::New => write!(f, "{ICE_TRANSPORT_STATE_NEW_STR}"),
            RTCIceTransportState::Checking => write!(f, "{ICE_TRANSPORT_STATE_CHECKING_STR}"),
            RTCIceTransportState::Connected => {
                write!(f, "{ICE_TRANSPORT_STATE_CONNECTED_STR}")
            }
            RTCIceTransportState::Completed => write!(f, "{ICE_TRANSPORT_STATE_COMPLETED_STR}"),
            RTCIceTransportState::Failed => {
                write!(f, "{ICE_TRANSPORT_STATE_FAILED_STR}")
            }
            RTCIceTransportState::Disconnected => {
                write!(f, "{ICE_TRANSPORT_STATE_DISCONNECTED_STR}")
            }
            RTCIceTransportState::Closed => {
                write!(f, "{ICE_TRANSPORT_STATE_CLOSED_STR}")
            }
            _ => write!(f, "{}", crate::UNSPECIFIED_STR),
        }
    }
}

impl From<ConnectionState> for RTCIceTransportState {
    fn from(raw: ConnectionState) -> Self {
        match raw {
            ConnectionState::New => RTCIceTransportState::New,
            ConnectionState::Checking => RTCIceTransportState::Checking,
            ConnectionState::Connected => RTCIceTransportState::Connected,
            ConnectionState::Completed => RTCIceTransportState::Completed,
            ConnectionState::Failed => RTCIceTransportState::Failed,
            ConnectionState::Disconnected => RTCIceTransportState::Disconnected,
            ConnectionState::Closed => RTCIceTransportState::Closed,
            _ => RTCIceTransportState::Unspecified,
        }
    }
}

impl RTCIceTransportState {
    pub(crate) fn to_ice(self) -> ConnectionState {
        match self {
            RTCIceTransportState::New => ConnectionState::New,
            RTCIceTransportState::Checking => ConnectionState::Checking,
            RTCIceTransportState::Connected => ConnectionState::Connected,
            RTCIceTransportState::Completed => ConnectionState::Completed,
            RTCIceTransportState::Failed => ConnectionState::Failed,
            RTCIceTransportState::Disconnected => ConnectionState::Disconnected,
            RTCIceTransportState::Closed => ConnectionState::Closed,
            _ => ConnectionState::Unspecified,
        }
    }
}

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

    #[test]
    fn test_ice_transport_state_string() {
        let tests = vec![
            (RTCIceTransportState::Unspecified, "Unspecified"),
            (RTCIceTransportState::New, "new"),
            (RTCIceTransportState::Checking, "checking"),
            (RTCIceTransportState::Connected, "connected"),
            (RTCIceTransportState::Completed, "completed"),
            (RTCIceTransportState::Failed, "failed"),
            (RTCIceTransportState::Disconnected, "disconnected"),
            (RTCIceTransportState::Closed, "closed"),
        ];

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

    #[test]
    fn test_ice_transport_state_convert() {
        let tests = vec![
            (
                RTCIceTransportState::Unspecified,
                ConnectionState::Unspecified,
            ),
            (RTCIceTransportState::New, ConnectionState::New),
            (RTCIceTransportState::Checking, ConnectionState::Checking),
            (RTCIceTransportState::Connected, ConnectionState::Connected),
            (RTCIceTransportState::Completed, ConnectionState::Completed),
            (RTCIceTransportState::Failed, ConnectionState::Failed),
            (
                RTCIceTransportState::Disconnected,
                ConnectionState::Disconnected,
            ),
            (RTCIceTransportState::Closed, ConnectionState::Closed),
        ];

        for (native, ice_state) in tests {
            assert_eq!(native.to_ice(), ice_state);
            assert_eq!(native, RTCIceTransportState::from(ice_state));
        }
    }
}