1use std::fmt;
2
3#[cfg(test)]
4mod direction_test;
5
6#[derive(Default, Debug, PartialEq, Eq, Clone)]
8pub enum Direction {
9 #[default]
10 Unspecified = 0,
12 SendRecv = 1,
14 SendOnly = 2,
16 RecvOnly = 3,
18 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 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}