1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum Direction {
8 Out,
10 In,
12 Both,
14}
15
16impl Direction {
17 pub fn as_str(&self) -> &'static str {
18 match self {
19 Self::Out => "out",
20 Self::In => "in",
21 Self::Both => "both",
22 }
23 }
24}
25
26impl std::fmt::Display for Direction {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 f.write_str(self.as_str())
29 }
30}
31
32impl std::str::FromStr for Direction {
33 type Err = String;
34 fn from_str(s: &str) -> Result<Self, Self::Err> {
35 match s.to_lowercase().as_str() {
36 "out" | "outgoing" => Ok(Self::Out),
37 "in" | "incoming" => Ok(Self::In),
38 "both" | "any" => Ok(Self::Both),
39 other => Err(format!("unknown direction: '{other}'")),
40 }
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn direction_roundtrip() {
50 for dir in [Direction::Out, Direction::In, Direction::Both] {
51 let s = dir.as_str();
52 let parsed: Direction = s.parse().unwrap();
53 assert_eq!(dir, parsed);
54 }
55 }
56
57 #[test]
58 fn direction_display() {
59 assert_eq!(Direction::Out.to_string(), "out");
60 }
61}