1 use std::fmt; 2 3 #[cfg(test)] 4 mod direction_test; 5 6 /// Direction is a marker for transmission direction of an endpoint 7 #[derive(Default, Debug, PartialEq, Eq, Clone)] 8 pub enum Direction { 9 #[default] 10 Unspecified = 0, 11 /// Direction::SendRecv is for bidirectional communication 12 SendRecv = 1, 13 /// Direction::SendOnly is for outgoing communication 14 SendOnly = 2, 15 /// Direction::RecvOnly is for incoming communication 16 RecvOnly = 3, 17 /// Direction::Inactive is for no communication 18 Inactive = 4, 19 } 20 21 const DIRECTION_SEND_RECV_STR: &str = "sendrecv"; 22 const DIRECTION_SEND_ONLY_STR: &str = "sendonly"; 23 const DIRECTION_RECV_ONLY_STR: &str = "recvonly"; 24 const DIRECTION_INACTIVE_STR: &str = "inactive"; 25 const DIRECTION_UNSPECIFIED_STR: &str = "Unspecified"; 26 27 impl fmt::Display for Direction { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result28 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 40 impl Direction { 41 /// new defines a procedure for creating a new direction from a raw string. new(raw: &str) -> Self42 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 } 52