Skip to main content

rtc_ice/state/
mod.rs

1#[cfg(test)]
2mod state_test;
3
4use std::fmt;
5
6/// An enum showing the state of a ICE Connection List of supported States.
7#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum ConnectionState {
10    /// No state was set.
11    #[default]
12    Unspecified,
13
14    /// ICE agent is gathering addresses.
15    New,
16
17    /// ICE agent has been given local and remote candidates, and is attempting to find a match.
18    Checking,
19
20    /// ICE agent has a pairing, but is still checking other pairs.
21    Connected,
22
23    /// ICE agent has finished.
24    Completed,
25
26    /// ICE agent never could successfully connect.
27    Failed,
28
29    /// ICE agent connected successfully, but has entered a failed state.
30    Disconnected,
31
32    /// ICE agent has finished and is no longer handling requests.
33    Closed,
34}
35
36impl fmt::Display for ConnectionState {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        let s = match *self {
39            Self::Unspecified => "Unspecified",
40            Self::New => "New",
41            Self::Checking => "Checking",
42            Self::Connected => "Connected",
43            Self::Completed => "Completed",
44            Self::Failed => "Failed",
45            Self::Disconnected => "Disconnected",
46            Self::Closed => "Closed",
47        };
48        write!(f, "{s}")
49    }
50}
51
52impl From<u8> for ConnectionState {
53    fn from(v: u8) -> Self {
54        match v {
55            1 => Self::New,
56            2 => Self::Checking,
57            3 => Self::Connected,
58            4 => Self::Completed,
59            5 => Self::Failed,
60            6 => Self::Disconnected,
61            7 => Self::Closed,
62            _ => Self::Unspecified,
63        }
64    }
65}
66
67/// Describes the state of the candidate gathering process.
68#[derive(Default, PartialEq, Eq, Copy, Clone)]
69#[non_exhaustive]
70pub enum GatheringState {
71    /// No state was set.
72    #[default]
73    Unspecified,
74
75    /// Indicates candidate gathering is not yet started.
76    New,
77
78    /// Indicates candidate gathering is ongoing.
79    Gathering,
80
81    /// Indicates candidate gathering has been completed.
82    Complete,
83}
84
85impl From<u8> for GatheringState {
86    fn from(v: u8) -> Self {
87        match v {
88            1 => Self::New,
89            2 => Self::Gathering,
90            3 => Self::Complete,
91            _ => Self::Unspecified,
92        }
93    }
94}
95
96impl fmt::Display for GatheringState {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        let s = match *self {
99            Self::New => "new",
100            Self::Gathering => "gathering",
101            Self::Complete => "complete",
102            Self::Unspecified => "unspecified",
103        };
104        write!(f, "{s}")
105    }
106}