aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/sessioninit.rs
blob: 18c026deebb5537b22c0e655c3db32f44c5e2d04 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::error::ProtocolError;
use crate::message::objects::Identity;
use crate::primitive::{BufferInfo, NetworkId, Variant, VariantList, VariantMap};
use crate::HandshakeSerialize;

/// SessionInit is received along with ClientLoginAck to initialize that user Session
// TODO Replace with proper types
#[derive(Debug, Clone)]
pub struct SessionInit {
    /// List of all configured identities
    pub identities: Vec<Identity>,
    /// List of all existing buffers
    pub buffers: Vec<BufferInfo>, // Vec<Variant::BufferInfo()>
    /// Ids of all networks
    pub network_ids: Vec<NetworkId>,
}

impl TryFrom<VariantMap> for SessionInit {
    type Error = ProtocolError;

    fn try_from(input: VariantMap) -> Result<Self, Self::Error> {
        let mut state: VariantMap = input
            .get("SessionState")
            .ok_or_else(|| ProtocolError::MissingField("SessionState".to_string()))?
            .try_into()?;

        log::trace!("sessionstate: {:#?}", state);

        let identities: VariantList = state
            .remove("Identities")
            .ok_or_else(|| ProtocolError::MissingField("Identities".to_string()))?
            .try_into()?;
        let buffers: VariantList = state
            .remove("BufferInfos")
            .ok_or_else(|| ProtocolError::MissingField("BufferInfos".to_string()))?
            .try_into()?;
        let network_ids: VariantList = state
            .remove("NetworkIds")
            .ok_or_else(|| ProtocolError::MissingField("NetworkIds".to_string()))?
            .try_into()?;

        Ok(SessionInit {
            identities: identities
                .into_iter()
                .map(|x| x.try_into())
                .collect::<Result<Vec<_>, _>>()?,
            buffers: buffers
                .iter()
                .map(|buffer| match buffer {
                    Variant::BufferInfo(buffer) => Ok(buffer.clone()),
                    _ => Err(ProtocolError::WrongVariant),
                })
                .collect::<Result<Vec<_>, _>>()?,
            network_ids: network_ids
                .iter()
                .map(|network| match network {
                    Variant::NetworkId(network) => Ok(*network),
                    _ => Err(ProtocolError::WrongVariant),
                })
                .collect::<Result<Vec<_>, _>>()?,
        })
    }
}

impl HandshakeSerialize for SessionInit {
    fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
        let mut values: VariantMap = VariantMap::with_capacity(4);
        values.insert("MsgType".to_string(), Variant::String("SessionInit".to_string()));
        // values.insert(
        //     "Identities".to_string(),
        //     Variant::VariantList(
        //         self.identities
        //             .iter()
        //             .map(|ident| Variant::VariantMap(ident.clone().into()))
        //             .collect(),
        //     ),
        // );
        values.insert(
            "BufferInfos".to_string(),
            Variant::VariantList(
                self.buffers
                    .iter()
                    .map(|buffer| Variant::BufferInfo(buffer.clone()))
                    .collect(),
            ),
        );
        values.insert(
            "NetworkIds".to_string(),
            Variant::VariantList(
                self.network_ids
                    .iter()
                    .map(|id| Variant::NetworkId(*id))
                    .collect(),
            ),
        );
        HandshakeSerialize::serialize(&values)
    }
}