1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub struct NodeId(pub i64);
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub struct EdgeId(pub i64);
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub struct BranchId(pub String);
15
16impl fmt::Display for NodeId {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 write!(f, "NodeId({})", self.0)
19 }
20}
21
22impl fmt::Display for EdgeId {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 write!(f, "EdgeId({})", self.0)
25 }
26}
27
28impl fmt::Display for BranchId {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 write!(f, "{}", self.0)
31 }
32}
33
34impl From<i64> for NodeId {
35 fn from(id: i64) -> Self {
36 Self(id)
37 }
38}
39
40impl From<i64> for EdgeId {
41 fn from(id: i64) -> Self {
42 Self(id)
43 }
44}
45
46impl From<String> for BranchId {
47 fn from(s: String) -> Self {
48 Self(s)
49 }
50}
51
52impl From<&str> for BranchId {
53 fn from(s: &str) -> Self {
54 Self(s.to_owned())
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn node_id_equality() {
64 assert_eq!(NodeId(1), NodeId(1));
65 assert_ne!(NodeId(1), NodeId(2));
66 }
67
68 #[test]
69 fn edge_id_equality() {
70 assert_eq!(EdgeId(1), EdgeId(1));
71 assert_ne!(EdgeId(1), EdgeId(2));
72 }
73
74 #[test]
75 fn branch_id_from_str() {
76 let b = BranchId::from("main");
77 assert_eq!(b.0, "main");
78 }
79
80 #[test]
81 fn branch_id_from_string() {
82 let b = BranchId::from("feature/foo".to_owned());
83 assert_eq!(b.0, "feature/foo");
84 }
85
86 #[test]
87 fn id_display() {
88 assert_eq!(NodeId(42).to_string(), "NodeId(42)");
89 assert_eq!(EdgeId(7).to_string(), "EdgeId(7)");
90 assert_eq!(BranchId::from("main").to_string(), "main");
91 }
92
93 #[test]
94 fn id_from_i64() {
95 let n: NodeId = 5i64.into();
96 assert_eq!(n, NodeId(5));
97 let e: EdgeId = 10i64.into();
98 assert_eq!(e, EdgeId(10));
99 }
100}