pg-proto 0.2.0

Session-typed PostgreSQL wire protocol
Documentation
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
//! Core query, COPY, and error-draining typestates.

use std::io;

use bytes::{BufMut, Bytes, BytesMut};

use crate::{
    Conn, Dirty, Pristine,
    auth::Ready,
    codec::{
        BackendMessage, Bind, Close, CopyResponse, Describe, DiagnosticResponse, Execute, Frame,
        FunctionCall, Parse, TransactionStatus,
    },
    demux::SessionItem,
    grammar::frontend,
    pre_startup::Terminated,
    replication::{BackendReplication, FrontendReplication},
};

#[derive(Debug)]
/// A simple-query command is awaiting backend results and readiness.
pub enum SimpleQuery {}

#[derive(Debug)]
/// A legacy function call is awaiting its result.
pub enum FunctionCalling {}

#[derive(Debug)]
/// An extended-query pipeline is being built but has no bound portal yet.
pub enum Building {}

#[derive(Debug)]
/// An extended-query pipeline contains a bound, executable portal.
pub enum BoundBuilding {}

#[derive(Debug)]
/// Command results are complete and `ReadyForQuery` is required.
pub enum AwaitingReady {}

#[derive(Debug)]
/// The client may stream COPY data to the backend.
pub enum CopyIn {}

#[derive(Debug)]
/// The backend may stream COPY data to the client.
pub enum CopyOut {}

#[derive(Debug)]
/// Both peers may stream COPY data.
pub enum CopyBoth {}

#[derive(Debug)]
/// The client half of a COPY BOTH session is closed.
pub enum CopyBothClientDone {}

#[derive(Debug)]
/// The backend half of a COPY BOTH session is closed.
pub enum CopyBothServerDone {}

#[derive(Debug)]
/// An errored command is being drained until `ReadyForQuery`.
pub enum Draining {}

#[derive(Debug)]
/// A pool-reset query is executing on a dirty connection.
pub enum Resetting {}

#[derive(Debug)]
/// Reset command completion was observed and readiness is required.
pub enum ResetComplete {}

/// Structured backend error used by session transitions.
pub type ErrorResponse = DiagnosticResponse;

/// A successful transition or a backend error that enters [`Draining`].
pub type Fallible<T, S, C = Pristine> = Result<T, (Conn<S, Draining, C>, ErrorResponse)>;

