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 From<String> for Strand {
23 fn from(s: String) -> Self {
24 Strand(s)
25 }
26}
27
28impl From<&str> for Strand {
29 fn from(s: &str) -> Self {
30 Self::from(String::from(s))
31 }
32}
33
34impl Deref for Strand {
35 type Target = String;
36 fn deref(&self) -> &Self::Target {
37 &self.0
38 }
39}
40
41impl From<Strand> for String {
42 fn from(s: Strand) -> Self {
43 s.0
44 }
45}
46
47impl Strand {
48 pub fn as_str(&self) -> &str {
50 self.0.as_str()
51 }
52 pub fn as_string(self) -> String {
54 self.0
55 }
56 pub fn to_raw(self) -> String {
58 self.0
59 }
60}
61
62impl Display for Strand {
63 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
64 QuoteStr(&self.0).fmt(f)
65 }
66}
67
68impl ops::Add for Strand {
69 type Output = Self;
70 fn add(mut self, other: Self) -> Self {
71 self.0.push_str(other.as_str());
72 self
73 }
74}
75
76impl TryAdd for Strand {
77 type Output = Self;
78 fn try_add(mut self, other: Self) -> Result<Self, Error> {
79 if self.0.try_reserve(other.len()).is_ok() {
80 self.0.push_str(other.as_str());
81 Ok(self)
82 } else {
83 Err(Error::InsufficientReserve(format!(
84 "additional string of length {} bytes",
85 other.0.len()
86 )))
87 }
88 }
89}
90
91pub(crate) mod no_nul_bytes {
93 use serde::{
94 de::{self, Visitor},
95 Deserializer, Serializer,
96 };
97 use std::fmt;
98
99 pub(crate) fn serialize<S>(s: &str, serializer: S) -> Result<S::Ok, S::Error>
100 where
101 S: Serializer,
102 {
103 serializer.serialize_str(s)
104 }
105
106 pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
107 where
108 D: Deserializer<'de>,
109 {
110 struct NoNulBytesVisitor;
111
112 impl Visitor<'_> for NoNulBytesVisitor {
113 type Value = String;
114
115 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
116 formatter.write_str("a string without any NUL bytes")
117 }
118
119 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
120 where
121 E: de::Error,
122 {
123 if value.contains('\0') {
124 Err(de::Error::custom("contained NUL byte"))
125 } else {
126 Ok(value.to_owned())
127 }
128 }
129
130 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
131 where
132 E: de::Error,
133 {
134 if value.contains('\0') {
135 Err(de::Error::custom("contained NUL byte"))
136 } else {
137 Ok(value)
138 }
139 }
140 }
141
142 deserializer.deserialize_string(NoNulBytesVisitor)
143 }
144}