sqlx_core_guts/postgres/message/
notification.rs

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