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,
11 SendRecv = 1,
13 SendOnly = 2,
15 RecvOnly = 3,
17 Inactive = 4,
19}
20
21const DIRECTION_SEND_RECV_STR: &str = "sendrecv";
22const DIRECTION_SEND_ONLY_STR: &str = "sendonly";
23const DIRECTION_RECV_ONLY_STR: &str = "recvonly";
24const DIRECTION_INACTIVE_STR: &str = "inactive";
25const DIRECTION_UNSPECIFIED_STR: &str = "Unspecified";
26
27impl fmt::Display for Direction {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 let s = match self {
30 Direction::SendRecv => DIRECTION_SEND_RECV_STR,
31 Direction::SendOnly => DIRECTION_SEND_ONLY_STR,
32 Direction::RecvOnly => DIRECTION_RECV_ONLY_STR,
33 Direction::Inactive => DIRECTION_INACTIVE_STR,
34 _ => DIRECTION_UNSPECIFIED_STR,
35 };
36 write!(f, "{s}")
37 }
38}
39
40impl Direction {
41 pub fn new(raw: &str) -> Self {
43 match raw {
44 DIRECTION_SEND_RECV_STR => Direction::SendRecv,
45 DIRECTION_SEND_ONLY_STR => Direction::SendOnly,
46 DIRECTION_RECV_ONLY_STR => Direction::RecvOnly,
47 DIRECTION_INACTIVE_STR => Direction::Inactive,
48 _ => Direction::Unspecified,
49 }
50 }
51}