surrealdb_core/sql/
strand.rs1use crate::err::Error;
2use revision::revisioned;
3use serde::{Deserialize, Serialize};
4use std::fmt::{self, Display, Formatter};
5use std::ops::Deref;
6use std::ops::{self};
7use std::str;
8
9use super::escape::QuoteStr;
10use super::value::TryAdd;
11
12pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Strand";
13
14#[revisioned(revision = 1)]
16#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
17#[serde(rename = "$surrealdb::private::sql::Strand")]
18#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
19#[non_exhaustive]
20pub struct Strand(#[serde(with = "no_nul_bytes")] pub String);
21
22impl Strand {
23 pub fn new(s: String) -> Option<Strand> {
25 if s.contains('\0') {
26 None
27 } else {
28 Some(Strand(s))
29 }
30 }
31
32 pub unsafe fn new_unchecked(s: String) -> Strand {
37 debug_assert!(!s.contains('\0'));
39 Strand(s)
40 }
41}
42
43impl From<String> for Strand {
44 fn from(s: String) -> Self {
45 unsafe { Self::new_unchecked(s) }
47 }
48}
49
50impl From<&str> for Strand {
51 fn from(s: &str) -> Self {
52 unsafe { Self::new_unchecked(s.to_string()) }
54 }
55}
56
57impl Deref for Strand {
58 type Target = String;
59 fn deref(&self) -> &Self::Target {
60 &self.0
61 }
62}
63
64impl From<Strand> for String {
65 fn from(s: Strand) -> Self {
66 s.0
67 }
68}
69
70impl Strand {
71 pub fn as_str(&self) -> &str {
73 self.0.as_str()
74 }
75 pub fn as_string(self) -> String {
77 self.0
78 }
79 pub fn to_raw(self) -> String {
81 self.0
82 }
83}
84
85impl Display for Strand {
86 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
87 QuoteStr(&self.0).fmt(f)
88 }
89}
90
91impl ops::Add for Strand {
92 type Output = Self;
93 fn add(mut self, other: Self) -> Self {
94 self.0.push_str(other.as_str());
95 self
96 }
97}
98
99impl TryAdd for Strand {
100 type Output = Self;
101 fn try_add(mut self, other: Self) -> Result<Self, Error> {
102 if self.0.try_reserve(other.len()).is_ok() {
103 self.0.push_str(other.as_str());
104 Ok(self)
105 } else {
106 Err(Error::InsufficientReserve(format!(
107 "additional string of length {} bytes",
108 other.0.len()
109 )))
110 }
111 }
112}
113
114pub(crate) mod no_nul_bytes {
116 use serde::{
117 de::{self, Visitor},
118 Deserializer, Serializer,
119 };
120 use std::fmt;
121
122 pub(crate) fn serialize<S>(s: &str, serializer: S) -> Result<S::Ok, S::Error>
123 where
124 S: Serializer,
125 {
126 if s.contains('\0') {
127 return Err(<S::Error as serde::ser::Error>::custom(
128 "to be serialized string contained a null byte",
129 ));
130 }
131 serializer.serialize_str(s)
132 }
133
134 pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
135 where
136 D: Deserializer<'de>,
137 {
138 struct NoNulBytesVisitor;
139
140 impl Visitor<'_> for NoNulBytesVisitor {
141 type Value = String;
142
143 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
144 formatter.write_str("a string without any NUL bytes")
145 }
146
147 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
148 where
149 E: de::Error,
150 {
151 if value.contains('\0') {
152 Err(de::Error::custom("contained NUL byte"))
153 } else {
154 Ok(value.to_owned())
155 }
156 }
157
158 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
159 where
160 E: de::Error,
161 {
162 if value.contains('\0') {
163 Err(de::Error::custom("contained NUL byte"))
164 } else {
165 Ok(value)
166 }
167 }
168 }
169
170 deserializer.deserialize_string(NoNulBytesVisitor)
171 }
172}