rtci-ice 0.4.0

Sans-I/O ICE agent (RFC 8445) with trickle ICE, ICE-TCP and consent freshness, for WebRTC
Documentation
#[cfg(test)]
mod tcp_type_test;

use std::fmt;

// TCPType is the type of ICE TCP candidate as described in
// https://tools.ietf.org/html/rfc6544#section-4.5
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
/// The role of an ICE-TCP candidate, per [RFC 6544] §4.5.
///
/// [RFC 6544]: https://datatracker.ietf.org/doc/html/rfc6544#section-4.5
#[non_exhaustive]
pub enum TcpType {
    /// The default value. For example UDP candidates do not need this field.
    #[default]
    Unspecified,
    /// Active TCP candidate, which initiates TCP connections.
    Active,
    /// Passive TCP candidate, only accepts TCP connections.
    Passive,
    /// Like `Active` and `Passive` at the same time.
    SimultaneousOpen,
}

// from creates a new TCPType from string.
impl From<&str> for TcpType {
    fn from(raw: &str) -> Self {
        match raw {
            "active" => Self::Active,
            "passive" => Self::Passive,
            "so" => Self::SimultaneousOpen,
            _ => Self::Unspecified,
        }
    }
}

impl TcpType {
    /// The TCP type a remote candidate needs to pair with a local one of this type: an
    /// active candidate pairs with a passive one and the other way round, simultaneous-open
    /// with simultaneous-open (RFC 6544 §6.2). `Unspecified` (UDP) stays `Unspecified`.
    pub fn complement(self) -> Self {
        match self {
            Self::Active => Self::Passive,
            Self::Passive => Self::Active,
            other => other,
        }
    }
}

impl fmt::Display for TcpType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match *self {
            Self::Active => "active",
            Self::Passive => "passive",
            Self::SimultaneousOpen => "so",
            Self::Unspecified => "unspecified",
        };
        write!(f, "{s}")
    }
}