1use std::cmp::Eq;
2use std::collections::HashMap;
3use std::convert::Into;
4use std::fmt::Debug;
5use std::hash::Hash;
6use std::net::IpAddr;
7
8use crate::frame::IntoBytes;
9use crate::time::PrimitiveDateTime;
10use uuid::Uuid;
11
12use super::blob::Blob;
13use super::decimal::Decimal;
14use super::*;
15
16#[derive(Debug, Clone, PartialEq)]
18pub enum ValueType {
19 Normal(i32),
20 Null,
21 NotSet,
22}
23
24impl IntoBytes for ValueType {
25 fn into_cbytes(&self) -> Vec<u8> {
26 match *self {
27 ValueType::Normal(n) => to_int(n),
28 ValueType::Null => i_to_n_bytes(-1, INT_LEN),
29 ValueType::NotSet => i_to_n_bytes(-2, INT_LEN),
30 }
31 }
32}
33
34#[derive(Debug, Clone, PartialEq)]
36pub struct Value {
37 pub body: Vec<u8>,
38 pub value_type: ValueType,
39}
40
41impl Value {
42 pub fn new_normal<B>(v: B) -> Value
44 where
45 B: Into<Bytes>,
46 {
47 let bytes = v.into().0;
48 let l = bytes.len() as i32;
49 Value {
50 body: bytes,
51 value_type: ValueType::Normal(l),
52 }
53 }
54
55 pub fn new_null() -> Value {
57 Value {
58 body: vec![],
59 value_type: ValueType::Null,
60 }
61 }
62
63 pub fn new_not_set() -> Value {
65 Value {
66 body: vec![],
67 value_type: ValueType::NotSet,
68 }
69 }
70}
71
72impl IntoBytes for Value {
73 fn into_cbytes(&self) -> Vec<u8> {
74 let mut v = Vec::with_capacity(INT_LEN + self.body.len());
75 v.extend_from_slice(self.value_type.into_cbytes().as_slice());
76 v.extend_from_slice(self.body.as_slice());
77 v
78 }
79}
80
81impl<T: Into<Bytes>> From<T> for Value {
82 fn from(b: T) -> Value {
83 Value::new_normal(b.into())
84 }
85}
86
87impl <T: serde::Serialize + Into<Bytes>> Into<Bytes> for Option<T> {
88 fn into(self) -> Bytes {
89 match self {
90 None => "".to_string().into(),
92 Some(t) => t.into(),
93 }
94 }
95}
96
97#[derive(Debug, Clone)]
98pub struct Bytes(Vec<u8>);
99
100impl Bytes {
101 pub fn new(bytes: Vec<u8>) -> Bytes {
102 Bytes(bytes)
103 }
104}
105
106impl Into<Bytes> for String {
107 fn into(self) -> Bytes {
108 Bytes(self.into_bytes())
109 }
110}
111
112impl<'a> Into<Bytes> for &'a str {
113 fn into(self) -> Bytes {
114 Bytes(self.as_bytes().to_vec())
115 }
116}
117
118impl Into<Bytes> for i8 {
119 fn into(self) -> Bytes {
120 Bytes(vec![self as u8])
121 }
122}
123
124impl Into<Bytes> for i16 {
125 fn into(self) -> Bytes {
126 Bytes(to_short(self))
127 }
128}
129
130impl Into<Bytes> for i32 {
131 fn into(self) -> Bytes {
132 Bytes(to_int(self))
133 }
134}
135
136impl Into<Bytes> for i64 {
137 fn into(self) -> Bytes {
138 Bytes(to_bigint(self))
139 }
140}
141
142impl Into<Bytes> for u8 {
143 fn into(self) -> Bytes {
144 Bytes(vec![self])
145 }
146}
147
148impl Into<Bytes> for u16 {
149 fn into(self) -> Bytes {
150 Bytes(to_u_short(self))
151 }
152}
153
154impl Into<Bytes> for u32 {
155 fn into(self) -> Bytes {
156 Bytes(to_u(self))
157 }
158}
159
160impl Into<Bytes> for u64 {
161 fn into(self) -> Bytes {
162 Bytes(to_u_big(self))
163 }
164}
165
166impl Into<Bytes> for bool {
167 fn into(self) -> Bytes {
168 if self {
169 Bytes(vec![1])
170 } else {
171 Bytes(vec![0])
172 }
173 }
174}
175
176impl Into<Bytes> for Uuid {
177 fn into(self) -> Bytes {
178 Bytes(self.as_bytes().to_vec())
179 }
180}
181
182impl Into<Bytes> for IpAddr {
183 fn into(self) -> Bytes {
184 match self {
185 IpAddr::V4(ip) => Bytes(ip.octets().to_vec()),
186 IpAddr::V6(ip) => Bytes(ip.octets().to_vec()),
187 }
188 }
189}
190
191impl Into<Bytes> for f32 {
192 fn into(self) -> Bytes {
193 Bytes(to_float(self))
194 }
195}
196
197impl Into<Bytes> for f64 {
198 fn into(self) -> Bytes {
199 Bytes(to_float_big(self))
200 }
201}
202
203impl Into<Bytes> for PrimitiveDateTime {
204 fn into(self) -> Bytes {
205 let ts: i64 = self.assume_utc().timestamp() * 1_000 + self.nanosecond() as i64 / 1_000_000;
206 Bytes(to_bigint(ts))
207 }
208}
209
210impl Into<Bytes> for Blob {
211 fn into(self) -> Bytes {
212 Bytes(self.into_vec())
213 }
214}
215
216impl Into<Bytes> for Decimal {
217 fn into(self) -> Bytes {
218 Bytes(self.into_cbytes())
219 }
220}
221
222impl<T: Into<Bytes> + Clone + Debug> From<Vec<T>> for Bytes {
223 fn from(vec: Vec<T>) -> Bytes {
224 let mut bytes: Vec<u8> = vec![];
225 bytes.extend_from_slice(to_int(vec.len() as i32).as_slice());
226 bytes = vec.iter().fold(bytes, |mut acc, v| {
227 let b: Bytes = v.clone().into();
228 acc.extend_from_slice(Value::new_normal(b).into_cbytes().as_slice());
229 acc
230 });
231 Bytes(bytes)
232 }
233}
234
235impl<K, V> From<HashMap<K, V>> for Bytes
236where
237 K: Into<Bytes> + Clone + Debug + Hash + Eq,
238 V: Into<Bytes> + Clone + Debug,
239{
240 fn from(map: HashMap<K, V>) -> Bytes {
241 let mut bytes: Vec<u8> = vec![];
242 bytes.extend_from_slice(to_int(map.len() as i32).as_slice());
243 bytes = map.iter().fold(bytes, |mut acc, (k, v)| {
244 let key_bytes: Bytes = k.clone().into();
245 let val_bytes: Bytes = v.clone().into();
246 acc.extend_from_slice(Value::new_normal(key_bytes).into_cbytes().as_slice());
247 acc.extend_from_slice(Value::new_normal(val_bytes).into_cbytes().as_slice());
248 acc
249 });
250 Bytes(bytes)
251 }
252}
253
254#[cfg(test)]
255mod tests {
256
257 use super::*;
258 use crate::frame::traits::IntoBytes;
259
260 #[test]
261 fn test_value_type_into_cbytes() {
262 let normal_type = ValueType::Normal(1);
264 assert_eq!(normal_type.into_cbytes(), vec![0, 0, 0, 1]);
265 let null_type = ValueType::Null;
267 assert_eq!(null_type.into_cbytes(), vec![255, 255, 255, 255]);
268 let not_set = ValueType::NotSet;
270 assert_eq!(not_set.into_cbytes(), vec![255, 255, 255, 254])
271 }
272
273 #[test]
274 fn test_new_normal_value() {
275 let plain_value = "hello";
276 let len = plain_value.len() as i32;
277 let normal_value = Value::new_normal(plain_value);
278 assert_eq!(normal_value.body, b"hello");
279 match normal_value.value_type {
280 ValueType::Normal(l) => assert_eq!(l, len),
281 _ => unreachable!(),
282 }
283 }
284
285 #[test]
286 fn test_new_normal_value_all_types() {
287 let _ = Value::new_normal("hello");
288 let _ = Value::new_normal("hello".to_string());
289 let _ = Value::new_normal(1 as u8);
290 let _ = Value::new_normal(1 as u16);
291 let _ = Value::new_normal(1 as u32);
292 let _ = Value::new_normal(1 as u64);
293 let _ = Value::new_normal(1 as i8);
294 let _ = Value::new_normal(1 as i16);
295 let _ = Value::new_normal(1 as i32);
296 let _ = Value::new_normal(1 as i64);
297 let _ = Value::new_normal(true);
298 }
299
300 #[test]
301 fn test_new_null_value() {
302 let null_value = Value::new_null();
303 assert_eq!(null_value.body, vec![]);
304 match null_value.value_type {
305 ValueType::Null => assert!(true),
306 _ => unreachable!(),
307 }
308 }
309
310 #[test]
311 fn test_new_not_set_value() {
312 let not_set_value = Value::new_not_set();
313 assert_eq!(not_set_value.body, vec![]);
314 match not_set_value.value_type {
315 ValueType::NotSet => assert!(true),
316 _ => unreachable!(),
317 }
318 }
319
320 #[test]
321 fn test_value_into_cbytes() {
322 let value = Value::new_normal(1 as u8);
323 assert_eq!(value.into_cbytes(), vec![0, 0, 0, 1, 1]);
324 }
325}