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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
use std::collections::HashMap;
use std::u32;
use std::u16;
use std::convert::{TryFrom, TryInto};
use bytes::{Bytes, BufMut, Buf};
use uuid::Uuid;
use snafu::{OptionExt, ensure};
use crate::features::ProtocolVersion;
use crate::errors::{self, EncodeError, DecodeError};
use crate::encoding::{Input, Output, Headers, Decode, Encode};
use crate::descriptors::{OutputTypedesc, InputTypedesc, Descriptor, TypePos};
pub use crate::common::Cardinality;
use crate::common::Capabilities;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ServerMessage {
ServerHandshake(ServerHandshake),
UnknownMessage(u8, Bytes),
LogMessage(LogMessage),
ErrorResponse(ErrorResponse),
Authentication(Authentication),
ReadyForCommand(ReadyForCommand),
ServerKeyData(ServerKeyData),
ParameterStatus(ParameterStatus),
CommandComplete(CommandComplete),
PrepareComplete(PrepareComplete),
CommandDataDescription(CommandDataDescription),
Data(Data),
RestoreReady(RestoreReady),
DumpHeader(RawPacket),
DumpBlock(RawPacket),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadyForCommand {
pub headers: Headers,
pub transaction_state: TransactionState,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Authentication {
Ok,
Sasl { methods: Vec<String> },
SaslContinue { data: Bytes },
SaslFinal { data: Bytes },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorSeverity {
Error,
Fatal,
Panic,
Unknown(u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageSeverity {
Debug,
Info,
Notice,
Warning,
Unknown(u8),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionState {
NotInTransaction = 0x49,
InTransaction = 0x54,
InFailedTransaction = 0x45
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorResponse {
pub severity: ErrorSeverity,
pub code: u32,
pub message: String,
pub attributes: Headers,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LogMessage {
pub severity: MessageSeverity,
pub code: u32,
pub text: String,
pub attributes: Headers,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerHandshake {
pub major_ver: u16,
pub minor_ver: u16,
pub extensions: HashMap<String, Headers>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerKeyData {
pub data: [u8; 32],
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParameterStatus {
pub proto: ProtocolVersion,
pub name: Bytes,
pub value: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandComplete {
pub headers: Headers,
pub status_data: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrepareComplete {
pub headers: Headers,
pub cardinality: Cardinality,
pub input_typedesc_id: Uuid,
pub output_typedesc_id: Uuid,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandDataDescription {
pub proto: ProtocolVersion,
pub headers: Headers,
pub result_cardinality: Cardinality,
pub input_typedesc_id: Uuid,
pub input_typedesc: Bytes,
pub output_typedesc_id: Uuid,
pub output_typedesc: Bytes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Data {
pub data: Vec<Bytes>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RestoreReady {
pub headers: Headers,
pub jobs: u16,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawPacket {
pub data: Bytes,
}
fn encode<T: Encode>(buf: &mut Output, code: u8, msg: &T)
-> Result<(), EncodeError>
{
buf.reserve(5);
buf.put_u8(code);
let base = buf.len();
buf.put_slice(&[0; 4]);
msg.encode(buf)?;
let size = u32::try_from(buf.len() - base).ok()
.context(errors::MessageTooLong)?;
buf[base..base+4].copy_from_slice(&size.to_be_bytes()[..]);
Ok(())
}
impl CommandDataDescription {
pub fn output(&self) -> Result<OutputTypedesc, DecodeError> {
let ref mut cur = Input::new(
self.proto.clone(),
self.output_typedesc.clone(),
);
OutputTypedesc::decode_with_id(self.output_typedesc_id.clone(), cur)
}
pub fn input(&self) -> Result<InputTypedesc, DecodeError> {
let ref mut cur = Input::new(
self.proto.clone(),
self.input_typedesc.clone(),
);
let mut descriptors = Vec::new();
while cur.remaining() > 0 {
match Descriptor::decode(cur)? {
Descriptor::TypeAnnotation(_) => {}
item => descriptors.push(item),
}
}
let root_id = self.input_typedesc_id.clone();
let root_pos = if root_id == Uuid::from_u128(0) {
None
} else {
let idx = descriptors.iter().position(|x| *x.id() == root_id)
.context(errors::UuidNotFound { uuid: root_id })?;
let pos = idx.try_into().ok()
.context(errors::TooManyDescriptors { index: idx })?;
Some(TypePos(pos))
};
Ok(InputTypedesc {
array: descriptors,
proto: self.proto.clone(),
root_id,
root_pos,
})
}
}
impl ParameterStatus {
pub fn parse_system_config(self) -> Result<(OutputTypedesc, Bytes), DecodeError> {
let ref mut cur = Input::new(
self.proto.clone(),
self.value,
);
let typedesc_data = Bytes::decode(cur)?;
let data = Bytes::decode(cur)?;
let ref mut typedesc_buf = Input::new(
self.proto,
typedesc_data,
);
let typedesc_id = Uuid::decode(typedesc_buf)?;
let typedesc = OutputTypedesc::decode_with_id(typedesc_id, typedesc_buf)?;
Ok((typedesc, data))
}
}
impl ServerMessage {
pub fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
use ServerMessage::*;
match self {
ServerHandshake(h) => encode(buf, 0x76, h),
ErrorResponse(h) => encode(buf, 0x45, h),
LogMessage(h) => encode(buf, 0x4c, h),
Authentication(h) => encode(buf, 0x52, h),
ReadyForCommand(h) => encode(buf, 0x5a, h),
ServerKeyData(h) => encode(buf, 0x4b, h),
ParameterStatus(h) => encode(buf, 0x53, h),
CommandComplete(h) => encode(buf, 0x43, h),
PrepareComplete(h) => encode(buf, 0x31, h),
CommandDataDescription(h) => encode(buf, 0x54, h),
Data(h) => encode(buf, 0x44, h),
RestoreReady(h) => encode(buf, 0x2b, h),
DumpHeader(h) => encode(buf, 0x40, h),
DumpBlock(h) => encode(buf, 0x3d, h),
UnknownMessage(_, _) => {
errors::UnknownMessageCantBeEncoded.fail()?
}
}
}
pub fn decode(buf: &mut Input) -> Result<ServerMessage, DecodeError> {
use self::ServerMessage as M;
let ref mut data = buf.slice(5..);
match buf[0] {
0x76 => ServerHandshake::decode(data).map(M::ServerHandshake),
0x45 => ErrorResponse::decode(data).map(M::ErrorResponse),
0x4c => LogMessage::decode(data).map(M::LogMessage),
0x52 => Authentication::decode(data).map(M::Authentication),
0x5a => ReadyForCommand::decode(data).map(M::ReadyForCommand),
0x4b => ServerKeyData::decode(data).map(M::ServerKeyData),
0x53 => ParameterStatus::decode(data).map(M::ParameterStatus),
0x43 => CommandComplete::decode(data).map(M::CommandComplete),
0x31 => PrepareComplete::decode(data).map(M::PrepareComplete),
0x44 => Data::decode(data).map(M::Data),
0x2b => RestoreReady::decode(data).map(M::RestoreReady),
0x40 => RawPacket::decode(data).map(M::DumpHeader),
0x3d => RawPacket::decode(data).map(M::DumpBlock),
0x54 => {
CommandDataDescription::decode(data)
.map(M::CommandDataDescription)
}
code => {
Ok(M::UnknownMessage(
code,
data.copy_to_bytes(data.remaining())
))
}
}
}
}
impl Encode for ServerHandshake {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(6);
buf.put_u16(self.major_ver);
buf.put_u16(self.minor_ver);
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 ServerHandshake {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 6, errors::Underflow);
let major_ver = buf.get_u16();
let minor_ver = buf.get_u16();
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 {
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
extensions.insert(name, headers);
}
Ok(ServerHandshake {
major_ver, minor_ver, extensions,
})
}
}
impl Encode for ErrorResponse {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(11);
buf.put_u8(self.severity.to_u8());
buf.put_u32(self.code);
self.message.encode(buf)?;
buf.reserve(2);
buf.put_u16(u16::try_from(self.attributes.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in &self.attributes {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
Ok(())
}
}
impl Decode for ErrorResponse {
fn decode(buf: &mut Input) -> Result<ErrorResponse, DecodeError> {
ensure!(buf.remaining() >= 11, errors::Underflow);
let severity = ErrorSeverity::from_u8(buf.get_u8());
let code = buf.get_u32();
let message = String::decode(buf)?;
ensure!(buf.remaining() >= 2, errors::Underflow);
let num_attributes = buf.get_u16();
let mut attributes = HashMap::new();
for _ in 0..num_attributes {
ensure!(buf.remaining() >= 4, errors::Underflow);
attributes.insert(buf.get_u16(), Bytes::decode(buf)?);
}
return Ok(ErrorResponse {
severity, code, message, attributes,
})
}
}
impl Encode for LogMessage {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(11);
buf.put_u8(self.severity.to_u8());
buf.put_u32(self.code);
self.text.encode(buf)?;
buf.reserve(2);
buf.put_u16(u16::try_from(self.attributes.len()).ok()
.context(errors::TooManyHeaders)?);
for (&name, value) in &self.attributes {
buf.reserve(2);
buf.put_u16(name);
value.encode(buf)?;
}
Ok(())
}
}
impl Decode for LogMessage {
fn decode(buf: &mut Input) -> Result<LogMessage, DecodeError> {
ensure!(buf.remaining() >= 11, errors::Underflow);
let severity = MessageSeverity::from_u8(buf.get_u8());
let code = buf.get_u32();
let text = String::decode(buf)?;
ensure!(buf.remaining() >= 2, errors::Underflow);
let num_attributes = buf.get_u16();
let mut attributes = HashMap::new();
for _ in 0..num_attributes {
ensure!(buf.remaining() >= 4, errors::Underflow);
attributes.insert(buf.get_u16(), Bytes::decode(buf)?);
}
return Ok(LogMessage {
severity, code, text, attributes,
})
}
}
impl Encode for Authentication {
fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
use Authentication as A;
buf.reserve(1);
match self {
A::Ok => buf.put_u32(0),
A::Sasl { methods } => {
buf.put_u32(0x0A);
buf.reserve(4);
buf.put_u32(methods.len().try_into()
.ok().context(errors::TooManyMethods)?);
for meth in methods {
meth.encode(buf)?;
}
}
A::SaslContinue { data } => {
buf.put_u32(0x0B);
data.encode(buf)?;
}
A::SaslFinal { data } => {
buf.put_u32(0x0C);
data.encode(buf)?;
}
}
Ok(())
}
}
impl Decode for Authentication {
fn decode(buf: &mut Input) -> Result<Authentication, DecodeError> {
ensure!(buf.remaining() >= 4, errors::Underflow);
match buf.get_u32() {
0x00 => Ok(Authentication::Ok),
0x0A => {
ensure!(buf.remaining() >= 4, errors::Underflow);
let num_methods = buf.get_u32() as usize;
let mut methods = Vec::with_capacity(num_methods);
for _ in 0..num_methods {
methods.push(String::decode(buf)?);
}
Ok(Authentication::Sasl { methods })
}
0x0B => {
let data = Bytes::decode(buf)?;
Ok(Authentication::SaslContinue { data })
}
0x0C => {
let data = Bytes::decode(buf)?;
Ok(Authentication::SaslFinal { data })
}
c => errors::AuthStatusInvalid { auth_status: c }.fail()?,
}
}
}
impl Encode for ReadyForCommand {
fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
buf.reserve(3);
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(1);
buf.put_u8(self.transaction_state as u8);
Ok(())
}
}
impl Decode for ReadyForCommand {
fn decode(buf: &mut Input)
-> Result<ReadyForCommand, DecodeError>
{
use TransactionState::*;
ensure!(buf.remaining() >= 3, errors::Underflow);
let mut headers = HashMap::new();
let num_headers = buf.get_u16();
for _ in 0..num_headers {
ensure!(buf.remaining() >= 4, errors::Underflow);
headers.insert(buf.get_u16(), Bytes::decode(buf)?);
}
ensure!(buf.remaining() >= 1, errors::Underflow);
let transaction_state = match buf.get_u8() {
0x49 => NotInTransaction,
0x54 => InTransaction,
0x45 => InFailedTransaction,
s => {
errors::InvalidTransactionState {
transaction_state: s
}.fail()?
}
};
Ok(ReadyForCommand { headers, transaction_state })
}
}
impl ErrorSeverity {
pub fn from_u8(code: u8) -> ErrorSeverity {
use ErrorSeverity::*;
match code {
120 => Error,
200 => Fatal,
255 => Panic,
_ => Unknown(code),
}
}
pub fn to_u8(&self) -> u8 {
use ErrorSeverity::*;
match *self {
Error => 120,
Fatal => 200,
Panic => 255,
Unknown(code) => code,
}
}
}
impl MessageSeverity {
fn from_u8(code: u8) -> MessageSeverity {
use MessageSeverity::*;
match code {
20 => Debug,
40 => Info,
60 => Notice,
80 => Warning,
_ => Unknown(code),
}
}
fn to_u8(&self) -> u8 {
use MessageSeverity::*;
match *self {
Debug => 20,
Info => 40,
Notice => 60,
Warning => 80,
Unknown(code) => code,
}
}
}
impl Encode for ServerKeyData {
fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
buf.extend(&self.data[..]);
Ok(())
}
}
impl Decode for ServerKeyData {
fn decode(buf: &mut Input)
-> Result<ServerKeyData, DecodeError>
{
ensure!(buf.remaining() >= 32, errors::Underflow);
let mut data = [0u8; 32];
buf.copy_to_slice(&mut data[..]);
Ok(ServerKeyData { data })
}
}
impl Encode for ParameterStatus {
fn encode(&self, buf: &mut Output) -> Result<(), EncodeError> {
self.name.encode(buf)?;
self.value.encode(buf)?;
Ok(())
}
}
impl Decode for ParameterStatus {
fn decode(buf: &mut Input)
-> Result<ParameterStatus, DecodeError>
{
let name = Bytes::decode(buf)?;
let value = Bytes::decode(buf)?;
Ok(ParameterStatus { proto: buf.proto().clone(), name, value })
}
}
impl Encode for CommandComplete {
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.status_data.encode(buf)?;
Ok(())
}
}
impl Decode for CommandComplete {
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 status_data = Bytes::decode(buf)?;
Ok(CommandComplete { status_data, headers })
}
}
impl Encode for PrepareComplete {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(35);
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(33);
buf.put_u8(self.cardinality as u8);
self.input_typedesc_id.encode(buf)?;
self.output_typedesc_id.encode(buf)?;
Ok(())
}
}
impl Decode for PrepareComplete {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 35, 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() >= 33, errors::Underflow);
let cardinality = TryFrom::try_from(buf.get_u8())?;
let input_typedesc_id = Uuid::decode(buf)?;
let output_typedesc_id = Uuid::decode(buf)?;
Ok(PrepareComplete {
headers,
cardinality,
input_typedesc_id,
output_typedesc_id,
})
}
}
impl Encode for CommandDataDescription {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(43);
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(41);
buf.put_u8(self.result_cardinality as u8);
self.input_typedesc_id.encode(buf)?;
self.input_typedesc.encode(buf)?;
self.output_typedesc_id.encode(buf)?;
self.output_typedesc.encode(buf)?;
Ok(())
}
}
impl Decode for CommandDataDescription {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 43, 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() >= 41, errors::Underflow);
let result_cardinality = TryFrom::try_from(buf.get_u8())?;
let input_typedesc_id = Uuid::decode(buf)?;
let input_typedesc = Bytes::decode(buf)?;
let output_typedesc_id = Uuid::decode(buf)?;
let output_typedesc = Bytes::decode(buf)?;
Ok(CommandDataDescription {
proto: buf.proto().clone(),
headers,
result_cardinality,
input_typedesc_id,
input_typedesc,
output_typedesc_id,
output_typedesc,
})
}
}
impl Encode for Data {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(2);
buf.put_u16(u16::try_from(self.data.len()).ok()
.context(errors::TooManyHeaders)?);
for chunk in &self.data {
chunk.encode(buf)?;
}
Ok(())
}
}
impl Decode for Data {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
ensure!(buf.remaining() >= 2, errors::Underflow);
let num_chunks = buf.get_u16() as usize;
let mut data = Vec::with_capacity(num_chunks);
for _ in 0..num_chunks {
data.push(Bytes::decode(buf)?);
}
return Ok(Data { data })
}
}
impl Encode for RestoreReady {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.reserve(4);
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(2);
buf.put_u16(self.jobs);
Ok(())
}
}
impl Decode for RestoreReady {
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)?);
}
ensure!(buf.remaining() >= 2, errors::Underflow);
let jobs = buf.get_u16();
return Ok(RestoreReady { jobs, headers })
}
}
impl Encode for RawPacket {
fn encode(&self, buf: &mut Output)
-> Result<(), EncodeError>
{
buf.extend(&self.data);
Ok(())
}
}
impl Decode for RawPacket {
fn decode(buf: &mut Input) -> Result<Self, DecodeError> {
return Ok(RawPacket { data: buf.copy_to_bytes(buf.remaining()) })
}
}
impl PrepareComplete {
pub fn get_capabilities(&self) -> Option<Capabilities> {
self.headers.get(&0x1001).and_then(|bytes| {
if bytes.len() == 8 {
let mut array = [0u8; 8];
array.copy_from_slice(bytes);
Capabilities::from_bits(u64::from_be_bytes(array))
} else {
None
}
})
}
}