Skip to main content

volans_core/
connection.rs

1use crate::Multiaddr;
2
3#[derive(PartialEq, Eq, Debug, Clone, Hash)]
4pub enum ConnectedPoint {
5    Dialer {
6        addr: Multiaddr,
7    },
8    Listener {
9        local_addr: Multiaddr,
10        remote_addr: Multiaddr,
11    },
12}
13
14impl ConnectedPoint {
15    pub fn to_endpoint(&self) -> Endpoint {
16        match self {
17            ConnectedPoint::Dialer { .. } => Endpoint::Dialer,
18            ConnectedPoint::Listener { .. } => Endpoint::Listener,
19        }
20    }
21
22    pub fn is_dialer(&self) -> bool {
23        matches!(self, ConnectedPoint::Dialer { .. })
24    }
25
26    pub fn is_listener(&self) -> bool {
27        matches!(self, ConnectedPoint::Listener { .. })
28    }
29}
30
31#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
32pub enum Endpoint {
33    Dialer,
34    Listener,
35}
36
37impl std::ops::Not for Endpoint {
38    type Output = Endpoint;
39
40    fn not(self) -> Self::Output {
41        match self {
42            Endpoint::Dialer => Endpoint::Listener,
43            Endpoint::Listener => Endpoint::Dialer,
44        }
45    }
46}
47
48impl Endpoint {
49    pub fn is_dialer(self) -> bool {
50        matches!(self, Endpoint::Dialer)
51    }
52
53    pub fn is_listener(self) -> bool {
54        matches!(self, Endpoint::Listener)
55    }
56}
57
58impl From<&'_ ConnectedPoint> for Endpoint {
59    fn from(endpoint: &'_ ConnectedPoint) -> Endpoint {
60        endpoint.to_endpoint()
61    }
62}
63
64impl From<ConnectedPoint> for Endpoint {
65    fn from(endpoint: ConnectedPoint) -> Endpoint {
66        endpoint.to_endpoint()
67    }
68}