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