Skip to main content

surrealdb_types/
variables.rs

1use std::collections::{BTreeMap, HashMap};
2
3use serde::{Deserialize, Serialize};
4
5#[allow(unused_imports)]
6use crate as surrealdb_types;
7use crate::{Object, SurrealValue, Value};
8
9/// Represents a set of variables that can be used in a query.
10#[derive(Clone, Debug, Default, Eq, PartialEq, SurrealValue, Serialize, Deserialize)]
11#[surreal(crate = "crate")]
12#[serde(transparent)]
13pub struct Variables(BTreeMap<String, Value>);
14
15impl Variables {
16	/// Create a new empty variables map.
17	pub fn new() -> Self {
18		Self(BTreeMap::new())
19	}
20
21	/// Check if the variables map is empty.
22	pub fn is_empty(&self) -> bool {
23		self.0.is_empty()
24	}
25
26	/// Get the number of variables in the map.
27	pub fn len(&self) -> usize {
28		self.0.len()
29	}
30
31	/// Get an iterator over the variables in the map.
32	pub fn iter(&self) -> std::collections::btree_map::Iter<'_, String, Value> {
33		self.0.iter()
34	}
35
36	/// Get a variable from the map.
37	pub fn get(&self, key: &str) -> Option<&Value> {
38		self.0.get(key)
39	}
40
41	/// Insert a new variable into the map.
42	pub fn insert(&mut self, key: impl Into<String>, value: impl SurrealValue) {
43		self.0.insert(key.into(), value.into_value());
44	}
45
46	/// Remove a variable from the map.
47	pub fn remove(&mut self, key: &str) {
48		self.0.remove(key);
49	}
50
51	/// Extend the variables map with another variables map.
52	pub fn extend(&mut self, other: Variables) {
53		self.0.extend(other.0);
54	}
55}
56
57impl IntoIterator for Variables {
58	type Item = (String, Value);
59	type IntoIter = std::collections::btree_map::IntoIter<String, Value>;
60
61	fn into_iter(self) -> Self::IntoIter {
62		self.0.into_iter()
63	}
64}
65
66impl FromIterator<(String, Value)> for Variables {
67	fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
68		Self(iter.into_iter().collect())
69	}
70}
71
72impl From<BTreeMap<String, Value>> for Variables {
73	fn from(map: BTreeMap<String, Value>) -> Self {
74		Self(map)
75	}
76}
77
78impl From<BTreeMap<String, String>> for Variables {
79	fn from(map: BTreeMap<String, String>) -> Self {
80		Self(map.into_iter().map(|(k, v)| (k, Value::String(v))).collect())
81	}
82}
83
84impl From<HashMap<String, String>> for Variables {
85	fn from(map: HashMap<String, String>) -> Self {
86		Self(map.into_iter().map(|(k, v)| (k, Value::String(v))).collect())
87	}
88}
89
90impl From<Object> for Variables {
91	fn from(obj: Object) -> Self {
92		Self(obj.0)
93	}
94}
95
96impl From<Variables> for Object {
97	fn from(vars: Variables) -> Self {
98		Object(vars.0)
99	}
100}
101
102#[cfg(test)]
103mod tests {
104	use super::*;
105
106	#[test]
107	fn test_variables_serde() {
108		let vars = Variables::new();
109		let json = serde_json::to_string(&vars).unwrap();
110		assert_eq!(json, "{}");
111
112		let vars1 = serde_json::from_str::<Variables>("{\"name\":{\"String\":\"John\"}}").unwrap();
113		assert_eq!(vars1.get("name"), Some(&Value::String("John".to_string())));
114
115		let vars2 =
116			Variables::from_iter(vec![("name".to_string(), "John".to_string().into_value())]);
117		let json = serde_json::to_string(&vars2).unwrap();
118		assert_eq!(json, "{\"name\":{\"String\":\"John\"}}");
119	}
120}