aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/clientloginack.rs
blob: de262747309cf094776d68a1f6a427ad93d3c3e9 (plain)
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
use crate::error::ProtocolError;
use crate::primitive::{Variant, VariantMap};
use crate::{HandshakeDeserialize, HandshakeSerialize};

/// ClientLoginAck is received after the client has successfully logged in
/// it has no fields
#[derive(Debug, Clone)]
pub struct ClientLoginAck;

impl HandshakeSerialize for ClientLoginAck {
    fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
        let mut values: VariantMap = VariantMap::with_capacity(1);
        values.insert(
            "MsgType".to_string(),
            Variant::String("ClientLoginAck".to_string()),
        );
        HandshakeSerialize::serialize(&values)
    }
}

impl HandshakeDeserialize for ClientLoginAck {
    fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
        let (len, mut values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;

        let msgtype: String = values.remove("MsgType").unwrap().try_into().unwrap();

        if msgtype == "ClientLogin" {
            Ok((len, Self {}))
        } else {
            Err(ProtocolError::WrongMsgType)
        }
    }
}