sqlx_postgres/message/
close.rs

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
use crate::io::{PgBufMutExt, PortalId, StatementId};
use crate::message::{FrontendMessage, FrontendMessageFormat};
use std::num::Saturating;

const CLOSE_PORTAL: u8 = b'P';
const CLOSE_STATEMENT: u8 = b'S';

#[derive(Debug)]
#[allow(dead_code)]
pub enum Close {
    Statement(StatementId),
    Portal(PortalId),
}

impl FrontendMessage for Close {
    const FORMAT: FrontendMessageFormat = FrontendMessageFormat::Close;

    fn body_size_hint(&self) -> Saturating<usize> {
        // Either `CLOSE_PORTAL` or `CLOSE_STATEMENT`
        let mut size = Saturating(1);

        match self {
            Close::Statement(id) => size += id.name_len(),
            Close::Portal(id) => size += id.name_len(),
        }

        size
    }

    fn encode_body(&self, buf: &mut Vec<u8>) -> Result<(), crate::Error> {
        match self {
            Close::Statement(id) => {
                buf.push(CLOSE_STATEMENT);
                buf.put_statement_name(*id);
            }

            Close::Portal(id) => {
                buf.push(CLOSE_PORTAL);
                buf.put_portal_name(*id);
            }
        }

        Ok(())
    }
}