dust_devil_core/sandstorm/
meow.rs

1use std::io::{Error, ErrorKind};
2
3use tokio::io::AsyncReadExt;
4
5use crate::serialize::{ByteRead, ByteWrite};
6
7use super::SandstormCommandType;
8
9/// A Sandstorm meow (ping) request.
10pub struct MeowRequest;
11
12/// A Sandstorm meow (ping) response.
13pub struct MeowResponse;
14
15impl ByteRead for MeowRequest {
16    async fn read<R: tokio::io::AsyncRead + Unpin + ?Sized>(_reader: &mut R) -> Result<Self, Error> {
17        Ok(Self)
18    }
19}
20
21impl ByteWrite for MeowRequest {
22    async fn write<W: tokio::io::AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
23        SandstormCommandType::Meow.write(writer).await
24    }
25}
26
27impl ByteRead for MeowResponse {
28    async fn read<R: tokio::io::AsyncRead + Unpin + ?Sized>(reader: &mut R) -> Result<Self, Error> {
29        let mut meow = [0u8; 4];
30        reader.read_exact(&mut meow).await?;
31
32        if meow == [b'M', b'E', b'O', b'W'] {
33            Ok(Self)
34        } else {
35            Err(Error::new(
36                ErrorKind::InvalidData,
37                "Server responded to meow, but did not say MEOW!",
38            ))
39        }
40    }
41}
42
43impl ByteWrite for MeowResponse {
44    async fn write<W: tokio::io::AsyncWrite + Unpin + ?Sized>(&self, writer: &mut W) -> Result<(), Error> {
45        (SandstormCommandType::Meow, b'M', b'E', b'O', b'W').write(writer).await
46    }
47}