1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
pub const LONG_STR_LEN: usize = 4;
pub const SHORT_LEN: usize = 2;
pub const INT_LEN: usize = 4;
pub const UUID_LEN: usize = 16;
use std::io;
use std::io::{Cursor, Read};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt, ByteOrder};
use {FromBytes, IntoBytes, FromCursor};
use error::{Result as CDRSResult};
pub mod data_serialization_types;
pub mod list;
pub mod map;
pub mod rows;
pub mod udt;
pub mod value;
pub trait AsRust<T> {
fn as_rust(&self) -> CDRSResult<T>;
}
pub trait IntoRustByName<R> {
fn get_by_name(&self, name: &str) -> Option<CDRSResult<R>>;
}
pub fn try_to_n_bytes(int: u64, n: usize) -> io::Result<Vec<u8>> {
let mut bytes = vec![];
try!(bytes.write_uint::<BigEndian>(int, n));
return Ok(bytes);
}
pub fn to_n_bytes(int: u64, n: usize) -> Vec<u8> {
return try_to_n_bytes(int, n).unwrap();
}
pub fn try_i_to_n_bytes(int: i64, n: usize) -> io::Result<Vec<u8>> {
let mut bytes = Vec::with_capacity(n);
unsafe {
bytes.set_len(n);
}
BigEndian::write_int(&mut bytes, int, n);
return Ok(bytes);
}
pub fn i_to_n_bytes(int: i64, n: usize) -> Vec<u8> {
return try_i_to_n_bytes(int, n).unwrap();
}
pub fn try_from_bytes(bytes: Vec<u8>) -> Result<u64, io::Error> {
let mut c = Cursor::new(bytes.clone());
return c.read_uint::<BigEndian>(bytes.len());
}
pub fn try_u16_from_bytes(bytes: Vec<u8>) -> Result<u16, io::Error> {
let mut c = Cursor::new(bytes.clone());
return c.read_u16::<BigEndian>();
}
pub fn try_i_from_bytes(bytes: Vec<u8>) -> Result<i64, io::Error> {
let mut c = Cursor::new(bytes.clone());
return c.read_int::<BigEndian>(bytes.len());
}
pub fn try_i32_from_bytes(bytes: Vec<u8>) -> Result<i32, io::Error> {
let mut c = Cursor::new(bytes.clone());
return c.read_i32::<BigEndian>();
}
pub fn try_f32_from_bytes(bytes: Vec<u8>) -> Result<f32, io::Error> {
let mut c = Cursor::new(bytes.clone());
return c.read_f32::<BigEndian>();
}
pub fn try_f64_from_bytes(bytes: Vec<u8>) -> Result<f64, io::Error> {
let mut c = Cursor::new(bytes.clone());
return c.read_f64::<BigEndian>();
}
pub fn from_bytes(bytes: Vec<u8>) -> u64 {
return try_from_bytes(bytes).unwrap();
}
pub fn from_i_bytes(bytes: Vec<u8>) -> i64 {
return try_i_from_bytes(bytes).unwrap();
}
pub fn from_u16_bytes(bytes: Vec<u8>) -> u16 {
return try_u16_from_bytes(bytes).unwrap();
}
pub fn to_short(int: u64) -> Vec<u8> {
return to_n_bytes(int, SHORT_LEN);
}
pub fn to_int(int: i64) -> Vec<u8> {
return i_to_n_bytes(int, INT_LEN);
}
#[derive(Debug, Clone)]
pub struct CString {
string: String
}
impl CString {
pub fn new(string: String) -> CString {
return CString { string: string };
}
pub fn as_str<'a>(&'a self) -> &'a str {
return self.string.as_str();
}
pub fn into_plain(self) -> String {
return self.string;
}
pub fn as_plain(&self) -> String {
return self.string.clone();
}
}
impl IntoBytes for CString {
fn into_cbytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = vec![];
let l = self.string.len() as u64;
v.extend_from_slice(to_short(l).as_slice());
v.extend_from_slice(self.string.as_bytes());
return v;
}
}
impl FromCursor for CString {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CString {
let len_bytes = cursor_next_value(&mut cursor, SHORT_LEN as u64);
let len: u64 = from_bytes(len_bytes.to_vec());
let body_bytes = cursor_next_value(&mut cursor, len);
return CString { string: String::from_utf8(body_bytes).unwrap() };
}
}
#[derive(Debug, Clone)]
pub struct CStringLong {
string: String
}
impl CStringLong {
pub fn new(string: String) -> CStringLong {
return CStringLong { string: string };
}
pub fn as_str<'a>(&'a self) -> &'a str {
return self.string.as_str();
}
pub fn into_plain(self) -> String {
return self.string;
}
}
impl IntoBytes for CStringLong {
fn into_cbytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = vec![];
let l = self.string.len() as i64;
v.extend_from_slice(to_int(l).as_slice());
v.extend_from_slice(self.string.as_bytes());
return v;
}
}
impl FromCursor for CStringLong {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CStringLong {
let len_bytes = cursor_next_value(&mut cursor, INT_LEN as u64);
let len: u64 = from_bytes(len_bytes.to_vec());
let body_bytes = cursor_next_value(&mut cursor, len);
return CStringLong { string: String::from_utf8(body_bytes).unwrap() };
}
}
#[derive(Debug, Clone)]
pub struct CStringList {
list: Vec<CString>
}
impl CStringList {
pub fn into_plain(self) -> Vec<String> {
return self.list
.iter()
.map(|string| string.clone().into_plain())
.collect();
}
}
impl FromCursor for CStringList {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CStringList {
let mut len_bytes = [0; SHORT_LEN];
if let Err(err) = cursor.read(&mut len_bytes) {
error!("Read Cassandra bytes error: {}", err);
panic!(err);
}
let len: u64 = from_bytes(len_bytes.to_vec());
let list = (0..len).map(|_| CString::from_cursor(&mut cursor)).collect();
return CStringList { list: list };
}
}
#[derive(Debug, Clone)]
pub struct CBytes {
bytes: Vec<u8>
}
impl CBytes {
pub fn new(bytes: Vec<u8>) -> CBytes {
return CBytes { bytes: bytes };
}
pub fn into_plain(self) -> Vec<u8> {
return self.bytes;
}
pub fn as_plain(&self) -> Vec<u8> {
return self.bytes.clone();
}
}
impl FromCursor for CBytes {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CBytes {
let len: u64 = CInt::from_cursor(&mut cursor) as u64;
return CBytes { bytes: cursor_next_value(&mut cursor, len) };
}
}
impl IntoBytes for CBytes {
fn into_cbytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = vec![];
let l = self.bytes.len() as i64;
v.extend_from_slice(to_int(l).as_slice());
v.extend_from_slice(self.bytes.as_slice());
return v;
}
}
#[derive(Debug, Clone)]
pub struct CBytesShort {
bytes: Vec<u8>
}
impl CBytesShort {
pub fn new(bytes: Vec<u8>) -> CBytesShort {
return CBytesShort { bytes: bytes };
}
pub fn into_plain(self) -> Vec<u8> {
return self.bytes;
}
}
impl FromCursor for CBytesShort {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CBytesShort {
let len: u64 = CIntShort::from_cursor(&mut cursor) as u64;
return CBytesShort { bytes: cursor_next_value(&mut cursor, len) };
}
}
impl IntoBytes for CBytesShort {
fn into_cbytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = vec![];
let l = self.bytes.len() as u64;
v.extend_from_slice(to_short(l).as_slice());
v.extend_from_slice(self.bytes.as_slice());
return v;
}
}
pub type CInt = i32;
impl FromCursor for CInt {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CInt {
let bytes = cursor_next_value(&mut cursor, INT_LEN as u64);
return from_bytes(bytes) as CInt;
}
}
pub type CIntShort = i16;
impl FromCursor for CIntShort {
fn from_cursor(mut cursor: &mut Cursor<Vec<u8>>) -> CIntShort {
let bytes = cursor_next_value(&mut cursor, SHORT_LEN as u64);
return from_bytes(bytes) as CIntShort;
}
}
impl FromBytes for Vec<u8> {
fn from_bytes(bytes: Vec<u8>) -> Vec<u8> {
let mut cursor = Cursor::new(bytes);
let len_bytes = cursor_next_value(&mut cursor, SHORT_LEN as u64);
let len: u64 = from_bytes(len_bytes);
return cursor_next_value(&mut cursor, len);
}
}
pub fn cursor_next_value(mut cursor: &mut Cursor<Vec<u8>>, len: u64) -> Vec<u8> {
let l = len as usize;
let current_position = cursor.position();
let mut buff: Vec<u8> = Vec::with_capacity(l);
unsafe {
buff.set_len(l);
}
if let Err(err) = cursor.read(&mut buff) {
error!("Read from cursor error: {}", err);
panic!(err);
}
cursor.set_position(current_position + len);
return buff;
}