Skip to main content

rtc_sdp/direction/
mod.rs

1use std::fmt;
2
3#[cfg(test)]
4mod direction_test;
5
6/// Direction is a marker for transmission direction of an endpoint
7#[derive(Default, Debug, PartialEq, Eq, Clone)]
8pub enum Direction {
9    #[default]
10    /// No direction attribute was present.
11    Unspecified = 0,
12    /// Direction::SendRecv is for bidirectional communication
13    SendRecv = 1,
14    /// Direction::SendOnly is for outgoing communication
15    SendOnly = 2,
16    /// Direction::RecvOnly is for incoming communication
17    RecvOnly = 3,
18    /// Direction::Inactive is for no communication
19    Inactive = 4,
20}
21
22const DIRECTION_SEND_RECV_STR: &str = "sendrecv";
23const DIRECTION_SEND_ONLY_STR: &str = "sendonly";
24const DIRECTION_RECV_ONLY_STR: &str = "recvonly";
25const DIRECTION_INACTIVE_STR: &str = "inactive";
26const DIRECTION_UNSPECIFIED_STR: &str = "Unspecified";
27
28impl fmt::Display for Direction {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        let s = match self {
31            Direction::SendRecv => DIRECTION_SEND_RECV_STR,
32            Direction::SendOnly => DIRECTION_SEND_ONLY_STR,
33            Direction::RecvOnly => DIRECTION_RECV_ONLY_STR,
34            Direction::Inactive => DIRECTION_INACTIVE_STR,
35            _ => DIRECTION_UNSPECIFIED_STR,
36        };
37        write!(f, "{s}")
38    }
39}
40
41impl Direction {
42    /// new defines a procedure for creating a new direction from a raw string.
43    pub fn new(raw: &str) -> Self {
44        match raw {
45            DIRECTION_SEND_RECV_STR => Direction::SendRecv,
46            DIRECTION_SEND_ONLY_STR => Direction::SendOnly,
47            DIRECTION_RECV_ONLY_STR => Direction::RecvOnly,
48            DIRECTION_INACTIVE_STR => Direction::Inactive,
49            _ => Direction::Unspecified,
50        }
51    }
52}