postgres_proto_rs/messages/
backend.rs1use bytes::{Buf, BufMut, BytesMut};
2use std::{
3 io::{Cursor, Read},
4 mem,
5};
6
7use crate::messages::{BytesMutReader, Error, Message};
8
9#[derive(Debug)]
13pub enum BackendMessageType {
14 AuthenticationCleartextPassword(AuthenticationCleartextPassword),
15 AuthenticationMd5Password(AuthenticationMD5Password),
16 AuthenticationOk(AuthenticationOk),
17 AuthenticationSASL(AuthenticationSASL),
18 AuthenticationSASLContinue(AuthenticationSASLContinue),
19 AuthenticationSASLFinal(AuthenticationSASLFinal),
20 BackendKeyData(BackendKeyData),
21 BindComplete(BindComplete),
22 CloseComplete(CloseComplete),
23 CommandComplete(CommandComplete),
24 CopyData(CopyData),
25 CopyDone(CopyDone),
26 CopyInResponse(CopyInResponse),
27 CopyOutResponse(CopyOutResponse),
28 DataRow(DataRow),
29 EmptyQueryResponse(EmptyQueryResponse),
30 ErrorResponse(ErrorResponse),
31 NoData(NoData),
32 NoticeResponse(NoticeResponse),
33 NotificationResponse(NotificationResponse),
34 ParameterDescription(ParameterDescription),
35 ParameterStatus(ParameterStatus),
36 ParseComplete(ParseComplete),
37 PortalSuspended(PortalSuspended),
38 ReadyForQuery(ReadyForQuery),
39 RowDescription(RowDescription),
40 FunctionCallResponse(FunctionCallResponse),
41 CopyBothResponse(CopyBothResponse),
42}
43
44impl BackendMessageType {
45 pub fn get_bytes(&self) -> &BytesMut {
46 match self {
47 Self::AuthenticationCleartextPassword(ct_password) => ct_password.get_bytes(),
48 Self::AuthenticationMd5Password(md5_password) => md5_password.get_bytes(),
49 Self::AuthenticationOk(auth_ok) => auth_ok.get_bytes(),
50 Self::AuthenticationSASL(sasl) => sasl.get_bytes(),
51 Self::AuthenticationSASLContinue(sasl_cont) => sasl_cont.get_bytes(),
52 Self::AuthenticationSASLFinal(sasl_final) => sasl_final.get_bytes(),
53 Self::BackendKeyData(data) => data.get_bytes(),
54 Self::BindComplete(bind_complete) => bind_complete.get_bytes(),
55 Self::CloseComplete(close_complete) => close_complete.get_bytes(),
56 Self::CommandComplete(command_complete) => command_complete.get_bytes(),
57 Self::CopyData(copy_data) => copy_data.get_bytes(),
58 Self::CopyDone(copy_done) => copy_done.get_bytes(),
59 Self::CopyInResponse(copy_in_response) => copy_in_response.get_bytes(),
60 Self::CopyOutResponse(copy_out_response) => copy_out_response.get_bytes(),
61 Self::DataRow(row_data) => row_data.get_bytes(),
62 Self::EmptyQueryResponse(empty_query_response) => empty_query_response.get_bytes(),
63 Self::ErrorResponse(error_response) => error_response.get_bytes(),
64 Self::NoData(no_data) => no_data.get_bytes(),
65 Self::NoticeResponse(notice_response) => notice_response.get_bytes(),
66 Self::NotificationResponse(notif_resp) => notif_resp.get_bytes(),
67 Self::ParameterDescription(param_desc) => param_desc.get_bytes(),
68 Self::ParameterStatus(parameter_status) => parameter_status.get_bytes(),
69 Self::ParseComplete(parse_complete) => parse_complete.get_bytes(),
70 Self::PortalSuspended(portal_suspended) => portal_suspended.get_bytes(),
71 Self::ReadyForQuery(ready_for_query) => ready_for_query.get_bytes(),
72 Self::RowDescription(row_description) => row_description.get_bytes(),
73 Self::FunctionCallResponse(function_call_resp) => function_call_resp.get_bytes(),
74 Self::CopyBothResponse(copy_both_resp) => copy_both_resp.get_bytes(),
75 }
76 }
77
78 pub fn new_from_bytes(msg_type: u8, message_bytes: BytesMut) -> Result<Self, Error> {
79 match msg_type as char {
80 'R' => {
81 let auth_type = match message_bytes.get(5..9) {
82 Some(auth_type_bytes) => {
83 i32::from_be_bytes(match <[u8; 4]>::try_from(auth_type_bytes) {
84 Ok(auth_type) => auth_type,
85 Err(_) => return Err(Error::InvalidBytes),
86 })
87 }
88 None => return Err(Error::InvalidBytes),
89 };
90
91 match auth_type {
92 AUTH_CODE_OK => {
93 let auth_ok = AuthenticationOk::new_from_bytes(message_bytes)?;
94 Ok(Self::AuthenticationOk(auth_ok))
95 }
96 AUTH_CODE_CLEARTEXT_PASSWORD => {
97 let cleartext_password =
98 AuthenticationCleartextPassword::new_from_bytes(message_bytes)?;
99 Ok(Self::AuthenticationCleartextPassword(cleartext_password))
100 }
101 AUTH_CODE_MD5_PASSWORD => {
102 let md5_password =
103 AuthenticationMD5Password::new_from_bytes(message_bytes)?;
104 Ok(Self::AuthenticationMd5Password(md5_password))
105 }
106 AUTH_CODE_SASL => {
107 let sasl = AuthenticationSASL::new_from_bytes(message_bytes)?;
108 Ok(Self::AuthenticationSASL(sasl))
109 }
110 AUTH_CODE_SASL_CONTINUE => {
111 let sasl_cont = AuthenticationSASLContinue::new_from_bytes(message_bytes)?;
112 Ok(Self::AuthenticationSASLContinue(sasl_cont))
113 }
114 AUTH_CODE_SASL_FINAL => {
115 let sasl_final = AuthenticationSASLFinal::new_from_bytes(message_bytes)?;
116 Ok(Self::AuthenticationSASLFinal(sasl_final))
117 }
118 AUTH_CODE_SCM_CREDS | AUTH_CODE_GSS | AUTH_CODE_GSS_CONT | AUTH_CODE_SSPI => {
119 Err(Error::UnsupportedProtocol)
120 }
121 _ => return Err(Error::InvalidProtocol),
122 }
123 }
124 'E' => {
125 let message = ErrorResponse::new_from_bytes(message_bytes)?;
126 Ok(Self::ErrorResponse(message))
127 }
128 'S' => {
129 let message = ParameterStatus::new_from_bytes(message_bytes)?;
130 Ok(Self::ParameterStatus(message))
131 }
132 'K' => {
133 let message = BackendKeyData::new_from_bytes(message_bytes)?;
134 Ok(Self::BackendKeyData(message))
135 }
136 'T' => {
137 let message = RowDescription::new_from_bytes(message_bytes)?;
138 Ok(Self::RowDescription(message))
139 }
140 'D' => {
141 let message = DataRow::new_from_bytes(message_bytes)?;
142 Ok(Self::DataRow(message))
143 }
144 'C' => {
145 let message = CommandComplete::new_from_bytes(message_bytes)?;
146 Ok(Self::CommandComplete(message))
147 }
148 'Z' => {
149 let ready_for_query = ReadyForQuery::new_from_bytes(message_bytes)?;
150 Ok(Self::ReadyForQuery(ready_for_query))
151 }
152 '1' => {
153 let parse_complete = ParseComplete::new_from_bytes(message_bytes)?;
154 Ok(Self::ParseComplete(parse_complete))
155 }
156 '2' => {
157 let bind_complete = BindComplete::new_from_bytes(message_bytes)?;
158 Ok(Self::BindComplete(bind_complete))
159 }
160 '3' => {
161 let close_complete = CloseComplete::new_from_bytes(message_bytes)?;
162 Ok(Self::CloseComplete(close_complete))
163 }
164 'A' => {
165 let notification_response = NotificationResponse::new_from_bytes(message_bytes)?;
166 Ok(Self::NotificationResponse(notification_response))
167 }
168 'c' => {
169 let copy_done = CopyDone::new_from_bytes(message_bytes)?;
170 Ok(Self::CopyDone(copy_done))
171 }
172 'd' => {
173 let copy_data = CopyData::new_from_bytes(message_bytes)?;
174 Ok(Self::CopyData(copy_data))
175 }
176 'G' => {
177 let copy_in_response = CopyInResponse::new_from_bytes(message_bytes)?;
178 Ok(Self::CopyInResponse(copy_in_response))
179 }
180 'H' => {
181 let copy_out_response = CopyOutResponse::new_from_bytes(message_bytes)?;
182 Ok(Self::CopyOutResponse(copy_out_response))
183 }
184 'I' => {
185 let empty_query_response = EmptyQueryResponse::new_from_bytes(message_bytes)?;
186 Ok(Self::EmptyQueryResponse(empty_query_response))
187 }
188 'n' => {
189 let no_data = NoData::new_from_bytes(message_bytes)?;
190 Ok(Self::NoData(no_data))
191 }
192 'N' => {
193 let notice_response = NoticeResponse::new_from_bytes(message_bytes)?;
194 Ok(Self::NoticeResponse(notice_response))
195 }
196 's' => {
197 let portal_suspended = PortalSuspended::new_from_bytes(message_bytes)?;
198 Ok(Self::PortalSuspended(portal_suspended))
199 }
200 't' => {
201 let parameter_description = ParameterDescription::new_from_bytes(message_bytes)?;
202 Ok(Self::ParameterDescription(parameter_description))
203 }
204 'V' => {
205 let function_call_response = FunctionCallResponse::new_from_bytes(message_bytes)?;
206 Ok(Self::FunctionCallResponse(function_call_response))
207 }
208 'W' => {
209 let copy_both_response = CopyBothResponse::new_from_bytes(message_bytes)?;
210 Ok(Self::CopyBothResponse(copy_both_response))
211 }
212
213 _ => return Err(Error::InvalidProtocol),
214 }
215 }
216}
217
218pub trait BackendMessage: Message {}
219
220#[derive(Debug)]
221pub struct ParseComplete {
222 message_bytes: BytesMut,
223}
224
225impl ParseComplete {
226 pub fn new() -> Self {
227 let mut message_bytes =
228 BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i32>());
229
230 message_bytes.put_u8(b'1');
231 message_bytes.put_i32(4);
232
233 Self { message_bytes }
234 }
235}
236
237impl BackendMessage for ParseComplete {}
238
239impl Message for ParseComplete {
240 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
241 if message_bytes.len() != mem::size_of::<u8>() + mem::size_of::<i32>() {
242 return Err(Error::InvalidBytes);
243 }
244
245 Ok(Self { message_bytes })
246 }
247
248 fn get_bytes(&self) -> &BytesMut {
249 return &self.message_bytes;
250 }
251}
252
253#[derive(Debug)]
254pub struct BindComplete {
255 message_bytes: BytesMut,
256}
257
258impl BindComplete {
259 pub fn new() -> Self {
260 let mut message_bytes =
261 BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i32>());
262
263 message_bytes.put_u8(b'2');
264 message_bytes.put_i32(4);
265
266 Self { message_bytes }
267 }
268}
269
270impl BackendMessage for BindComplete {}
271
272impl Message for BindComplete {
273 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
274 if message_bytes.len() != mem::size_of::<u8>() + mem::size_of::<i32>() {
275 return Err(Error::InvalidBytes);
276 }
277
278 Ok(Self { message_bytes })
279 }
280
281 fn get_bytes(&self) -> &BytesMut {
282 return &self.message_bytes;
283 }
284}
285
286#[derive(Debug)]
287pub struct CloseComplete {
288 message_bytes: BytesMut,
289}
290
291impl CloseComplete {
292 pub fn new() -> Self {
293 let mut message_bytes =
294 BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i32>());
295
296 message_bytes.put_u8(b'3');
297 message_bytes.put_i32(4);
298
299 Self { message_bytes }
300 }
301}
302
303impl BackendMessage for CloseComplete {}
304
305impl Message for CloseComplete {
306 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
307 if message_bytes.len() != mem::size_of::<u8>() + mem::size_of::<i32>() {
308 return Err(Error::InvalidBytes);
309 }
310
311 Ok(Self { message_bytes })
312 }
313
314 fn get_bytes(&self) -> &BytesMut {
315 return &self.message_bytes;
316 }
317}
318
319#[derive(Debug)]
320pub struct NotificationResponse {
321 message_bytes: BytesMut,
322}
323
324impl NotificationResponse {
325 pub fn new(message_bytes: BytesMut) -> Self {
326 Self { message_bytes }
327 }
328}
329
330impl BackendMessage for NotificationResponse {}
331
332impl Message for NotificationResponse {
333 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
334 Ok(Self { message_bytes })
335 }
336
337 fn get_bytes(&self) -> &BytesMut {
338 return &self.message_bytes;
339 }
340}
341
342#[derive(Debug)]
343pub struct FunctionCallResponse {
344 message_bytes: BytesMut,
345}
346
347impl FunctionCallResponse {
348 pub fn new(message_bytes: BytesMut) -> Self {
349 Self { message_bytes }
350 }
351}
352
353impl BackendMessage for FunctionCallResponse {}
354
355impl Message for FunctionCallResponse {
356 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
357 Ok(Self { message_bytes })
358 }
359
360 fn get_bytes(&self) -> &BytesMut {
361 return &self.message_bytes;
362 }
363}
364
365#[derive(Debug)]
366pub struct CopyBothResponse {
367 message_bytes: BytesMut,
368}
369
370impl CopyBothResponse {
371 pub fn new(message_bytes: BytesMut) -> Self {
372 Self { message_bytes }
373 }
374}
375
376impl BackendMessage for CopyBothResponse {}
377
378impl Message for CopyBothResponse {
379 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
380 Ok(Self { message_bytes })
381 }
382
383 fn get_bytes(&self) -> &BytesMut {
384 return &self.message_bytes;
385 }
386}
387
388#[derive(Debug)]
389pub struct CopyDone {
390 message_bytes: BytesMut,
391}
392
393impl CopyDone {
394 pub fn new() -> Self {
395 let mut message_bytes =
396 BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i32>());
397
398 message_bytes.put_u8(b'c');
399 message_bytes.put_i32(4);
400 Self { message_bytes }
401 }
402}
403
404impl BackendMessage for CopyDone {}
405
406impl Message for CopyDone {
407 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
408 if message_bytes.len() != mem::size_of::<u8>() + mem::size_of::<i32>() {
409 return Err(Error::InvalidBytes);
410 }
411
412 Ok(Self { message_bytes })
413 }
414
415 fn get_bytes(&self) -> &BytesMut {
416 return &self.message_bytes;
417 }
418}
419
420#[derive(Debug)]
421pub struct CommandComplete {
422 message_bytes: BytesMut,
423}
424
425pub struct CommandCompleteParams {
426 pub command_tag: String,
427}
428
429impl CommandComplete {
430 pub fn new(command_tag: String) -> Self {
431 let mut message_bytes = BytesMut::with_capacity(
432 mem::size_of::<u8>() + mem::size_of::<i32>() + mem::size_of::<u8>(),
433 );
434
435 let msg_len = (command_tag.len() + 1 + mem::size_of::<i32>()) as i32;
436
437 message_bytes.put_u8(b'C');
438 message_bytes.put_i32(msg_len);
439 message_bytes.put(&command_tag.as_bytes()[..]);
440 message_bytes.put_u8(0);
441
442 Self { message_bytes }
443 }
444
445 pub fn get_params(&self) -> CommandCompleteParams {
446 let mut cursor = Cursor::new(&self.message_bytes);
447
448 let _code = cursor.get_u8();
449 let _len = cursor.get_i32() as usize;
450
451 let command_tag = cursor.read_string().unwrap();
452
453 CommandCompleteParams { command_tag }
454 }
455}
456
457impl BackendMessage for CommandComplete {}
458
459impl Message for CommandComplete {
460 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
461 Ok(Self { message_bytes })
462 }
463
464 fn get_bytes(&self) -> &BytesMut {
465 return &self.message_bytes;
466 }
467}
468
469#[derive(Debug)]
470pub struct CopyData {
471 message_bytes: BytesMut,
472}
473
474impl CopyData {
475 pub fn new(message_bytes: BytesMut) -> Self {
476 Self { message_bytes }
477 }
478}
479
480impl BackendMessage for CopyData {}
481
482impl Message for CopyData {
483 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
484 Ok(Self { message_bytes })
485 }
486
487 fn get_bytes(&self) -> &BytesMut {
488 return &self.message_bytes;
489 }
490}
491
492#[derive(Debug)]
493pub struct DataRow {
494 message_bytes: BytesMut,
495}
496
497impl DataRow {
498 pub fn new(message_bytes: BytesMut) -> Self {
499 Self { message_bytes }
500 }
501}
502
503impl BackendMessage for DataRow {}
504
505impl Message for DataRow {
506 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
507 Ok(Self { message_bytes })
508 }
509
510 fn get_bytes(&self) -> &BytesMut {
511 return &self.message_bytes;
512 }
513}
514
515#[derive(Debug)]
516pub struct ErrorResponse {
517 message_bytes: BytesMut,
518}
519
520impl ErrorResponse {
521 pub fn new(message_bytes: BytesMut) -> Self {
522 Self { message_bytes }
523 }
524}
525
526impl BackendMessage for ErrorResponse {}
527
528impl Message for ErrorResponse {
529 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
530 Ok(Self { message_bytes })
531 }
532
533 fn get_bytes(&self) -> &BytesMut {
534 return &self.message_bytes;
535 }
536}
537
538#[derive(Debug)]
539pub struct CopyInResponse {
540 message_bytes: BytesMut,
541}
542
543impl CopyInResponse {
544 pub fn new(message_bytes: BytesMut) -> Self {
545 Self { message_bytes }
546 }
547}
548
549impl BackendMessage for CopyInResponse {}
550
551impl Message for CopyInResponse {
552 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
553 Ok(Self { message_bytes })
554 }
555
556 fn get_bytes(&self) -> &BytesMut {
557 return &self.message_bytes;
558 }
559}
560
561#[derive(Debug)]
562pub struct CopyOutResponse {
563 message_bytes: BytesMut,
564}
565
566impl CopyOutResponse {
567 pub fn new(message_bytes: BytesMut) -> Self {
568 Self { message_bytes }
569 }
570}
571
572impl BackendMessage for CopyOutResponse {}
573
574impl Message for CopyOutResponse {
575 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
576 Ok(Self { message_bytes })
577 }
578
579 fn get_bytes(&self) -> &BytesMut {
580 return &self.message_bytes;
581 }
582}
583
584#[derive(Debug)]
585pub struct EmptyQueryResponse {
586 message_bytes: BytesMut,
587}
588
589impl EmptyQueryResponse {
590 pub fn new() -> Self {
591 let mut message_bytes =
592 BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i32>());
593
594 message_bytes.put_u8(b'I');
595 message_bytes.put_i32(4);
596 Self { message_bytes }
597 }
598}
599
600impl BackendMessage for EmptyQueryResponse {}
601
602impl Message for EmptyQueryResponse {
603 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
604 if message_bytes.len() != mem::size_of::<u8>() + mem::size_of::<i32>() {
605 return Err(Error::InvalidBytes);
606 }
607
608 Ok(Self { message_bytes })
609 }
610
611 fn get_bytes(&self) -> &BytesMut {
612 return &self.message_bytes;
613 }
614}
615
616#[derive(Debug)]
617pub struct BackendKeyData {
618 message_bytes: BytesMut,
619}
620
621pub struct BackendKeyDataParams {
622 pub process_id: i32,
623 pub secret_key: i32,
624}
625
626impl BackendKeyData {
627 pub fn new(process_id: i32, secret_key: i32) -> Self {
628 let mut message_bytes = BytesMut::with_capacity(
629 mem::size_of::<u8>()
630 + mem::size_of::<i32>()
631 + mem::size_of::<i32>()
632 + mem::size_of::<i32>(),
633 );
634
635 message_bytes.put_u8(b'K');
636 message_bytes.put_i32(12);
637 message_bytes.put_i32(process_id);
638 message_bytes.put_i32(secret_key);
639
640 Self { message_bytes }
641 }
642
643 pub fn get_params(&self) -> BackendKeyDataParams {
644 let mut cursor = Cursor::new(&self.message_bytes);
645
646 let _code = cursor.get_u8();
647 let _len = cursor.get_i32();
648 let process_id = cursor.get_i32();
649 let secret_key = cursor.get_i32();
650
651 BackendKeyDataParams {
652 process_id,
653 secret_key,
654 }
655 }
656}
657
658impl BackendMessage for BackendKeyData {}
659
660impl Message for BackendKeyData {
661 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
662 if message_bytes.len()
663 != mem::size_of::<u8>()
664 + mem::size_of::<i32>()
665 + mem::size_of::<i32>()
666 + mem::size_of::<i32>()
667 {
668 return Err(Error::InvalidBytes);
669 }
670
671 Ok(Self { message_bytes })
672 }
673
674 fn get_bytes(&self) -> &BytesMut {
675 return &self.message_bytes;
676 }
677}
678
679#[derive(Debug)]
680pub struct NoData {
681 message_bytes: BytesMut,
682}
683
684impl NoData {
685 pub fn new() -> Self {
686 let mut message_bytes =
687 BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i32>());
688
689 message_bytes.put_u8(b'n');
690 message_bytes.put_i32(4);
691
692 Self { message_bytes }
693 }
694}
695
696impl BackendMessage for NoData {}
697
698impl Message for NoData {
699 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
700 if message_bytes.len() != mem::size_of::<u8>() + mem::size_of::<i32>() {
701 return Err(Error::InvalidBytes);
702 }
703
704 Ok(Self { message_bytes })
705 }
706
707 fn get_bytes(&self) -> &BytesMut {
708 return &self.message_bytes;
709 }
710}
711
712#[derive(Debug)]
713pub struct NoticeResponse {
714 message_bytes: BytesMut,
715}
716
717impl NoticeResponse {
718 pub fn new(message_bytes: BytesMut) -> Self {
719 Self { message_bytes }
720 }
721}
722
723impl BackendMessage for NoticeResponse {}
724
725impl Message for NoticeResponse {
726 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
727 Ok(Self { message_bytes })
728 }
729
730 fn get_bytes(&self) -> &BytesMut {
731 return &self.message_bytes;
732 }
733}
734
735#[derive(Debug)]
736pub struct ParameterDescription {
737 message_bytes: BytesMut,
738}
739
740impl ParameterDescription {
741 pub fn new(message_bytes: BytesMut) -> Self {
742 Self { message_bytes }
743 }
744}
745
746impl BackendMessage for ParameterDescription {}
747
748impl Message for ParameterDescription {
749 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
750 Ok(Self { message_bytes })
751 }
752
753 fn get_bytes(&self) -> &BytesMut {
754 return &self.message_bytes;
755 }
756}
757
758#[derive(Debug)]
759pub struct ParameterStatus {
760 message_bytes: BytesMut,
761}
762
763impl ParameterStatus {
764 pub fn new(message_bytes: BytesMut) -> Self {
765 Self { message_bytes }
766 }
767}
768
769impl BackendMessage for ParameterStatus {}
770
771impl Message for ParameterStatus {
772 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
773 Ok(Self { message_bytes })
774 }
775
776 fn get_bytes(&self) -> &BytesMut {
777 return &self.message_bytes;
778 }
779}
780
781#[derive(Debug)]
782pub struct RowDescription {
783 message_bytes: BytesMut,
784}
785
786impl RowDescription {
787 pub fn new(message_bytes: BytesMut) -> Self {
788 Self { message_bytes }
789 }
790}
791
792impl BackendMessage for RowDescription {}
793
794impl Message for RowDescription {
795 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
796 Ok(Self { message_bytes })
797 }
798
799 fn get_bytes(&self) -> &BytesMut {
800 return &self.message_bytes;
801 }
802}
803
804#[derive(Debug)]
805pub struct PortalSuspended {
806 message_bytes: BytesMut,
807}
808
809impl PortalSuspended {
810 pub fn new() -> Self {
811 let mut message_bytes =
812 BytesMut::with_capacity(mem::size_of::<u8>() + mem::size_of::<i32>());
813
814 message_bytes.put_u8(b's');
815 message_bytes.put_i32(4);
816
817 Self { message_bytes }
818 }
819}
820
821impl BackendMessage for PortalSuspended {}
822
823impl Message for PortalSuspended {
824 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
825 Ok(Self { message_bytes })
826 }
827
828 fn get_bytes(&self) -> &BytesMut {
829 return &self.message_bytes;
830 }
831}
832
833#[derive(Debug)]
834pub struct ReadyForQuery {
835 message_bytes: BytesMut,
836}
837
838pub struct ReadyForQueryParams {
839 pub tx_status: u8,
840}
841
842impl ReadyForQuery {
843 pub fn new(tx_status: u8) -> Self {
844 let mut message_bytes = BytesMut::with_capacity(
845 mem::size_of::<u8>() + mem::size_of::<i32>() + mem::size_of::<u8>(),
846 );
847 message_bytes.put_u8(b'Z');
848 message_bytes.put_i32(5);
849 message_bytes.put_u8(tx_status);
850
851 Self { message_bytes }
852 }
853
854 pub fn get_params(&self) -> ReadyForQueryParams {
855 let mut cursor = Cursor::new(&self.message_bytes);
856
857 let _code = cursor.get_u8();
858 let _len = cursor.get_i32();
859 let tx_status = cursor.get_u8(); ReadyForQueryParams { tx_status }
862 }
863}
864
865impl BackendMessage for ReadyForQuery {}
866
867impl Message for ReadyForQuery {
868 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
869 if message_bytes.len()
870 != mem::size_of::<u8>() + mem::size_of::<i32>() + mem::size_of::<u8>()
871 {
872 return Err(Error::InvalidBytes);
873 }
874
875 Ok(Self { message_bytes })
876 }
877
878 fn get_bytes(&self) -> &BytesMut {
879 return &self.message_bytes;
880 }
881}
882
883pub const AUTH_CODE_OK: i32 = 0;
889pub const AUTH_CODE_CLEARTEXT_PASSWORD: i32 = 3;
890pub const AUTH_CODE_MD5_PASSWORD: i32 = 5;
891pub const AUTH_CODE_SCM_CREDS: i32 = 6;
892pub const AUTH_CODE_GSS: i32 = 7;
893pub const AUTH_CODE_GSS_CONT: i32 = 8;
894pub const AUTH_CODE_SSPI: i32 = 9;
895pub const AUTH_CODE_SASL: i32 = 10;
896pub const AUTH_CODE_SASL_CONTINUE: i32 = 11;
897pub const AUTH_CODE_SASL_FINAL: i32 = 12;
898
899#[derive(Debug)]
900pub struct AuthenticationOk {
901 message_bytes: BytesMut,
902}
903
904impl AuthenticationOk {
905 pub fn new() -> Self {
906 let mut message_bytes = BytesMut::with_capacity(
907 mem::size_of::<u8>() + mem::size_of::<i32>() + mem::size_of::<i32>(),
908 );
909
910 message_bytes.put_u8(b'R');
911 message_bytes.put_i32(8);
912 message_bytes.put_i32(AUTH_CODE_OK);
913
914 Self { message_bytes }
915 }
916}
917
918impl BackendMessage for AuthenticationOk {}
919
920impl Message for AuthenticationOk {
921 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
922 if message_bytes.len()
923 != mem::size_of::<u8>() + mem::size_of::<i32>() + mem::size_of::<i32>()
924 {
925 return Err(Error::InvalidBytes);
926 }
927
928 Ok(Self { message_bytes })
929 }
930
931 fn get_bytes(&self) -> &BytesMut {
932 return &self.message_bytes;
933 }
934}
935
936#[derive(Debug)]
937pub struct AuthenticationCleartextPassword {
938 message_bytes: BytesMut,
939}
940
941impl AuthenticationCleartextPassword {
942 pub fn new() -> Self {
943 let mut message_bytes = BytesMut::with_capacity(
944 mem::size_of::<u8>() + mem::size_of::<i32>() + mem::size_of::<i32>(),
945 );
946
947 message_bytes.put_u8(b'R');
948 message_bytes.put_i32(8);
949 message_bytes.put_i32(AUTH_CODE_CLEARTEXT_PASSWORD);
950
951 Self { message_bytes }
952 }
953}
954
955impl BackendMessage for AuthenticationCleartextPassword {}
956
957impl Message for AuthenticationCleartextPassword {
958 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
959 if message_bytes.len()
960 != mem::size_of::<u8>() + mem::size_of::<i32>() + mem::size_of::<i32>()
961 {
962 return Err(Error::InvalidBytes);
963 }
964
965 Ok(Self { message_bytes })
966 }
967
968 fn get_bytes(&self) -> &BytesMut {
969 return &self.message_bytes;
970 }
971}
972
973#[derive(Debug)]
974pub struct AuthenticationMD5Password {
975 message_bytes: BytesMut,
976}
977
978pub struct AuthenticationMD5PasswordParams {
979 pub salt: [u8; 4],
980}
981
982impl AuthenticationMD5Password {
983 pub fn new(salt: [u8; 4]) -> Self {
984 let mut message_bytes = BytesMut::with_capacity(
985 mem::size_of::<u8>() + mem::size_of::<i32>() * 2 + mem::size_of::<u8>() * 4,
986 );
987
988 message_bytes.put_u8(b'R');
989 message_bytes.put_i32(12);
990 message_bytes.put_i32(AUTH_CODE_MD5_PASSWORD);
991 message_bytes.put_slice(&salt);
992
993 Self { message_bytes }
994 }
995
996 pub fn get_params(&self) -> AuthenticationMD5PasswordParams {
997 let mut cursor = Cursor::new(&self.message_bytes);
998
999 let _code = cursor.get_u8();
1000 let _len = cursor.get_i32();
1001 let _type = cursor.get_i32();
1002
1003 let mut salt = [0; 4];
1004 cursor.read_exact(&mut salt).unwrap();
1005
1006 AuthenticationMD5PasswordParams { salt }
1007 }
1008}
1009
1010impl BackendMessage for AuthenticationMD5Password {}
1011
1012impl Message for AuthenticationMD5Password {
1013 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
1014 if message_bytes.len()
1015 != mem::size_of::<u8>() + mem::size_of::<i32>() * 2 + mem::size_of::<u8>() * 4
1016 {
1017 return Err(Error::InvalidBytes);
1018 }
1019
1020 Ok(Self { message_bytes })
1021 }
1022
1023 fn get_bytes(&self) -> &BytesMut {
1024 return &self.message_bytes;
1025 }
1026}
1027
1028#[derive(Debug)]
1029pub struct AuthenticationSASL {
1030 message_bytes: BytesMut,
1031}
1032
1033impl AuthenticationSASL {
1034 pub fn new(message_bytes: BytesMut) -> Self {
1035 Self { message_bytes }
1036 }
1037}
1038
1039impl BackendMessage for AuthenticationSASL {}
1040
1041impl Message for AuthenticationSASL {
1042 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
1043 Ok(Self { message_bytes })
1044 }
1045
1046 fn get_bytes(&self) -> &BytesMut {
1047 return &self.message_bytes;
1048 }
1049}
1050
1051#[derive(Debug)]
1052pub struct AuthenticationSASLContinue {
1053 message_bytes: BytesMut,
1054}
1055
1056impl AuthenticationSASLContinue {
1057 pub fn new(message_bytes: BytesMut) -> Self {
1058 Self { message_bytes }
1059 }
1060}
1061
1062impl BackendMessage for AuthenticationSASLContinue {}
1063
1064impl Message for AuthenticationSASLContinue {
1065 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
1066 Ok(Self { message_bytes })
1067 }
1068
1069 fn get_bytes(&self) -> &BytesMut {
1070 return &self.message_bytes;
1071 }
1072}
1073
1074#[derive(Debug)]
1075pub struct AuthenticationSASLFinal {
1076 message_bytes: BytesMut,
1077}
1078
1079impl AuthenticationSASLFinal {
1080 pub fn new(message_bytes: BytesMut) -> Self {
1081 Self { message_bytes }
1082 }
1083}
1084
1085impl BackendMessage for AuthenticationSASLFinal {}
1086
1087impl Message for AuthenticationSASLFinal {
1088 fn new_from_bytes(message_bytes: BytesMut) -> Result<Self, Error> {
1089 Ok(Self { message_bytes })
1090 }
1091
1092 fn get_bytes(&self) -> &BytesMut {
1093 return &self.message_bytes;
1094 }
1095}