h3ron_graph/graph/
node.rs

1use std::ops::{Add, AddAssign};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(PartialEq, Eq, Debug, Copy, Clone, Serialize, Deserialize)]
6pub enum NodeType {
7    Origin,
8    Destination,
9    OriginAndDestination,
10}
11
12impl NodeType {
13    pub const fn is_origin(&self) -> bool {
14        match self {
15            Self::Origin => true,
16            Self::Destination => false,
17            Self::OriginAndDestination => true,
18        }
19    }
20
21    pub const fn is_destination(&self) -> bool {
22        match self {
23            Self::Origin => false,
24            Self::Destination => true,
25            Self::OriginAndDestination => true,
26        }
27    }
28}
29
30impl Add<Self> for NodeType {
31    type Output = Self;
32
33    fn add(self, rhs: Self) -> Self::Output {
34        if rhs == self {
35            self
36        } else {
37            Self::OriginAndDestination
38        }
39    }
40}
41
42impl AddAssign<Self> for NodeType {
43    fn add_assign(&mut self, rhs: Self) {
44        if self != &rhs {
45            *self = Self::OriginAndDestination
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use crate::graph::node::NodeType;
53
54    #[test]
55    fn test_nodetype_add() {
56        assert_eq!(NodeType::Origin, NodeType::Origin + NodeType::Origin);
57        assert_eq!(
58            NodeType::Destination,
59            NodeType::Destination + NodeType::Destination
60        );
61        assert_eq!(
62            NodeType::OriginAndDestination,
63            NodeType::Origin + NodeType::Destination
64        );
65        assert_eq!(
66            NodeType::OriginAndDestination,
67            NodeType::OriginAndDestination + NodeType::Destination
68        );
69        assert_eq!(
70            NodeType::OriginAndDestination,
71            NodeType::Destination + NodeType::Origin
72        );
73    }
74
75    #[test]
76    fn test_nodetype_addassign() {
77        let mut n1 = NodeType::Origin;
78        n1 += NodeType::Origin;
79        assert_eq!(n1, NodeType::Origin);
80
81        let mut n2 = NodeType::Origin;
82        n2 += NodeType::OriginAndDestination;
83        assert_eq!(n2, NodeType::OriginAndDestination);
84
85        let mut n3 = NodeType::Destination;
86        n3 += NodeType::OriginAndDestination;
87        assert_eq!(n3, NodeType::OriginAndDestination);
88
89        let mut n4 = NodeType::Destination;
90        n4 += NodeType::Origin;
91        assert_eq!(n4, NodeType::OriginAndDestination);
92    }
93}