gel_db_protocol/
datatypes.rs1use std::str::Utf8Error;
2pub use uuid::Uuid;
3
4#[derive(Copy, Debug, PartialEq, Eq, Default, Clone)]
6pub struct Rest<'a> {
7 buf: &'a [u8],
8}
9
10impl<'a> Rest<'a> {
11 pub fn new(buf: &'a [u8]) -> Self {
12 Self { buf }
13 }
14}
15
16impl Rest<'_> {}
17
18impl AsRef<[u8]> for Rest<'_> {
19 fn as_ref(&self) -> &[u8] {
20 self.buf
21 }
22}
23
24impl std::ops::Deref for Rest<'_> {
25 type Target = [u8];
26 fn deref(&self) -> &Self::Target {
27 self.buf
28 }
29}
30
31impl PartialEq<[u8]> for Rest<'_> {
32 fn eq(&self, other: &[u8]) -> bool {
33 self.buf == other
34 }
35}
36
37impl<const N: usize> PartialEq<&[u8; N]> for Rest<'_> {
38 fn eq(&self, other: &&[u8; N]) -> bool {
39 self.buf == *other
40 }
41}
42
43impl PartialEq<&[u8]> for Rest<'_> {
44 fn eq(&self, other: &&[u8]) -> bool {
45 self.buf == *other
46 }
47}
48
49#[derive(Copy, Clone, Default)]
51pub struct ZTString<'a> {
52 buf: &'a [u8],
53}
54
55impl std::fmt::Debug for ZTString<'_> {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 String::from_utf8_lossy(self.buf).fmt(f)
58 }
59}
60
61impl<'a> ZTString<'a> {
62 pub fn new(buf: &'a [u8]) -> Self {
63 Self { buf }
64 }
65}
66
67impl ZTString<'_> {
68 pub fn to_owned(&self) -> Result<String, std::str::Utf8Error> {
69 std::str::from_utf8(self.buf).map(|s| s.to_owned())
70 }
71
72 pub fn to_str(&self) -> Result<&str, std::str::Utf8Error> {
73 std::str::from_utf8(self.buf)
74 }
75
76 pub fn to_string_lossy(&self) -> std::borrow::Cow<'_, str> {
77 String::from_utf8_lossy(self.buf)
78 }
79
80 pub fn to_bytes(&self) -> &[u8] {
81 self.buf
82 }
83}
84
85impl PartialEq for ZTString<'_> {
86 fn eq(&self, other: &Self) -> bool {
87 self.buf == other.buf
88 }
89}
90impl Eq for ZTString<'_> {}
91
92impl PartialEq<str> for ZTString<'_> {
93 fn eq(&self, other: &str) -> bool {
94 self.buf == other.as_bytes()
95 }
96}
97
98impl PartialEq<&str> for ZTString<'_> {
99 fn eq(&self, other: &&str) -> bool {
100 self.buf == other.as_bytes()
101 }
102}
103
104impl<'a> TryInto<&'a str> for ZTString<'a> {
105 type Error = Utf8Error;
106 fn try_into(self) -> Result<&'a str, Self::Error> {
107 std::str::from_utf8(self.buf)
108 }
109}
110
111#[derive(Copy, Clone, Default)]
113pub struct LString<'a> {
114 buf: &'a [u8],
115}
116
117impl std::fmt::Debug for LString<'_> {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 String::from_utf8_lossy(self.buf).fmt(f)
120 }
121}
122
123impl<'a> LString<'a> {
124 pub fn new(buf: &'a [u8]) -> Self {
125 Self { buf }
126 }
127}
128
129impl LString<'_> {
130 pub fn to_owned(&self) -> Result<String, std::str::Utf8Error> {
131 std::str::from_utf8(self.buf).map(|s| s.to_owned())
132 }
133
134 pub fn to_str(&self) -> Result<&str, std::str::Utf8Error> {
135 std::str::from_utf8(self.buf)
136 }
137
138 pub fn to_string_lossy(&self) -> std::borrow::Cow<'_, str> {
139 String::from_utf8_lossy(self.buf)
140 }
141
142 pub fn to_bytes(&self) -> &[u8] {
143 self.buf
144 }
145}
146
147impl PartialEq for LString<'_> {
148 fn eq(&self, other: &Self) -> bool {
149 self.buf == other.buf
150 }
151}
152impl Eq for LString<'_> {}
153
154impl PartialEq<str> for LString<'_> {
155 fn eq(&self, other: &str) -> bool {
156 self.buf == other.as_bytes()
157 }
158}
159
160impl PartialEq<&str> for LString<'_> {
161 fn eq(&self, other: &&str) -> bool {
162 self.buf == other.as_bytes()
163 }
164}
165
166impl<'a> TryInto<&'a str> for LString<'a> {
167 type Error = Utf8Error;
168 fn try_into(self) -> Result<&'a str, Self::Error> {
169 std::str::from_utf8(self.buf)
170 }
171}
172
173#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
174pub enum Encoded<'a> {
176 #[default]
177 Null,
178 Value(&'a [u8]),
179}
180
181impl Encoded<'_> {
182 pub fn to_string_lossy(&self) -> std::borrow::Cow<'_, str> {
183 match self {
184 Encoded::Null => "".into(),
185 Encoded::Value(value) => String::from_utf8_lossy(value),
186 }
187 }
188}
189
190impl<'a> AsRef<Encoded<'a>> for Encoded<'a> {
191 fn as_ref(&self) -> &Encoded<'a> {
192 self
193 }
194}
195
196impl Encoded<'_> {}
197
198impl PartialEq<str> for Encoded<'_> {
199 fn eq(&self, other: &str) -> bool {
200 self == &Encoded::Value(other.as_bytes())
201 }
202}
203
204impl PartialEq<&str> for Encoded<'_> {
205 fn eq(&self, other: &&str) -> bool {
206 self == &Encoded::Value(other.as_bytes())
207 }
208}
209
210impl PartialEq<[u8]> for Encoded<'_> {
211 fn eq(&self, other: &[u8]) -> bool {
212 self == &Encoded::Value(other)
213 }
214}
215
216impl PartialEq<&[u8]> for Encoded<'_> {
217 fn eq(&self, other: &&[u8]) -> bool {
218 self == &Encoded::Value(other)
219 }
220}
221
222#[derive(Copy, Clone, Default, PartialEq, Eq)]
223pub struct Length(pub i32);
224
225impl std::fmt::Debug for Length {
226 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
227 write!(f, "{}", self.0)
228 }
229}
230
231impl std::ops::Deref for Length {
232 type Target = i32;
233 fn deref(&self) -> &Self::Target {
234 &self.0
235 }
236}