/// Backend branch available while a simple query is active.
#[derive(Debug)]
pub enum SimpleTransition<S, C> {
    /// A result item was consumed and more results may follow.
    Continue(Conn<S, SimpleQuery, C>, SessionItem),
    /// The command entered COPY IN.
    CopyIn(Conn<S, CopyIn, C>, CopyResponse),
    /// The command entered COPY OUT.
    CopyOut(Conn<S, CopyOut, C>, CopyResponse),
    /// The command entered COPY BOTH.
    CopyBoth(Conn<S, CopyBoth, C>, CopyResponse),
    /// Readiness completed the simple-query cycle.
    Ready(ReadyState<S, C>),
    /// An error entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Backend branch available after a result completes.
#[derive(Debug)]
pub enum AwaitingReadyTransition<S, C> {
    /// A non-terminal item was consumed; continue waiting.
    Continue(Conn<S, AwaitingReady, C>, SessionItem),
    /// Readiness completed the command cycle.
    Ready(ReadyState<S, C>),
    /// An error entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Backend branch available for the legacy function-call protocol.
#[derive(Debug)]
pub enum FunctionCallTransition<S, C> {
    /// The backend returned the function value.
    Response(Conn<S, AwaitingReady, C>, Bytes),
    /// The backend rejected the call.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Backend branch while discarding messages after an error.
#[derive(Debug)]
pub enum DrainingTransition<S, C> {
    /// A non-terminal item was discarded; continue draining.
    Continue(Conn<S, Draining, C>, SessionItem),
    /// Readiness completed recovery.
    Ready(ReadyState<S, C>),
}

/// Backend branch during COPY OUT.
#[derive(Debug)]
pub enum CopyOutTransition<S, C> {
    /// One chunk of copy data was received.
    Data(Conn<S, CopyOut, C>, Bytes),
    /// The backend closed the copy stream.
    Done(Conn<S, AwaitingReady, C>),
    /// COPY failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Asynchronous backend branch available while sending COPY IN data.
#[derive(Debug)]
pub enum CopyInTransition<S, C> {
    /// COPY failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Backend branch while both COPY directions remain open.
#[derive(Debug)]
pub enum CopyBothReceive<S, C> {
    /// One opaque backend data chunk was received.
    Data(Conn<S, CopyBoth, C>, Bytes),
    /// The backend closed its half of the stream.
    Done(Conn<S, CopyBothServerDone, C>),
    /// COPY failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Backend branch after the client closes its COPY BOTH half.
#[derive(Debug)]
pub enum CopyBothClientDoneReceive<S, C> {
    /// One final opaque backend data chunk was received.
    Data(Conn<S, CopyBothClientDone, C>, Bytes),
    /// The backend closed its half of the stream.
    Done(Conn<S, AwaitingReady, C>),
    /// COPY failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Typed walsender branch while both replication directions remain open.
#[derive(Debug)]
pub enum ReplicationReceive<S, C> {
    /// One decoded backend replication message was received.
    Message(Conn<S, CopyBoth, C>, BackendReplication),
    /// The backend closed its half of the stream.
    Done(Conn<S, CopyBothServerDone, C>),
    /// Replication failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Typed walsender branch after the standby closes its sending half.
#[derive(Debug)]
pub enum ReplicationClientDoneReceive<S, C> {
    /// One decoded backend replication message was received.
    Message(Conn<S, CopyBothClientDone, C>, BackendReplication),
    /// The backend closed its half of the stream.
    Done(Conn<S, AwaitingReady, C>),
    /// Replication failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, C>, ErrorResponse),
}

/// Replication projection preserving the connection when decoding fails.
pub type ReplicationProjection<S, C> =
    Result<ReplicationReceive<S, C>, (Conn<S, CopyBoth, C>, io::Error)>;
/// Replication projection after client half-close, preserving decode failures.
pub type ReplicationClientDoneProjection<S, C> =
    Result<ReplicationClientDoneReceive<S, C>, (Conn<S, CopyBothClientDone, C>, io::Error)>;

/// Readiness classified by pooling cleanliness evidence.
#[derive(Debug)]
pub enum ReadyState<S, C> {
    /// The connection retained its existing cleanliness index.
    Clean(Conn<S, Ready, C>),
    /// Transaction or parameter evidence made the connection dirty.
    Dirty {
        /// Ready connection carrying the dirty cleanliness marker.
        conn: Conn<S, Ready, Dirty>,
        /// Transaction status reported by the backend.
        status: TransactionStatus,
        /// Whether reported parameters differ from startup values.
        parameters_changed: bool,
    },
}

/// Backend branch while a reset command is executing.
#[derive(Debug)]
pub enum ResettingTransition<S> {
    /// A non-terminal item was consumed; continue waiting.
    Continue(Conn<S, Resetting, Dirty>, SessionItem),
    /// Reset command completion was observed.
    Complete(Conn<S, ResetComplete, Dirty>),
    /// Reset failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, Dirty>, ErrorResponse),
}

/// Backend branch after reset command completion.
#[derive(Debug)]
pub enum ResetCompleteTransition<S> {
    /// A non-terminal item was consumed; continue waiting.
    Continue(Conn<S, ResetComplete, Dirty>, SessionItem),
    /// Idle readiness with restored parameters proved the connection pristine.
    Ready(Conn<S, Ready, Pristine>),
    /// Readiness retained dirty transaction or parameter state.
    Dirty {
        /// Ready connection retaining its dirty marker.
        conn: Conn<S, Ready, Dirty>,
        /// Transaction status reported by the backend.
        status: TransactionStatus,
        /// Whether reported parameters differ from startup values.
        parameters_changed: bool,
    },
    /// Reset failed and entered drain-until-ready recovery.
    Error(Conn<S, Draining, Dirty>, ErrorResponse),
}

impl<S, C> Conn<S, Ready, C> {
    /// Gracefully terminates a ready session without waiting for a backend reply.
    pub fn push_terminate(self) -> (Conn<S, Terminated, C>, Frame) {
        (self.transition(), empty_frame(b'X'))
    }

    /// Buffers a simple query and conservatively taints the pooled session.
    ///
    /// Simple-query text can create resources which are not reflected in
    /// `ParameterStatus`, including listeners, prepared statements, and
    /// advisory locks. Use [`Self::push_stateless_query`] only after custom SQL
    /// inspection has established that the command cannot retain session state.
    ///
    /// # Errors
    ///
    /// Returns an error if the query contains a NUL byte.
    pub fn push_query(self, query: &[u8]) -> io::Result<(Conn<S, SimpleQuery, Dirty>, Frame)> {
        Ok((self.transition(), cstr_frame(b'Q', query)?))
    }

    /// Buffers query text which the caller has proved leaves no session state.
    ///
    /// # Errors
    ///
    /// Returns an error if the query contains a NUL byte.
    pub fn push_stateless_query(
        self,
        query: &[u8],
    ) -> io::Result<(Conn<S, SimpleQuery, C>, Frame)> {
        Ok((self.transition(), cstr_frame(b'Q', query)?))
    }

    /// Buffers the deprecated function-call protocol message as a typed exchange.
    ///
    /// # Errors
    ///
    /// Returns an error if a count or argument length exceeds its wire field.
    pub fn push_function_call(
        self,
        message: &FunctionCall,
    ) -> io::Result<(Conn<S, FunctionCalling, Dirty>, Frame)> {
        Ok((self.transition(), message.to_frame()?))
    }

    /// Buffers an allow-listed function call known not to retain session state.
    ///
    /// # Errors
    ///
    /// Returns an error if a count or argument length exceeds its wire field.
    pub fn push_stateless_function_call(
        self,
        message: &FunctionCall,
    ) -> io::Result<(Conn<S, FunctionCalling, C>, Frame)> {
        Ok((self.transition(), message.to_frame()?))
    }

