solomon_gremlin/structure/
edge.rs

1use uuid::Uuid;
2
3use crate::structure::{Property, Vertex, GID};
4use std::collections::hash_map::{IntoIter, Iter};
5use std::collections::HashMap;
6use std::hash::Hasher;
7
8pub type PropertyMap = HashMap<String, Property>;
9
10#[derive(Debug, Clone)]
11pub struct Edge {
12	id: GID,
13	label: String,
14	in_v: Option<Vertex>,
15	out_v: Option<Vertex>,
16	properties: PropertyMap,
17}
18
19impl Default for Edge {
20	fn default() -> Self {
21		let gid = GID::String(Uuid::new_v4().to_string());
22		Self {
23			id: gid,
24			label: Default::default(),
25			in_v: Default::default(),
26			out_v: Default::default(),
27			properties: Default::default(),
28		}
29	}
30}
31
32impl Edge {
33	pub fn new<T>(
34		id: GID,
35		label: T,
36		in_v: Option<Vertex>,
37		out_v: Option<Vertex>,
38		properties: HashMap<String, Property>,
39	) -> Edge
40	where
41		T: Into<String>,
42	{
43		Edge {
44			id,
45			label: label.into(),
46			in_v,
47			out_v,
48			properties,
49		}
50	}
51
52	pub fn partial_new(id: GID) -> Edge {
53		Edge {
54			id,
55			label: Default::default(),
56			properties: Default::default(),
57			in_v: None,
58			out_v: None,
59		}
60	}
61
62	pub fn add_label<T>(&mut self, label: T)
63	where
64		T: Into<String>,
65	{
66		self.label = label.into();
67	}
68
69	pub fn add_property(&mut self, property: Property) {
70		self.properties.entry(property.label().to_string()).or_insert(property);
71	}
72
73	pub fn add_properties(&mut self, properties: PropertyMap) {
74		self.properties = properties;
75	}
76
77	pub fn id(&self) -> &GID {
78		&self.id
79	}
80
81	pub fn label(&self) -> &String {
82		&self.label
83	}
84
85	pub fn set_partial_in_v(&mut self, id: GID) {
86		self.in_v = Some(Vertex::partial_new(id));
87	}
88
89	pub fn set_partial_out_v(&mut self, id: GID) {
90		self.in_v = Some(Vertex::partial_new(id));
91	}
92
93	pub fn set_in_v(&mut self, v: Vertex) {
94		self.in_v = Some(v);
95	}
96
97	pub fn set_out_v(&mut self, v: Vertex) {
98		self.out_v = Some(v);
99	}
100
101	pub fn in_v(&self) -> &Option<Vertex> {
102		&self.in_v
103	}
104	pub fn out_v(&self) -> &Option<Vertex> {
105		&self.out_v
106	}
107
108	pub fn iter(&self) -> Iter<String, Property> {
109		self.properties.iter()
110	}
111
112	pub fn property(&self, key: &str) -> Option<&Property> {
113		self.properties.get(key)
114	}
115}
116
117impl IntoIterator for Edge {
118	type Item = (String, Property);
119	type IntoIter = IntoIter<String, Property>;
120	fn into_iter(self) -> Self::IntoIter {
121		self.properties.into_iter()
122	}
123}
124
125impl std::cmp::Eq for Edge {}
126
127impl PartialEq for Edge {
128	fn eq(&self, other: &Edge) -> bool {
129		&self.id == other.id()
130	}
131}
132
133impl std::hash::Hash for Edge {
134	fn hash<H: Hasher>(&self, state: &mut H) {
135		self.id.hash(state);
136	}
137}