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 Run {
    pub(crate) statement: String,
    pub(crate) parameters: HashMap<String, Value>,
}

impl Run {
    pub fn new(statement: String, parameters: HashMap<String, Value>) -> Self {
        Self {
            statement,
            parameters,
        }
    }
}

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

    fn try_from(bolt_run: bolt::message::Run) -> Result<Self, Self::Error> {
        Ok(Run {
            statement: bolt_run.statement.try_into()?,
            parameters: bolt_run.parameters.try_into()?,
        })
    }
}

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

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