sqlx_postgres/message/
notification.rs

1use sqlx_core::bytes::{Buf, Bytes};
2
3use crate::error::Error;
4use crate::io::BufExt;
5use crate::message::{BackendMessage, BackendMessageFormat};
6
7#[derive(Debug)]
8pub struct Notification {
9    pub(crate) process_id: u32,
10    pub(crate) channel: Bytes,
11    pub(crate) payload: Bytes,
12}
13
14impl BackendMessage for Notification {
15    const FORMAT: BackendMessageFormat = BackendMessageFormat::NotificationResponse;
16
17    fn decode_body(mut buf: Bytes) -> Result<Self, Error> {
18        let process_id = buf.get_u32();
19        let channel = buf.get_bytes_nul()?;
20        let payload = buf.get_bytes_nul()?;
21
22        Ok(Self {
23            process_id,
24            channel,
25            payload,
26        })
27    }
28}
29
30#[test]
31fn test_decode_notification_response() {
32    const NOTIFICATION_RESPONSE: &[u8] = b"\x34\x20\x10\x02TEST-CHANNEL\0THIS IS A TEST\0";
33
34    let message = Notification::decode_body(Bytes::from(NOTIFICATION_RESPONSE)).unwrap();
35
36    assert_eq!(message.process_id, 0x34201002);
37    assert_eq!(&*message.channel, &b"TEST-CHANNEL"[..]);
38    assert_eq!(&*message.payload, &b"THIS IS A TEST"[..]);
39}