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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::collections::HashMap;
use std::convert::TryFrom;
use std::u16;
use bytes::{Bytes, BufMut, Buf};
use snafu::{OptionExt, ensure};
use crate::encoding::{Encode, Decode, Headers, encode, Input, Output};
use crate::errors::{self, EncodeError, DecodeError};
pub use crate::common::Cardinality;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClientMessage {
ClientHandshake(ClientHandshake),
ExecuteScript(ExecuteScript),
Prepare(Prepare),
DescribeStatement(DescribeStatement),
Execute(Execute),
UnknownMessage(u8, Bytes),
AuthenticationSaslInitialResponse(SaslInitialResponse),
AuthenticationSaslResponse(SaslResponse),
Dump(Dump),
Restore(Restore),
RestoreBlock(RestoreBlock),
RestoreEof,
Sync,
Flush,
Terminate,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SaslInitialResponse {
pub method: String,
pub data: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SaslResponse {
pub data: Bytes
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientHandshake {
pub major_ver: u16,
pub minor_ver: u16,
pub params: HashMap<String, String>,
pub extensions: HashMap<String, Headers>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecuteScript {
pub headers: Headers,
pub script_text: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Prepare {
pub headers: Headers,
pub io_format: IoFormat,
pub expected_cardinality: Cardinality,
pub statement_name: Bytes,
pub command_text: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DescribeStatement {
pub headers: Headers,
pub aspect: DescribeAspect,
pub statement_name: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Execute {
pub headers: Headers,
pub statement_name: Bytes,
pub arguments: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dump {
pub headers: Headers,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Restore {
pub headers: Headers,
pub jobs: u16,
pub data: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RestoreBlock {
pub data: Bytes,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DescribeAspect {
DataDescription = 0x54,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum IoFormat {
Binary = 0x62,
Json = 0x6a,
JsonElements = 0x4a,
}
struct Empty;
impl ClientMessage {
pub fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
use ClientMessage::*;
match self {
ClientHandshake(h) => encode(buf, 0x56, h),
AuthenticationSaslInitialResponse(h) => encode(buf, 0x70, h),
AuthenticationSaslResponse(h) => encode(buf, 0x72, h),
ExecuteScript(h) => encode(buf, 0x51, h),
Prepare(h) => encode(buf, 0x50, h),
DescribeStatement(h) => encode(buf, 0x44, h),
Execute(h) => encode(buf, 0x45, h),
Dump(h) => encode(buf, 0x3e, h),
Restore(h) => encode(buf, 0x3c, h),
RestoreBlock(h) => encode(buf, 0x3d, h),
RestoreEof => encode(buf, 0x2e, &Empty),
Sync => encode(buf, 0x53, &Empty),
Flush => encode(buf, 0x48, &Empty),
Terminate => encode(buf, 0x58, &Empty),
UnknownMessage(_, _) => {
errors::UnknownMessageCantBeEncoded.fail()?
}
}
}
pub fn decode(buf: &mut Input) -> Result<ClientMessage, DecodeError> {
use self::ClientMessage as M;
let mut data = buf.slice(5..);
match buf[0] {
0x56 => ClientHandshake::decode(&mut data).map(M::ClientHandshake),
0x70 => SaslInitialResponse::decode(&mut data)
.map(M::AuthenticationSaslInitialResponse),
0x72 => SaslResponse::decode(&mut data)
.map(M::AuthenticationSaslResponse),
0x51 => ExecuteScript::decode(&mut data).map(M::ExecuteScript),
0x50 => Prepare::decode(&mut data).map(M::Prepare),
0x45 => Execute::decode(&mut data).map(M::Execute),
0x3e => Dump::decode(&mut data).map(M::Dump),
0x3c => Restore::decode(&mut data).map(M::Restore),
0x3d => RestoreBlock::decode(&mut data).map(M::RestoreBlock),
0x2e => Ok(M::RestoreEof),
0x53 => Ok(M::Sync),
0x48 => Ok(M::Flush),
0x58 => Ok(M::Terminate),
0x44 => {
DescribeStatement::decode(&mut data).map(M::DescribeStatement)
}
code => Ok(M::UnknownMessage(
code,
data.copy_to_bytes(data.remaining()),
)),
}
}
}
impl Encode for Empty {
fn encode(&self, _buf: &mut Output)
-> Result<(), EncodeError>
{
Ok(())
}
}
impl Encode for ClientHandshake {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(8);
buf.put_u16(self.major_ver);
buf.put_u16(self.minor_ver);
buf.put_u16(u16::try_from(self.params.len()).ok()
.context(errors::TooManyParams)?);
for (k, v) in &self.params {
k.encode(buf)?;
v.encode(buf)?;
}
buf.reserve(2);
buf.put_u16(u16::try_from(self.extensions.len()).ok()
.context(errors::TooManyExtensions)?);
for (name, headers) in &self.extensions {
name.encode(buf)?;
buf.reserve(2);
buf.put_u16(u16::try_from(headers.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in headers {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
}
Ok(())
}
}
impl Decode for ClientHandshake {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 8, errors::Underflow);
let major_ver = buf.get_u16();
let minor_ver = buf.get_u16();
let num_params = buf.get_u16();
let mut params = HashMap::new();
for _ in 0..num_params {
params.insert(String::decode(buf)?, String::decode(buf)?);
}
ensure!(buf.remaining() >= 2, errors::Underflow);
let num_ext = buf.get_u16();
let mut extensions = HashMap::new();
for _ in 0..num_ext {
let name = String::decode(buf)?;
ensure!(buf.remaining() >= 2, errors::Underflow);
let num_headers = buf.get_u16();
let mut headers = HashMap::new();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
extensions.insert(name, headers);
}
Ok(ClientHandshake {
major_ver, minor_ver, params, extensions,
})
}
}
impl Encode for SaslInitialResponse {
fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
self.method.encode(buf)?;
self.data.encode(buf)?;
Ok(())
}
}
impl Decode for SaslInitialResponse {
fn decode(buf: &mut Input)
-> Result<SaslInitialResponse, DecodeError>
{
let method = String::decode(buf)?;
let data = Bytes::decode(buf)?;
Ok(SaslInitialResponse { method, data })
}
}
impl Encode for SaslResponse {
fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
self.data.encode(buf)?;
Ok(())
}
}
impl Decode for SaslResponse {
fn decode(buf: &mut Input) -> Result<SaslResponse, DecodeError> {
let data = Bytes::decode(buf)?;
Ok(SaslResponse { data })
}
}
impl Encode for ExecuteScript {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(6);
buf.put_u16(u16::try_from(self.headers.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in &self.headers {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
self.script_text.encode(buf)?;
Ok(())
}
}
impl Decode for ExecuteScript {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 6, errors::Underflow);
let num_headers = buf.get_u16();
let mut headers = HashMap::new();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
let script_text = String::decode(buf)?;
Ok(ExecuteScript { script_text, headers })
}
}
impl Encode for Prepare {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(12);
buf.put_u16(u16::try_from(self.headers.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in &self.headers {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
buf.reserve(10);
buf.put_u8(self.io_format as u8);
buf.put_u8(self.expected_cardinality as u8);
self.statement_name.encode(buf)?;
self.command_text.encode(buf)?;
Ok(())
}
}
impl Decode for Prepare {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 12, errors::Underflow);
let num_headers = buf.get_u16();
let mut headers = HashMap::new();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
ensure!(buf.remaining() >= 8, errors::Underflow);
let io_format = match buf.get_u8() {
0x62 => IoFormat::Binary,
0x6a => IoFormat::Json,
0x4a => IoFormat::JsonElements,
c => errors::InvalidIoFormat { io_format: c }.fail()?,
};
let expected_cardinality = TryFrom::try_from(buf.get_u8())?;
let statement_name = Bytes::decode(buf)?;
let command_text = String::decode(buf)?;
Ok(Prepare {
headers,
io_format,
expected_cardinality,
statement_name,
command_text,
})
}
}
impl Encode for DescribeStatement {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(7);
buf.put_u16(u16::try_from(self.headers.len()).ok()
.context(errors::TooManyHeaders)?);
buf.reserve(5);
buf.put_u8(self.aspect as u8);
self.statement_name.encode(buf)?;
Ok(())
}
}
impl Decode for DescribeStatement {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 12, errors::Underflow);
let num_headers = buf.get_u16();
let mut headers = HashMap::new();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
ensure!(buf.remaining() >= 8, errors::Underflow);
let aspect = match buf.get_u8() {
0x54 => DescribeAspect::DataDescription,
c => errors::InvalidAspect { aspect: c }.fail()?,
};
let statement_name = Bytes::decode(buf)?;
Ok(DescribeStatement {
headers,
aspect,
statement_name,
})
}
}
impl Encode for Execute {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(10);
buf.put_u16(u16::try_from(self.headers.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in &self.headers {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
self.statement_name.encode(buf)?;
self.arguments.encode(buf)?;
Ok(())
}
}
impl Decode for Execute {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 12, errors::Underflow);
let num_headers = buf.get_u16();
let mut headers = HashMap::new();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
let statement_name = Bytes::decode(buf)?;
let arguments = Bytes::decode(buf)?;
Ok(Execute {
headers,
statement_name,
arguments,
})
}
}
impl Encode for Dump {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(10);
buf.put_u16(u16::try_from(self.headers.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in &self.headers {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
Ok(())
}
}
impl Decode for Dump {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 12, errors::Underflow);
let num_headers = buf.get_u16();
let mut headers = HashMap::new();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
Ok(Dump { headers })
}
}
impl Encode for Restore {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(4 + self.data.len());
buf.put_u16(u16::try_from(self.headers.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in &self.headers {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
buf.put_u16(self.jobs);
buf.extend(&self.data);
Ok(())
}
}
impl Decode for Restore {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 4, errors::Underflow);
let num_headers = buf.get_u16();
let mut headers = HashMap::new();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
let jobs = buf.get_u16();
let data = buf.copy_to_bytes(buf.remaining());
return Ok(Restore { jobs, headers, data })
}
}
impl Encode for RestoreBlock {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.extend(&self.data);
Ok(())
}
}
impl Decode for RestoreBlock {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
let data = buf.copy_to_bytes(buf.remaining());
return Ok(RestoreBlock { data })
}
}