    /// Begins extended-query construction and consumes the ready connection.
    ///
    /// ```compile_fail
    /// use pg_proto::{Conn, auth::Ready};
    /// fn use_after_transition<S, C>(conn: Conn<S, Ready, C>) {
    ///     let _building = conn.begin_extended();
    ///     let _again = conn.begin_extended();
    /// }
    /// ```
    pub fn begin_extended(self) -> Conn<S, Building, C> {
        self.transition()
    }
}

impl<S, C> Conn<S, FunctionCalling, C> {
    /// Accepts exactly the function result or an error, after which readiness
    /// must still be consumed.
    ///
    /// # Errors
    ///
    /// Returns the unchanged connection and message for an illegal response.
    pub fn offer(
        self,
        message: BackendMessage,
    ) -> Result<FunctionCallTransition<S, C>, (Self, BackendMessage)> {
        match (
            frontend::project_external(frontend::RuntimeState::FunctionCalling, &message),
            message,
        ) {
            (
                Some(frontend::Event::FunctionResponse),
                BackendMessage::FunctionCallResponse(value),
            ) => Ok(FunctionCallTransition::Response(self.transition(), value)),
            (Some(frontend::Event::Error), BackendMessage::ErrorResponse(error)) => {
                Ok(FunctionCallTransition::Error(self.transition(), error))
            }
            (_, other) => Err((self, other)),
        }
    }
}

impl<S> Conn<S, Ready, Pristine> {
    /// Releases only a statically pristine, ready connection to a pool.
    ///
    /// ```compile_fail
    /// use pg_proto::{Conn, Dirty, auth::Ready};
    /// fn cannot_release<S>(conn: Conn<S, Ready, Dirty>) {
    ///     let _transport = conn.release();
    /// }
    /// ```
    pub fn release(self) -> S {
        self.into_transport()
    }
}

impl<S> Conn<S, Ready, Dirty> {
    /// Begins the only typed path which can recover pool-safe cleanliness.
    ///
    /// `ROLLBACK` makes the sequence legal after either transaction status;
    /// `DISCARD ALL` then clears session-local resources and settings.
    ///
    /// # Errors
    ///
    /// Returns an error only if the fixed reset query cannot be encoded.
    pub fn begin_reset(self) -> io::Result<(Conn<S, Resetting, Dirty>, Frame)> {
        Ok((
            self.transition(),
            cstr_frame(b'Q', b"ROLLBACK; DISCARD ALL")?,
        ))
    }
}

impl<S, C> Conn<S, Building, C> {
    /// Parse is a self-loop while constructing an extended-query pipeline.
    ///
    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_parse(self, message: &Parse) -> io::Result<(Conn<S, Building, Dirty>, Frame)> {
        Ok((self.transition(), message.to_frame()?))
    }

    /// Describe is a self-loop while constructing an extended-query pipeline.
    ///
    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_describe(self, message: &Describe) -> io::Result<(Self, Frame)> {
        Ok((self, message.to_frame()?))
    }

    /// Bind introduces an executable portal.
    ///
    /// Execute is not available before this transition:
    ///
    /// ```compile_fail
    /// use bytes::Bytes;
    /// use pg_proto::{Conn, session::Building};
    /// fn execute_without_bind<S, C>(conn: Conn<S, Building, C>) {
    ///     let _ = conn.push_execute(Bytes::new());
    /// }
    /// ```
    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_bind(self, message: &Bind) -> io::Result<(Conn<S, BoundBuilding, Dirty>, Frame)> {
        Ok((self.transition(), message.to_frame()?))
    }

    /// Close is legal while building, but does not make a portal executable.
    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_close(self, message: &Close) -> io::Result<(Self, Frame)> {
        Ok((self, message.to_frame()?))
    }

    /// Requests delivery of buffered extended-query responses without ending the cycle.
    pub fn push_flush(self) -> (Self, Frame) {
        (self, empty_frame(b'H'))
    }

    /// Ends the extended-query pipeline and waits for readiness.
    pub fn push_sync(self) -> (Conn<S, AwaitingReady, C>, Frame) {
        (self.transition(), empty_frame(b'S'))
    }
}

impl<S, C> Conn<S, BoundBuilding, C> {
    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_parse(self, message: &Parse) -> io::Result<(Conn<S, BoundBuilding, Dirty>, Frame)> {
        Ok((self.transition(), message.to_frame()?))
    }

    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_bind(self, message: &Bind) -> io::Result<(Conn<S, BoundBuilding, Dirty>, Frame)> {
        Ok((self.transition(), message.to_frame()?))
    }

    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_describe(self, message: &Describe) -> io::Result<(Self, Frame)> {
        Ok((self, message.to_frame()?))
    }

    /// Execute is unavailable until a Bind transition has occurred.
    ///
    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_execute(self, message: &Execute) -> io::Result<(Self, Frame)> {
        Ok((self, message.to_frame()?))
    }

