sqlx_postgres/message/
close.rs

1use crate::io::{PgBufMutExt, PortalId, StatementId};
2use crate::message::{FrontendMessage, FrontendMessageFormat};
3use std::num::Saturating;
4
5const CLOSE_PORTAL: u8 = b'P';
6const CLOSE_STATEMENT: u8 = b'S';
7
8#[derive(Debug)]
9#[allow(dead_code)]
10pub enum Close {
11    Statement(StatementId),
12    Portal(PortalId),
13}
14
15impl FrontendMessage for Close {
16    const FORMAT: FrontendMessageFormat = FrontendMessageFormat::Close;
17
18    fn body_size_hint(&self) -> Saturating<usize> {
19        // Either `CLOSE_PORTAL` or `CLOSE_STATEMENT`
20        let mut size = Saturating(1);
21
22        match self {
23            Close::Statement(id) => size += id.name_len(),
24            Close::Portal(id) => size += id.name_len(),
25        }
26
27        size
28    }
29
30    fn encode_body(&self, buf: &mut Vec<u8>) -> Result<(), crate::Error> {
31        match self {
32            Close::Statement(id) => {
33                buf.push(CLOSE_STATEMENT);
34                buf.put_statement_name(*id);
35            }
36
37            Close::Portal(id) => {
38                buf.push(CLOSE_PORTAL);
39                buf.put_portal_name(*id);
40            }
41        }
42
43        Ok(())
44    }
45}