Skip to main content

surrealdb_types/value/
bytes.rs

1use std::fmt::{self, Display, Formatter};
2use std::ops::Deref;
3
4use hex;
5use serde::de::{self, SeqAccess, Visitor};
6use serde::{Deserialize, Serialize};
7
8use crate::sql::{SqlFormat, ToSql};
9
10/// Represents binary data in SurrealDB
11///
12/// Bytes stores raw binary data as a vector of unsigned 8-bit integers.
13
14#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
15#[repr(transparent)]
16pub struct Bytes(pub(crate) ::bytes::Bytes);
17
18impl Bytes {
19	/// Convert the bytes to a bytes::Bytes
20	pub fn into_inner(self) -> bytes::Bytes {
21		self.0
22	}
23}
24
25impl From<Vec<u8>> for Bytes {
26	fn from(v: Vec<u8>) -> Self {
27		Self(bytes::Bytes::from(v))
28	}
29}
30
31impl From<Bytes> for bytes::Bytes {
32	fn from(bytes: Bytes) -> Self {
33		bytes.0
34	}
35}
36
37impl From<bytes::Bytes> for Bytes {
38	fn from(bytes: bytes::Bytes) -> Self {
39		Bytes(bytes)
40	}
41}
42
43impl Deref for Bytes {
44	type Target = bytes::Bytes;
45
46	fn deref(&self) -> &Self::Target {
47		&self.0
48	}
49}
50
51impl Display for Bytes {
52	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
53		write!(f, "b\"{}\"", hex::encode_upper(&self.0))
54	}
55}
56
57impl ToSql for crate::Bytes {
58	fn fmt_sql(&self, f: &mut String, _fmt: SqlFormat) {
59		f.push_str("b\"");
60		f.push_str(&hex::encode_upper(&self.0));
61		f.push('"');
62	}
63}
64
65impl Serialize for Bytes {
66	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
67	where
68		S: serde::Serializer,
69	{
70		serializer.serialize_bytes(&self.0)
71	}
72}
73
74impl<'de> Deserialize<'de> for Bytes {
75	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
76	where
77		D: serde::Deserializer<'de>,
78	{
79		struct RawBytesVisitor;
80
81		impl<'de> Visitor<'de> for RawBytesVisitor {
82			type Value = Bytes;
83
84			fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
85				formatter.write_str("bytes or sequence of bytes")
86			}
87
88			fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
89			where
90				E: de::Error,
91			{
92				Ok(Bytes(bytes::Bytes::from(v)))
93			}
94
95			fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
96			where
97				E: de::Error,
98			{
99				Ok(Bytes(::bytes::Bytes::copy_from_slice(v)))
100			}
101
102			fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
103			where
104				A: SeqAccess<'de>,
105			{
106				let capacity = seq.size_hint().unwrap_or_default();
107				let mut vec = Vec::with_capacity(capacity);
108				while let Some(byte) = seq.next_element()? {
109					vec.push(byte);
110				}
111				Ok(Bytes(bytes::Bytes::from(vec)))
112			}
113		}
114
115		deserializer.deserialize_byte_buf(RawBytesVisitor)
116	}
117}
118
119#[cfg(feature = "arbitrary")]
120mod arb {
121	use arbitrary::{Arbitrary, Unstructured};
122
123	use super::*;
124
125	impl<'a> Arbitrary<'a> for Bytes {
126		fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
127			let b: &'a [u8] = u.arbitrary()?;
128			Ok(Self(::bytes::Bytes::copy_from_slice(b)))
129		}
130	}
131}