gremlin_client/structure/
vertex.rs

1use crate::structure::VertexProperty;
2use crate::structure::GID;
3use std::collections::hash_map::{IntoIter, Iter};
4use std::collections::HashMap;
5use std::hash::Hasher;
6
7#[derive(Debug, Clone)]
8pub struct Vertex {
9    id: GID,
10    label: String,
11    properties: HashMap<String, Vec<VertexProperty>>,
12}
13
14impl Vertex {
15    pub(crate) fn new<T>(
16        id: GID,
17        label: T,
18        properties: HashMap<String, Vec<VertexProperty>>,
19    ) -> Vertex
20    where
21        T: Into<String>,
22    {
23        Vertex {
24            id,
25            label: label.into(),
26            properties,
27        }
28    }
29
30    pub fn id(&self) -> &GID {
31        &self.id
32    }
33
34    pub fn label(&self) -> &String {
35        &self.label
36    }
37
38    pub fn iter(&self) -> Iter<String, Vec<VertexProperty>> {
39        self.properties.iter()
40    }
41
42    pub fn property(&self, key: &str) -> Option<&VertexProperty> {
43        self.properties.get(key).and_then(|v| v.get(0))
44    }
45}
46
47impl IntoIterator for Vertex {
48    type Item = (String, Vec<VertexProperty>);
49    type IntoIter = IntoIter<String, Vec<VertexProperty>>;
50    fn into_iter(self) -> Self::IntoIter {
51        self.properties.into_iter()
52    }
53}
54
55impl std::cmp::Eq for Vertex {}
56
57impl std::hash::Hash for Vertex {
58    fn hash<H: Hasher>(&self, state: &mut H) {
59        self.id.hash(state);
60    }
61}
62
63impl PartialEq for Vertex {
64    fn eq(&self, other: &Vertex) -> bool {
65        &self.id == other.id()
66    }
67}