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
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};

use failure::Error;

use crate::bolt;
use crate::bolt::Message;
use crate::bolt::Value;
use crate::error::MessageError;

#[derive(Debug)]
pub struct Init {
    pub(crate) client_name: String,
    pub(crate) auth_token: HashMap<String, Value>,
}

impl Init {
    pub fn new(client_name: String, auth_token: HashMap<String, Value>) -> Self {
        Self {
            client_name,
            auth_token,
        }
    }
}

impl TryFrom<bolt::message::Init> for Init {
    type Error = Error;

    fn try_from(bolt_init: bolt::message::Init) -> Result<Self, Self::Error> {
        Ok(Init {
            client_name: String::try_from(bolt_init.client_name)?,
            auth_token: bolt_init.auth_token.try_into()?,
        })
    }
}

impl TryFrom<Message> for Init {
    type Error = Error;

    fn try_from(message: Message) -> Result<Self, Self::Error> {
        match message {
            Message::Init(init) => Ok(Init::try_from(init)?),
            _ => Err(MessageError::InvalidConversion(message).into()),
        }
    }
}