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
use bytes::BytesMut;
use tokio_util::codec;

use super::Packet;
use crate::errors::Error;
use std::fmt::Write;

#[derive(Default)]
pub struct Encoder {}

/// Turn a an unstructured Packet into a UDP bytestream
impl codec::Encoder<Packet> for Encoder {
    type Error = Error;

    #[allow(clippy::write_with_newline)]
    fn encode(&mut self, p: Packet, dst: &mut BytesMut) -> Result<(), Self::Error> {
        write!(dst, "{}\r\n", p.typ.to_string())?;
        p.headers
            .iter()
            .map(|(k, v)| write!(dst, "{}: {}\r\n", k.to_uppercase(), v))
            .collect::<Result<(), std::fmt::Error>>()?;
        write!(dst, "\r\n")?;
        Ok(())
    }
}