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
use bytes::BytesMut;
use tokio::io;

use tokio_util::codec::{Decoder, Encoder};

use coap_lite::Packet;

pub struct Codec {}

impl Codec {
    pub fn new() -> Codec {
        Codec {}
    }
}

impl Decoder for Codec {
    type Item = Packet;
    type Error = io::Error;

    fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Packet>, io::Error> {
        if buf.len() == 0 {
            return Ok(None);
        }
        let result = (|| {
            let packet = Ok(Some(Packet::from_bytes(buf).map_err(|cause| {
                io::Error::new(io::ErrorKind::InvalidData, cause.to_string())
            })?));
            packet
        })();
        buf.clear();
        result
    }
}

impl Encoder<Packet> for Codec {
    type Error = io::Error;

    fn encode(&mut self, my_packet: Packet, buf: &mut BytesMut) -> Result<(), io::Error> {
        buf.extend_from_slice(&my_packet.to_bytes()
        .map_err(|cause| io::Error::new(io::ErrorKind::InvalidData, cause.to_string()))?[..]);
        Ok(())
    }
}