Skip to main content

surrealdb_types/value/
array.rs

1use std::collections::{BTreeSet, BinaryHeap, HashSet, LinkedList, VecDeque};
2use std::ops::{Deref, DerefMut};
3
4use serde::{Deserialize, Serialize};
5
6use crate::sql::{SqlFormat, ToSql};
7use crate::{SurrealValue, Value};
8
9/// Represents an array of values in SurrealDB
10///
11/// An array is an ordered collection of values that can contain elements of any type.
12/// The underlying storage is a `Vec<Value>`.
13
14#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
15#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
16pub struct Array(pub(crate) Vec<Value>);
17
18impl Array {
19	/// Create a new empty array
20	pub fn new() -> Self {
21		Array(Vec::new())
22	}
23
24	/// Create a new array with capacity
25	pub fn with_capacity(len: usize) -> Self {
26		Self(Vec::with_capacity(len))
27	}
28
29	/// Convert the array into a vector of values.
30	pub fn into_vec(self) -> Vec<Value> {
31		self.0
32	}
33
34	/// Convert into the inner `Vec<Value>`
35	pub fn into_inner(self) -> Vec<Value> {
36		self.0
37	}
38}
39
40impl<T: SurrealValue> From<Vec<T>> for Array {
41	fn from(v: Vec<T>) -> Self {
42		v.into_iter().map(T::into_value).collect()
43	}
44}
45
46impl<T: SurrealValue + Eq + std::hash::Hash> From<HashSet<T>> for Array {
47	fn from(v: HashSet<T>) -> Self {
48		v.into_iter().map(T::into_value).collect()
49	}
50}
51
52impl<T: SurrealValue + Ord> From<BTreeSet<T>> for Array {
53	fn from(v: BTreeSet<T>) -> Self {
54		v.into_iter().map(T::into_value).collect()
55	}
56}
57
58impl<T: SurrealValue> From<VecDeque<T>> for Array {
59	fn from(v: VecDeque<T>) -> Self {
60		v.into_iter().map(T::into_value).collect()
61	}
62}
63
64impl<T: SurrealValue> From<LinkedList<T>> for Array {
65	fn from(v: LinkedList<T>) -> Self {
66		v.into_iter().map(T::into_value).collect()
67	}
68}
69
70impl<T: SurrealValue + Ord> From<BinaryHeap<T>> for Array {
71	fn from(v: BinaryHeap<T>) -> Self {
72		v.into_iter().map(T::into_value).collect()
73	}
74}
75
76impl From<Array> for Vec<Value> {
77	fn from(s: Array) -> Self {
78		s.0
79	}
80}
81
82impl<T: SurrealValue> FromIterator<T> for Array {
83	fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
84		Array(iter.into_iter().map(T::into_value).collect())
85	}
86}
87
88impl Deref for Array {
89	type Target = Vec<Value>;
90	fn deref(&self) -> &Self::Target {
91		&self.0
92	}
93}
94
95impl DerefMut for Array {
96	fn deref_mut(&mut self) -> &mut Self::Target {
97		&mut self.0
98	}
99}
100
101impl IntoIterator for Array {
102	type Item = Value;
103	type IntoIter = std::vec::IntoIter<Self::Item>;
104	fn into_iter(self) -> Self::IntoIter {
105		self.0.into_iter()
106	}
107}
108
109impl<'a> IntoIterator for &'a Array {
110	type Item = &'a Value;
111	type IntoIter = std::slice::Iter<'a, Value>;
112	fn into_iter(self) -> Self::IntoIter {
113		self.0.iter()
114	}
115}
116
117impl<'a> IntoIterator for &'a mut Array {
118	type Item = &'a mut Value;
119	type IntoIter = std::slice::IterMut<'a, Value>;
120	fn into_iter(self) -> Self::IntoIter {
121		self.0.iter_mut()
122	}
123}
124
125impl ToSql for Array {
126	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
127		use crate::sql::fmt_sql_comma_separated;
128
129		f.push('[');
130		if !self.is_empty() {
131			let inner_fmt = fmt.increment();
132			fmt_sql_comma_separated(&self.0, f, inner_fmt);
133		}
134		f.push(']');
135	}
136}