bee_network/network/
origin.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use std::fmt;
5
6/// Describes direction of an established connection.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum Origin {
9    /// The connection is inbound (local=server).
10    Inbound,
11    /// The connection is outbound (local=client).
12    Outbound,
13}
14
15impl Origin {
16    /// Returns whether the connection is inbound.
17    pub fn is_inbound(&self) -> bool {
18        matches!(self, Self::Inbound)
19    }
20
21    /// Returns whether the connection is outbound.
22    pub fn is_outbound(&self) -> bool {
23        matches!(self, Self::Outbound)
24    }
25}
26
27impl fmt::Display for Origin {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match *self {
30            Origin::Inbound => f.write_str("inbound"),
31            Origin::Outbound => f.write_str("outbound"),
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn is_api() {
42        let mut origin = Origin::Inbound;
43        assert!(origin.is_inbound());
44
45        origin = Origin::Outbound;
46        assert!(origin.is_outbound());
47    }
48
49    #[test]
50    fn display() {
51        assert_eq!(&Origin::Inbound.to_string(), "inbound");
52        assert_eq!(&Origin::Outbound.to_string(), "outbound");
53    }
54}