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