    /// # Errors
    ///
    /// Returns an error if the structured message cannot be reconstructed.
    pub fn push_close(self, message: &Close) -> io::Result<(Self, Frame)> {
        Ok((self, message.to_frame()?))
    }

    /// Requests delivery of buffered extended-query responses without ending the cycle.
    pub fn push_flush(self) -> (Self, Frame) {
        (self, empty_frame(b'H'))
    }

    /// Ends the extended-query pipeline and waits for readiness.
    pub fn push_sync(self) -> (Conn<S, AwaitingReady, C>, Frame) {
        (self.transition(), empty_frame(b'S'))
    }
}

impl<S, C> Conn<S, SimpleQuery, C> {
    /// Advances a simple-query session using an actual projected backend item.
    ///
    /// # Errors
    ///
    /// Returns the unchanged connection and item if it is illegal in this phase.
    pub fn offer(self, item: SessionItem) -> Result<SimpleTransition<S, C>, (Self, SessionItem)> {
        match (
            project_session_item(frontend::RuntimeState::Simple, &item),
            item,
        ) {
            (
                Some(frontend::Event::CopyIn),
                SessionItem::Message(BackendMessage::CopyInResponse(response)),
            ) => Ok(SimpleTransition::CopyIn(self.transition(), response)),
            (
                Some(frontend::Event::CopyOut),
                SessionItem::Message(BackendMessage::CopyOutResponse(response)),
            ) => Ok(SimpleTransition::CopyOut(self.transition(), response)),
            (
                Some(frontend::Event::CopyBoth),
                SessionItem::Message(BackendMessage::CopyBothResponse(response)),
            ) => Ok(SimpleTransition::CopyBoth(self.transition(), response)),
            (
                Some(frontend::Event::Ready),
                SessionItem::ReadyForQuery {
                    status,
                    parameters_changed,
                },
            ) => Ok(SimpleTransition::Ready(ready_state(
                self,
                status,
                parameters_changed,
            ))),
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => Ok(SimpleTransition::Error(self.transition(), error)),
            (
                Some(frontend::Event::Continue),
                item @ (SessionItem::CommandComplete { .. }
                | SessionItem::Message(
                    BackendMessage::RowDescription(_)
                    | BackendMessage::DataRow(_)
                    | BackendMessage::EmptyQueryResponse,
                )),
            ) => Ok(SimpleTransition::Continue(self, item)),
            (_, item) => Err((self, item)),
        }
    }
}

impl<S, C> Conn<S, CopyIn, C> {
    /// Query is unavailable in this nested COPY session.
    ///
    /// ```compile_fail
    /// use pg_proto::{Conn, session::CopyIn};
    /// fn query_during_copy<S, C>(conn: Conn<S, CopyIn, C>) {
    ///     let _ = conn.push_query(b"select 1");
    /// }
    /// ```
    pub fn push_copy_data(self, data: Bytes) -> (Self, Frame) {
        (
            self,
            Frame {
                tag: b'd',
                body: data,
            },
        )
    }

    /// Closes the client-to-backend copy stream and waits for command completion.
    pub fn push_copy_done(self) -> (Conn<S, AwaitingReady, C>, Frame) {
        (self.transition(), empty_frame(b'c'))
    }

    /// Aborts COPY IN with a frontend error string.
    ///
    /// # Errors
    ///
    /// Returns an error if the message contains a NUL byte.
    pub fn push_copy_fail(self, message: &[u8]) -> io::Result<(Conn<S, AwaitingReady, C>, Frame)> {
        Ok((self.transition(), cstr_frame(b'f', message)?))
    }

    /// Projects an asynchronous backend failure while COPY IN data is being sent.
    ///
    /// This branch is reachable after cancellation or an early server-side COPY
    /// failure. Non-error messages leave the COPY IN connection unchanged.
    ///
    /// # Errors
    ///
    /// Returns the live connection and item when it is not an error response.
    pub fn offer(self, item: SessionItem) -> Result<CopyInTransition<S, C>, (Self, SessionItem)> {
        match (
            project_session_item(frontend::RuntimeState::CopyIn, &item),
            item,
        ) {
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => Ok(CopyInTransition::Error(self.transition(), error)),
            (_, item) => Err((self, item)),
        }
    }
}

impl<S, C> Conn<S, CopyBothClientDone, C> {
    /// Continues receiving after the frontend half has closed.
    ///
    /// # Errors
    ///
    /// Returns the unchanged connection and item for an illegal response.
    pub fn offer(
        self,
        item: SessionItem,
    ) -> Result<CopyBothClientDoneReceive<S, C>, (Self, SessionItem)> {
        match (
            project_session_item(frontend::RuntimeState::CopyBothClientDone, &item),
            item,
        ) {
            (
                Some(frontend::Event::ReceiveCopyData),
                SessionItem::Message(BackendMessage::CopyData(data)),
            ) => Ok(CopyBothClientDoneReceive::Data(self, data)),
            (
                Some(frontend::Event::ReceiveCopyDone),
                SessionItem::Message(BackendMessage::CopyDone),
            ) => Ok(CopyBothClientDoneReceive::Done(self.transition())),
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => Ok(CopyBothClientDoneReceive::Error(self.transition(), error)),
            (_, item) => Err((self, item)),
        }
    }
}

impl<S, C> CopyBothClientDoneReceive<S, C> {
    /// Decodes data received after the frontend half-close.
    ///
    /// # Errors
    ///
    /// Returns the connection with a decoding error for malformed known payloads.
    pub fn decode_replication(self) -> ReplicationClientDoneProjection<S, C> {
        match self {
            Self::Data(conn, data) => match BackendReplication::decode(data) {
                Ok(message) => Ok(ReplicationClientDoneReceive::Message(conn, message)),
                Err(error) => Err((conn, error)),
            },
            Self::Done(conn) => Ok(ReplicationClientDoneReceive::Done(conn)),
            Self::Error(conn, error) => Ok(ReplicationClientDoneReceive::Error(conn, error)),
        }
    }
}

impl<S, C> Conn<S, CopyBothServerDone, C> {
    /// Continues sending after the backend half has closed.
    pub fn push_copy_data(self, data: Bytes) -> (Self, Frame) {
        (
            self,
            Frame {
                tag: b'd',
                body: data,
            },
        )
    }

