Skip to main content

rtc_ice/tcp_type/
mod.rs

1#[cfg(test)]
2mod tcp_type_test;
3
4use std::fmt;
5
6// TCPType is the type of ICE TCP candidate as described in
7// https://tools.ietf.org/html/rfc6544#section-4.5
8#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
9/// The role of an ICE-TCP candidate, per [RFC 6544] ยง4.5.
10///
11/// [RFC 6544]: https://datatracker.ietf.org/doc/html/rfc6544#section-4.5
12pub enum TcpType {
13    /// The default value. For example UDP candidates do not need this field.
14    #[default]
15    Unspecified,
16    /// Active TCP candidate, which initiates TCP connections.
17    Active,
18    /// Passive TCP candidate, only accepts TCP connections.
19    Passive,
20    /// Like `Active` and `Passive` at the same time.
21    SimultaneousOpen,
22}
23
24// from creates a new TCPType from string.
25impl From<&str> for TcpType {
26    fn from(raw: &str) -> Self {
27        match raw {
28            "active" => Self::Active,
29            "passive" => Self::Passive,
30            "so" => Self::SimultaneousOpen,
31            _ => Self::Unspecified,
32        }
33    }
34}
35
36impl fmt::Display for TcpType {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        let s = match *self {
39            Self::Active => "active",
40            Self::Passive => "passive",
41            Self::SimultaneousOpen => "so",
42            Self::Unspecified => "unspecified",
43        };
44        write!(f, "{s}")
45    }
46}