gremlin_client/structure/
map.rs1use crate::error::GremlinError;
2use crate::structure::{Edge, GValue, Vertex};
3use crate::GremlinResult;
4use crate::Token;
5use std::collections::hash_map::IntoIter;
6use std::collections::{BTreeMap, HashMap};
7use std::convert::{TryFrom, TryInto};
8
9#[derive(Debug, PartialEq, Clone)]
12pub struct Map(HashMap<GKey, GValue>);
13
14impl Map {
15 pub(crate) fn empty() -> Map {
16 Map(HashMap::default())
17 }
18}
19
20impl From<HashMap<GKey, GValue>> for Map {
21 fn from(val: HashMap<GKey, GValue>) -> Self {
22 Map(val)
23 }
24}
25
26impl From<Map> for HashMap<GKey, GValue> {
27 fn from(map: Map) -> Self {
28 map.0
29 }
30}
31
32impl From<HashMap<String, GValue>> for Map {
33 fn from(val: HashMap<String, GValue>) -> Self {
34 let map = val.into_iter().map(|(k, v)| (GKey::String(k), v)).collect();
35 Map(map)
36 }
37}
38
39impl TryFrom<Map> for HashMap<String, GValue> {
40 type Error = GremlinError;
41
42 fn try_from(map: Map) -> Result<Self, Self::Error> {
43 map.into_iter()
44 .map(|(k, v)| Ok((k.try_into()?, v)))
45 .collect()
46 }
47}
48
49impl From<BTreeMap<String, GValue>> for Map {
50 fn from(val: BTreeMap<String, GValue>) -> Self {
51 let map = val.into_iter().map(|(k, v)| (GKey::String(k), v)).collect();
52 Map(map)
53 }
54}
55
56impl TryFrom<Map> for BTreeMap<String, GValue> {
57 type Error = GremlinError;
58
59 fn try_from(map: Map) -> Result<Self, Self::Error> {
60 map.into_iter()
61 .map(|(k, v)| Ok((k.try_into()?, v)))
62 .collect()
63 }
64}
65
66impl Map {
67 pub(crate) fn remove<T>(&mut self, key: T) -> Option<GValue>
68 where
69 T: Into<GKey>,
70 {
71 self.0.remove(&key.into())
72 }
73 pub fn iter(&self) -> impl Iterator<Item = (&GKey, &GValue)> {
75 self.0.iter()
76 }
77
78 pub fn get<T>(&self, key: T) -> Option<&GValue>
80 where
81 T: Into<GKey>,
82 {
83 self.0.get(&key.into())
84 }
85
86 pub fn try_get<K, V>(&self, key: K) -> GremlinResult<V>
88 where
89 K: Into<GKey>,
90 V: std::convert::TryFrom<GValue, Error = GremlinError>,
91 {
92 self.0
93 .get(&key.into())
94 .cloned()
95 .or_else(|| Some(GValue::Null))
96 .map(V::try_from)
97 .ok_or_else(|| GremlinError::Cast(String::from("field not found")))?
98 }
99
100 pub fn len(&self) -> usize {
102 self.0.len()
103 }
104
105 pub fn is_empty(&self) -> bool {
107 self.0.is_empty()
108 }
109}
110
111impl<T: Into<GKey>> std::ops::Index<T> for Map {
112 type Output = GValue;
113
114 fn index(&self, key: T) -> &GValue {
115 self.0.get(&key.into()).expect("no entry found for key")
116 }
117}
118
119impl IntoIterator for Map {
120 type Item = (GKey, GValue);
121 type IntoIter = IntoIter<GKey, GValue>;
122 fn into_iter(self) -> Self::IntoIter {
123 self.0.into_iter()
124 }
125}
126
127impl std::iter::FromIterator<(String, GValue)> for Map {
128 fn from_iter<I: IntoIterator<Item = (String, GValue)>>(iter: I) -> Self {
129 Map(iter
130 .into_iter()
131 .map(|(k, v)| (GKey::String(k), v))
132 .collect())
133 }
134}
135
136#[allow(clippy::large_enum_variant)]
138#[derive(Debug, PartialEq, Clone, Eq, Hash)]
139pub enum GKey {
140 String(String),
141 Token(Token),
142 Vertex(Vertex),
143 Edge(Edge),
144}
145
146impl From<&str> for GKey {
147 fn from(val: &str) -> Self {
148 GKey::String(String::from(val))
149 }
150}
151
152impl From<String> for GKey {
153 fn from(val: String) -> Self {
154 GKey::String(val)
155 }
156}
157
158impl TryFrom<GKey> for String {
159 type Error = GremlinError;
160
161 fn try_from(k: GKey) -> Result<Self, Self::Error> {
162 if let GKey::String(s) = k {
163 Ok(s)
164 } else {
165 Err(GremlinError::Cast(String::from(format!(
166 "Cannot cast from {:?} to String",
167 k
168 ))))
169 }
170 }
171}
172
173impl From<&Vertex> for GKey {
174 fn from(val: &Vertex) -> Self {
175 GKey::Vertex(val.clone())
176 }
177}
178
179impl From<&Edge> for GKey {
180 fn from(val: &Edge) -> Self {
181 GKey::Edge(val.clone())
182 }
183}