    /// Sends a structured standby message after the backend half-close.
    pub fn push_replication(self, message: &FrontendReplication) -> (Self, Frame) {
        self.push_copy_data(message.encode())
    }

    /// Closes the remaining frontend half and begins readiness processing.
    pub fn push_copy_done(self) -> (Conn<S, AwaitingReady, C>, Frame) {
        (self.transition(), empty_frame(b'c'))
    }
}

impl<S, C> Conn<S, CopyOut, C> {
    /// Advances COPY OUT using backend evidence.
    ///
    /// # Errors
    ///
    /// Returns the unchanged connection and item when it is illegal in COPY OUT.
    pub fn offer(self, item: SessionItem) -> Result<CopyOutTransition<S, C>, (Self, SessionItem)> {
        match (
            project_session_item(frontend::RuntimeState::CopyOut, &item),
            item,
        ) {
            (
                Some(frontend::Event::CopyData),
                SessionItem::Message(BackendMessage::CopyData(data)),
            ) => Ok(CopyOutTransition::Data(self, data)),
            (Some(frontend::Event::CopyDone), SessionItem::Message(BackendMessage::CopyDone)) => {
                Ok(CopyOutTransition::Done(self.transition()))
            }
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => Ok(CopyOutTransition::Error(self.transition(), error)),
            (_, item) => Err((self, item)),
        }
    }
}

impl<S, C> Conn<S, CopyBoth, C> {
    /// Sends one opaque data chunk while retaining both COPY directions.
    pub fn push_copy_data(self, data: Bytes) -> (Self, Frame) {
        (
            self,
            Frame {
                tag: b'd',
                body: data,
            },
        )
    }

    /// Sends a structured standby message in the walsender stream.
    pub fn push_replication(self, message: &FrontendReplication) -> (Self, Frame) {
        self.push_copy_data(message.encode())
    }

    /// Closes the client half while leaving the backend half readable.
    pub fn push_copy_done(self) -> (Conn<S, CopyBothClientDone, C>, Frame) {
        (self.transition(), empty_frame(b'c'))
    }

