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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::error::GremlinError;
use crate::structure::{Edge, GValue, Vertex};
use crate::GremlinResult;
use crate::Token;
use std::collections::hash_map::IntoIter;
use std::collections::{BTreeMap, HashMap};
#[derive(Debug, PartialEq, Clone)]
pub struct Map(HashMap<GKey, GValue>);
impl Map {
pub(crate) fn empty() -> Map {
Map(HashMap::default())
}
}
impl From<HashMap<GKey, GValue>> for Map {
fn from(val: HashMap<GKey, GValue>) -> Self {
Map(val)
}
}
impl From<HashMap<String, GValue>> for Map {
fn from(val: HashMap<String, GValue>) -> Self {
let map = val.into_iter().map(|(k, v)| (GKey::String(k), v)).collect();
Map(map)
}
}
impl From<BTreeMap<String, GValue>> for Map {
fn from(val: BTreeMap<String, GValue>) -> Self {
let map = val.into_iter().map(|(k, v)| (GKey::String(k), v)).collect();
Map(map)
}
}
impl Map {
pub(crate) fn remove<T>(&mut self, key: T) -> Option<GValue>
where
T: Into<GKey>,
{
self.0.remove(&key.into())
}
pub fn iter(&self) -> impl Iterator<Item = (&GKey, &GValue)> {
self.0.iter()
}
pub fn get<T>(&self, key: T) -> Option<&GValue>
where
T: Into<GKey>,
{
self.0.get(&key.into())
}
pub fn try_get<K, V>(&self, key: K) -> GremlinResult<V>
where
K: Into<GKey>,
V: std::convert::TryFrom<GValue, Error = GremlinError>,
{
self.0
.get(&key.into())
.cloned()
.or_else(|| Some(GValue::Null))
.map(V::try_from)
.ok_or_else(|| GremlinError::Cast(String::from("field not found")))?
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<T: Into<GKey>> std::ops::Index<T> for Map {
type Output = GValue;
fn index(&self, key: T) -> &GValue {
self.0.get(&key.into()).expect("no entry found for key")
}
}
impl IntoIterator for Map {
type Item = (GKey, GValue);
type IntoIter = IntoIter<GKey, GValue>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl std::iter::FromIterator<(String, GValue)> for Map {
fn from_iter<I: IntoIterator<Item = (String, GValue)>>(iter: I) -> Self {
Map(iter
.into_iter()
.map(|(k, v)| (GKey::String(k), v))
.collect())
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub enum GKey {
String(String),
Token(Token),
Vertex(Vertex),
Edge(Edge),
}
impl From<&str> for GKey {
fn from(val: &str) -> Self {
GKey::String(String::from(val))
}
}
impl From<String> for GKey {
fn from(val: String) -> Self {
GKey::String(val)
}
}
impl From<&Vertex> for GKey {
fn from(val: &Vertex) -> Self {
GKey::Vertex(val.clone())
}
}
impl From<&Edge> for GKey {
fn from(val: &Edge) -> Self {
GKey::Edge(val.clone())
}
}