zero_postgres/protocol/backend/
mod.rs

1//! PostgreSQL backend (server → client) messages.
2
3pub mod auth;
4pub mod copy;
5pub mod error;
6pub mod extended;
7pub mod query;
8
9pub use auth::{
10    AuthenticationMessage, BackendKeyData, NegotiateProtocolVersion, NotificationResponse,
11    ParameterStatus, ReadyForQuery,
12};
13pub use copy::{CopyInResponse, CopyOutResponse};
14// Re-export shared copy types for convenience
15pub use super::copy::{CopyData, CopyDone};
16pub use error::{ErrorResponse, NoticeResponse};
17pub use extended::{
18    BindComplete, CloseComplete, NoData, ParameterDescription, ParseComplete, PortalSuspended,
19};
20pub use query::{
21    CommandComplete, DataRow, EmptyQueryResponse, FieldDescriptionTail, RowDescription,
22};
23
24/// Backend message type bytes.
25pub mod msg_type {
26    /// Authentication message
27    pub const AUTHENTICATION: u8 = b'R';
28    /// BackendKeyData
29    pub const BACKEND_KEY_DATA: u8 = b'K';
30    /// ParameterStatus
31    pub const PARAMETER_STATUS: u8 = b'S';
32    /// ReadyForQuery
33    pub const READY_FOR_QUERY: u8 = b'Z';
34    /// RowDescription
35    pub const ROW_DESCRIPTION: u8 = b'T';
36    /// DataRow
37    pub const DATA_ROW: u8 = b'D';
38    /// CommandComplete
39    pub const COMMAND_COMPLETE: u8 = b'C';
40    /// EmptyQueryResponse
41    pub const EMPTY_QUERY_RESPONSE: u8 = b'I';
42    /// ErrorResponse
43    pub const ERROR_RESPONSE: u8 = b'E';
44    /// NoticeResponse
45    pub const NOTICE_RESPONSE: u8 = b'N';
46    /// NotificationResponse
47    pub const NOTIFICATION_RESPONSE: u8 = b'A';
48    /// ParseComplete
49    pub const PARSE_COMPLETE: u8 = b'1';
50    /// BindComplete
51    pub const BIND_COMPLETE: u8 = b'2';
52    /// CloseComplete
53    pub const CLOSE_COMPLETE: u8 = b'3';
54    /// ParameterDescription
55    pub const PARAMETER_DESCRIPTION: u8 = b't';
56    /// NoData
57    pub const NO_DATA: u8 = b'n';
58    /// PortalSuspended
59    pub const PORTAL_SUSPENDED: u8 = b's';
60    /// CopyInResponse
61    pub const COPY_IN_RESPONSE: u8 = b'G';
62    /// CopyOutResponse
63    pub const COPY_OUT_RESPONSE: u8 = b'H';
64    /// CopyBothResponse
65    pub const COPY_BOTH_RESPONSE: u8 = b'W';
66    /// CopyData
67    pub const COPY_DATA: u8 = b'd';
68    /// CopyDone
69    pub const COPY_DONE: u8 = b'c';
70    /// FunctionCallResponse
71    pub const FUNCTION_CALL_RESPONSE: u8 = b'V';
72    /// NegotiateProtocolVersion
73    pub const NEGOTIATE_PROTOCOL_VERSION: u8 = b'v';
74}
75
76/// Raw message from the PostgreSQL server.
77///
78/// This is a thin wrapper around the message type byte and payload.
79/// Individual message types are parsed on demand by state machines.
80#[derive(Debug, Clone, Copy)]
81pub struct RawMessage<'a> {
82    /// Message type byte
83    pub type_byte: u8,
84    /// Message payload (after length field)
85    pub payload: &'a [u8],
86}
87
88impl<'a> RawMessage<'a> {
89    /// Create a new RawMessage.
90    pub fn new(type_byte: u8, payload: &'a [u8]) -> Self {
91        Self { type_byte, payload }
92    }
93
94    /// Check if this is an error response.
95    pub fn is_error(&self) -> bool {
96        self.type_byte == msg_type::ERROR_RESPONSE
97    }
98
99    /// Check if this is a notice response.
100    pub fn is_notice(&self) -> bool {
101        self.type_byte == msg_type::NOTICE_RESPONSE
102    }
103
104    /// Check if this is a notification response.
105    pub fn is_notification(&self) -> bool {
106        self.type_byte == msg_type::NOTIFICATION_RESPONSE
107    }
108
109    /// Check if this is a parameter status message.
110    pub fn is_parameter_status(&self) -> bool {
111        self.type_byte == msg_type::PARAMETER_STATUS
112    }
113
114    /// Check if this is an async message (can arrive at any time).
115    pub fn is_async(&self) -> bool {
116        Self::is_async_type(self.type_byte)
117    }
118
119    /// Check if a type byte represents an async message (can arrive at any time).
120    pub fn is_async_type(type_byte: u8) -> bool {
121        matches!(
122            type_byte,
123            msg_type::NOTICE_RESPONSE
124                | msg_type::NOTIFICATION_RESPONSE
125                | msg_type::PARAMETER_STATUS
126        )
127    }
128}