    /// Receives the backend half of a bidirectional COPY session.
    ///
    /// # Errors
    ///
    /// Returns the unchanged connection and item when it is illegal in COPY BOTH.
    pub fn offer(self, item: SessionItem) -> Result<CopyBothReceive<S, C>, (Self, SessionItem)> {
        match (
            project_session_item(frontend::RuntimeState::CopyBoth, &item),
            item,
        ) {
            (
                Some(frontend::Event::ReceiveCopyData),
                SessionItem::Message(BackendMessage::CopyData(data)),
            ) => Ok(CopyBothReceive::Data(self, data)),
            (
                Some(frontend::Event::ReceiveCopyDone),
                SessionItem::Message(BackendMessage::CopyDone),
            ) => Ok(CopyBothReceive::Done(self.transition())),
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => Ok(CopyBothReceive::Error(self.transition(), error)),
            (_, item) => Err((self, item)),
        }
    }
}

impl<S, C> CopyBothReceive<S, C> {
    /// Decodes a COPY BOTH data branch as a walsender message.
    ///
    /// # Errors
    ///
    /// Returns the connection with a decoding error for malformed known payloads.
    pub fn decode_replication(self) -> ReplicationProjection<S, C> {
        match self {
            Self::Data(conn, data) => match BackendReplication::decode(data) {
                Ok(message) => Ok(ReplicationReceive::Message(conn, message)),
                Err(error) => Err((conn, error)),
            },
            Self::Done(conn) => Ok(ReplicationReceive::Done(conn)),
            Self::Error(conn, error) => Ok(ReplicationReceive::Error(conn, error)),
        }
    }
}

impl<S, C> Conn<S, Draining, C> {
    /// `ReadyForQuery` is the sole exit from error draining.
    pub fn offer(self, item: SessionItem) -> DrainingTransition<S, C> {
        match (
            project_session_item(frontend::RuntimeState::Draining, &item),
            item,
        ) {
            (
                Some(frontend::Event::Ready),
                SessionItem::ReadyForQuery {
                    status,
                    parameters_changed,
                },
            ) => DrainingTransition::Ready(ready_state(self, status, parameters_changed)),
            (_, item) => DrainingTransition::Continue(self, item),
        }
    }
}

impl<S, C> Conn<S, AwaitingReady, C> {
    /// Consumes responses after Sync until `ReadyForQuery` proves readiness.
    pub fn offer(self, item: SessionItem) -> AwaitingReadyTransition<S, C> {
        match (
            project_session_item(frontend::RuntimeState::AwaitingReady, &item),
            item,
        ) {
            (
                Some(frontend::Event::Ready),
                SessionItem::ReadyForQuery {
                    status,
                    parameters_changed,
                },
            ) => AwaitingReadyTransition::Ready(ready_state(self, status, parameters_changed)),
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => AwaitingReadyTransition::Error(self.transition(), error),
            (_, item) => AwaitingReadyTransition::Continue(self, item),
        }
    }
}

impl<S> Conn<S, Resetting, Dirty> {
    /// Waits for evidence that `DISCARD ALL` itself completed.
    #[must_use]
    pub fn offer(self, item: SessionItem) -> ResettingTransition<S> {
        match (
            project_session_item(frontend::RuntimeState::Resetting, &item),
            item,
        ) {
            (Some(frontend::Event::DiscardComplete), SessionItem::CommandComplete { tag, .. })
                if tag == b"DISCARD ALL".as_slice() =>
            {
                ResettingTransition::Complete(self.transition())
            }
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => ResettingTransition::Error(self.transition(), error),
            (_, item) => ResettingTransition::Continue(self, item),
        }
    }
}

impl<S> Conn<S, ResetComplete, Dirty> {
    /// Restores `Pristine` only from idle readiness and the startup parameter baseline.
    #[must_use]
    pub fn offer(self, item: SessionItem) -> ResetCompleteTransition<S> {
        match (
            project_session_item(frontend::RuntimeState::ResetComplete, &item),
            item,
        ) {
            (
                Some(frontend::Event::ReadyClean),
                SessionItem::ReadyForQuery {
                    status: TransactionStatus::Idle,
                    parameters_changed: false,
                },
            ) => ResetCompleteTransition::Ready(self.transition()),
            (
                Some(frontend::Event::ReadyClean | frontend::Event::ReadyDirty),
                SessionItem::ReadyForQuery {
                    status,
                    parameters_changed,
                },
            ) => ResetCompleteTransition::Dirty {
                conn: self.transition(),
                status,
                parameters_changed,
            },
            (
                Some(frontend::Event::Error),
                SessionItem::Message(BackendMessage::ErrorResponse(error)),
            ) => ResetCompleteTransition::Error(self.transition(), error),
            (_, item) => ResetCompleteTransition::Continue(self, item),
        }
    }
}

impl<S, P> Conn<S, P, Pristine> {
    /// Conservatively records session-local state without changing protocol phase.
    pub fn mark_dirty(self) -> Conn<S, P, Dirty> {
        self.transition()
    }
}

fn project_session_item(
    state: frontend::RuntimeState,
    item: &SessionItem,
) -> Option<frontend::Event> {
    match item {
        SessionItem::Message(message) => frontend::project_external(state, message),
        SessionItem::CommandComplete { tag, .. } => {
            frontend::project_external(state, &BackendMessage::CommandComplete(tag.clone()))
        }
        SessionItem::ReadyForQuery { status, .. } => {
            frontend::project_external(state, &BackendMessage::ReadyForQuery(*status))
        }
    }
}

fn cstr_frame(tag: u8, value: &[u8]) -> io::Result<Frame> {
    if value.contains(&0) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "message string contains a NUL byte",
        ));
    }
    let mut body = BytesMut::with_capacity(value.len() + 1);
    body.extend_from_slice(value);
    body.put_u8(0);
    Ok(Frame {
        tag,
        body: body.freeze(),
    })
}

fn empty_frame(tag: u8) -> Frame {
    Frame {
        tag,
        body: Bytes::new(),
    }
}

fn ready_state<S, P, C>(
    conn: Conn<S, P, C>,
    status: TransactionStatus,
    parameters_changed: bool,
) -> ReadyState<S, C> {
    if status == TransactionStatus::Idle && !parameters_changed {
        ReadyState::Clean(conn.transition())
    } else {
        ReadyState::Dirty {
            conn: conn.transition(),
            status,
            parameters_changed,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn extended_building_self_loops_then_syncs() {
        let ready: Conn<(), Ready> = Conn::new(()).transition();
        let building = ready.begin_extended();
        let (building, _) = building
            .push_parse(&Parse {
                statement: Bytes::from_static(b"statement"),
                query: Bytes::from_static(b"select $1"),
                parameter_types: vec![23],
            })
            .expect("valid Parse");
        let (bound, _) = building
            .push_bind(&Bind {
                portal: Bytes::from_static(b"portal"),
                statement: Bytes::from_static(b"statement"),
                parameter_formats: vec![0],
                parameters: vec![Some(Bytes::from_static(b"42"))],
                result_formats: vec![0],
            })
            .expect("valid Bind");
        let (bound, _) = bound
            .push_execute(&Execute {
                portal: Bytes::from_static(b"portal"),
                max_rows: 0,
            })
            .expect("valid Execute");
        let (awaiting_ready, sync) = bound.push_sync();
        assert_eq!(sync.tag, b'S');
        awaiting_ready.into_transport();
    }

    #[test]
    fn function_call_requires_result_then_ready() {
        let ready: Conn<(), Ready> = Conn::new(()).transition();
        let call = FunctionCall {
            function_oid: 42,
            argument_formats: vec![1],
            arguments: vec![Some(Bytes::from_static(b"argument"))],
            result_format: 1,
        };
        let (calling, frame) = ready.push_function_call(&call).unwrap();
        assert_eq!(frame.tag, b'F');

        let FunctionCallTransition::Response(awaiting_ready, result) = calling
            .offer(BackendMessage::FunctionCallResponse(Bytes::from_static(
                b"result",
            )))
            .unwrap()
        else {
            panic!("function result projected to the wrong branch")
        };
        assert_eq!(result, Bytes::from_static(b"result"));
        let AwaitingReadyTransition::Ready(ReadyState::Clean(ready)) =
            awaiting_ready.offer(SessionItem::ReadyForQuery {
                status: TransactionStatus::Idle,
                parameters_changed: false,
            })
        else {
            panic!("function call did not return to ready")
        };
        ready.into_transport();
    }

    #[test]
    fn ready_session_can_terminate_gracefully() {
        let ready: Conn<(), Ready> = Conn::new(()).transition();
        let (terminated, frame) = ready.push_terminate();
        assert_eq!(frame.tag, b'X');
        assert!(frame.body.is_empty());
        terminated.into_transport();
    }

    #[test]
    fn copy_both_waits_for_both_half_closes() {
        use crate::grammar::frontend::{Event, RuntimeFsm, RuntimeState};

        let mut client_first = RuntimeFsm::new();
        client_first.step(Event::Query).unwrap();
        client_first.step(Event::CopyBoth).unwrap();
        let open: Conn<(), CopyBoth> = Conn::new(()).transition();
        let (client_done, frame) = open.push_copy_done();
        client_first.step(Event::SendCopyDone).unwrap();
        assert_eq!(frame.tag, b'c');
        let CopyBothClientDoneReceive::Data(client_done, data) = client_done
            .offer(SessionItem::Message(BackendMessage::CopyData(
                Bytes::from_static(b"after client close"),
            )))
            .unwrap()
        else {
            panic!("backend data projected to the wrong branch")
        };
        client_first.step(Event::ReceiveCopyData).unwrap();
        assert_eq!(data, Bytes::from_static(b"after client close"));
        let CopyBothClientDoneReceive::Done(awaiting) = client_done
            .offer(SessionItem::Message(BackendMessage::CopyDone))
            .unwrap()
        else {
            panic!("backend close projected to the wrong branch")
        };
        client_first.step(Event::ReceiveCopyDone).unwrap();
        assert_eq!(client_first.state(), RuntimeState::AwaitingReady);
        awaiting.into_transport();

        let mut server_first = RuntimeFsm::new();
        server_first.step(Event::Query).unwrap();
        server_first.step(Event::CopyBoth).unwrap();
        let open: Conn<(), CopyBoth> = Conn::new(()).transition();
        let CopyBothReceive::Done(server_done) = open
            .offer(SessionItem::Message(BackendMessage::CopyDone))
            .unwrap()
        else {
            panic!("backend close projected to the wrong branch")
        };
        server_first.step(Event::ReceiveCopyDone).unwrap();
        let (server_done, data) =
            server_done.push_copy_data(Bytes::from_static(b"after server close"));
        server_first.step(Event::SendCopyData).unwrap();
        assert_eq!(data.tag, b'd');
        let (awaiting, done) = server_done.push_copy_done();
        server_first.step(Event::SendCopyDone).unwrap();
        assert_eq!(done.tag, b'c');
        assert_eq!(server_first.state(), RuntimeState::AwaitingReady);
        awaiting.into_transport();
    }

    #[test]
    fn copy_in_can_receive_an_early_backend_error() {
        let copy: Conn<(), CopyIn> = Conn::new(()).transition();
        let error = DiagnosticResponse {
            fields: vec![crate::codec::DiagnosticField {
                code: b'M',
                value: Bytes::from_static(b"copy cancelled"),
            }],
        };
        let CopyInTransition::Error(draining, received) = copy
            .offer(SessionItem::Message(BackendMessage::ErrorResponse(
                error.clone(),
            )))
            .unwrap();
        assert_eq!(received, error);

        let DrainingTransition::Ready(ReadyState::Clean(ready)) =
            draining.offer(SessionItem::ReadyForQuery {
                status: TransactionStatus::Idle,
                parameters_changed: false,
            })
        else {
            panic!("COPY failure did not drain to readiness")
        };
        ready.release();
    }

    #[test]
    fn copy_both_projects_typed_replication_without_losing_connection() {
        let open: Conn<(), CopyBoth> = Conn::new(()).transition();
        let status = FrontendReplication::StandbyStatus {
            written: 10,
            flushed: 9,
            applied: 8,
            client_time: 7,
            reply_requested: true,
        };
        let (open, frame) = open.push_replication(&status);
        assert_eq!(frame.body, status.encode());

        let keepalive = BackendReplication::PrimaryKeepalive {
            wal_end: 11,
            server_time: 12,
            reply_requested: true,
        };
        let receive = open
            .offer(SessionItem::Message(BackendMessage::CopyData(
                keepalive.encode(),
            )))
            .unwrap();
        let ReplicationReceive::Message(open, decoded) = receive.decode_replication().unwrap()
        else {
            panic!("keepalive projected to the wrong branch")
        };
        assert_eq!(decoded, keepalive);
        open.into_transport();

        let open: Conn<(), CopyBoth> = Conn::new(()).transition();
        let receive = open
            .offer(SessionItem::Message(BackendMessage::CopyData(
                Bytes::from_static(b"kshort"),
            )))
            .unwrap();
        let (open, _) = receive.decode_replication().unwrap_err();
        open.into_transport();
    }

    #[test]
    fn transaction_status_taints_ready_connection() {
        let query: Conn<(), SimpleQuery> = Conn::new(()).transition();
        let transition = query
            .offer(SessionItem::ReadyForQuery {
                status: TransactionStatus::InTransaction,
                parameters_changed: false,
            })
            .expect("ReadyForQuery is valid evidence");
        let SimpleTransition::Ready(ReadyState::Dirty {
            conn,
            status: TransactionStatus::InTransaction,
            parameters_changed: false,
        }) = transition
        else {
            panic!("transaction should taint readiness")
        };
        conn.into_transport();
    }

    #[test]
    fn changed_parameters_taint_idle_connection() {
        let query: Conn<(), SimpleQuery> = Conn::new(()).transition();
        let transition = query
            .offer(SessionItem::ReadyForQuery {
                status: TransactionStatus::Idle,
                parameters_changed: true,
            })
            .expect("ReadyForQuery is valid evidence");
        let SimpleTransition::Ready(ReadyState::Dirty {
            conn,
            status: TransactionStatus::Idle,
            parameters_changed: true,
        }) = transition
        else {
            panic!("parameter change should taint readiness")
        };
        conn.into_transport();
    }

    #[test]
    fn simple_queries_are_dirty_unless_inspection_proves_them_stateless() {
        fn require_dirty<S>(conn: Conn<S, Ready, Dirty>) {
            conn.into_transport();
        }

        let ready: Conn<(), Ready> = Conn::new(()).transition();
        let (query, _) = ready.push_query(b"LISTEN events").unwrap();
        let SimpleTransition::Ready(ReadyState::Clean(dirty)) = query
            .offer(SessionItem::ReadyForQuery {
                status: TransactionStatus::Idle,
                parameters_changed: false,
            })
            .unwrap()
        else {
            panic!("idle readiness should preserve the query's dirty evidence")
        };
        require_dirty(dirty);

        let ready: Conn<(), Ready> = Conn::new(()).transition();
        let (query, _) = ready.push_stateless_query(b"SELECT 1").unwrap();
        let SimpleTransition::Ready(ReadyState::Clean(pristine)) = query
            .offer(SessionItem::ReadyForQuery {
                status: TransactionStatus::Idle,
                parameters_changed: false,
            })
            .unwrap()
        else {
            panic!("stateless query should retain pristine evidence")
        };
        pristine.release();
    }

    #[test]
    fn discard_all_evidence_recovers_pool_cleanliness() {
        let ready: Conn<(), Ready> = Conn::new(()).transition();
        let (resetting, frame) = ready.mark_dirty().begin_reset().unwrap();
        assert_eq!(frame.body, Bytes::from_static(b"ROLLBACK; DISCARD ALL\0"));
        let ResettingTransition::Continue(resetting, _) =
            resetting.offer(SessionItem::CommandComplete {
                tag: Bytes::from_static(b"ROLLBACK"),
                command: crate::demux::CommandIndex(0),
                notices: vec![],
            })
        else {
            panic!("ROLLBACK incorrectly completed reset")
        };
        let ResettingTransition::Complete(reset_complete) =
            resetting.offer(SessionItem::CommandComplete {
                tag: Bytes::from_static(b"DISCARD ALL"),
                command: crate::demux::CommandIndex(1),
                notices: vec![],
            })
        else {
            panic!("DISCARD ALL did not advance reset")
        };
        let ResetCompleteTransition::Ready(ready) =
            reset_complete.offer(SessionItem::ReadyForQuery {
                status: TransactionStatus::Idle,
                parameters_changed: false,
            })
        else {
            panic!("clean ready evidence did not restore pristine state")
        };
        ready.release();
    }
}