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
46
47
48
49
50
51
52
53
use bytes::{BufMut, BytesMut};
use cookie_factory::gen;
use dpmaster_proto::deserializer::getserversresponse_message;
use dpmaster_proto::messages::{GetServersMessage, GetServersResponseMessage};
use dpmaster_proto::serializer::gen_getservers_message;
use tokio_util::codec::{Decoder, Encoder};

pub struct GameClientCodec(());

impl GameClientCodec {
    pub fn new() -> Self {
        Self(())
    }
}

impl Encoder<GetServersMessage> for GameClientCodec {
    type Error = std::io::Error;

    fn encode(&mut self, item: GetServersMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
        gen(gen_getservers_message(&item), dst.writer())
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)) // TODO
            .map(|_| ())
    }
}

impl Decoder for GameClientCodec {
    type Item = GetServersResponseMessage;
    type Error = std::io::Error;

    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        if src.is_empty() {
            Ok(None)
        } else {
            let msg = getserversresponse_message(&src[..]);
            match msg {
                Err(_e) => Err(std::io::Error::new(std::io::ErrorKind::Other, "uhoh")), // TODO
                Ok((_i, msg)) => {
                    // the parser operates on whole packets, so we can assume it parsed one on success
                    src.clear();
                    Ok(Some(msg))
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}