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

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

#[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,
        }
    }

    pub fn statement(&self) -> &str {
        &self.statement
    }

    pub fn parameters(&self) -> &HashMap<String, Value> {
        &self.parameters
    }
}

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

    fn try_from(bolt_run: bolt::message::Run) -> Result<Self> {
        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> {
        match message {
            Message::Run(run) => Ok(Run::try_from(run)?),
            _ => Err(MessageError::InvalidConversion(message).into()),
        }
    }
}