solomon_gremlin/structure/
vertex.rs1use uuid::Uuid;
2
3use crate::structure::GID;
4use crate::VertexProperty;
5use std::collections::hash_map::{IntoIter, Iter};
6use std::collections::HashMap;
7use std::hash::Hasher;
8
9pub type VertexPropertyMap = HashMap<String, Vec<VertexProperty>>;
10
11#[derive(Debug, Clone)]
12pub struct Vertex {
13 id: GID,
14 label: String,
15 properties: VertexPropertyMap,
16}
17
18impl Default for Vertex {
19 fn default() -> Self {
20 let uuid = Uuid::new_v4().as_bytes().to_vec();
21 let gid = GID::Bytes(uuid);
22 Vertex::partial_new(gid)
23 }
24}
25
26impl Vertex {
32 pub fn new<T>(id: GID, label: T, properties: VertexPropertyMap) -> Vertex
33 where
34 T: Into<String>,
35 {
36 Vertex {
37 id,
38 label: label.into(),
39 properties,
40 }
41 }
42
43 pub fn partial_new(id: GID) -> Vertex {
44 Vertex {
45 id,
46 label: Default::default(),
47 properties: Default::default(),
48 }
49 }
50
51 pub fn add_label<T>(&mut self, label: T)
52 where
53 T: Into<String>,
54 {
55 self.label = label.into();
56 }
57
58 pub fn add_property(&mut self, property: VertexProperty) {
59 self.properties.entry(property.label().to_string()).or_default().push(property);
60 }
61
62 pub fn add_properties(&mut self, properties: VertexPropertyMap) {
63 self.properties = properties;
64 }
65
66 pub fn id(&self) -> &GID {
67 &self.id
68 }
69
70 pub fn label(&self) -> &String {
71 &self.label
72 }
73
74 pub fn has_label(&self) -> bool {
75 !&self.label.is_empty()
76 }
77
78 pub fn iter(&self) -> Iter<String, Vec<VertexProperty>> {
79 self.properties.iter()
80 }
81
82 pub fn property(&self, key: &str) -> Option<&Vec<VertexProperty>> {
83 self.properties.get(key)
84 }
85
86 pub fn properties(&self) -> HashMap<String, Vec<VertexProperty>> {
87 self.properties.clone()
88 }
89}
90
91impl IntoIterator for Vertex {
92 type Item = (String, Vec<VertexProperty>);
93 type IntoIter = IntoIter<String, Vec<VertexProperty>>;
94 fn into_iter(self) -> Self::IntoIter {
95 self.properties.into_iter()
96 }
97}
98
99impl std::cmp::Eq for Vertex {}
100
101impl std::hash::Hash for Vertex {
102 fn hash<H: Hasher>(&self, state: &mut H) {
103 self.id.hash(state);
104 }
105}
106
107impl PartialEq for Vertex {
108 fn eq(&self, other: &Vertex) -> bool {
109 &self.id == other.id()
110 }
111}