-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathmessages.rs
180 lines (162 loc) · 4.78 KB
/
messages.rs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
mod begin;
mod bye;
mod commit;
mod discard;
mod failure;
mod hello;
mod pull;
mod record;
mod reset;
mod rollback;
mod run;
mod success;
use crate::{
errors::{Error, Result},
types::{BoltMap, BoltWireFormat},
version::Version,
BoltString, BoltType,
};
use begin::Begin;
use bytes::Bytes;
use commit::Commit;
use discard::Discard;
use failure::Failure;
use hello::Hello;
use pull::Pull;
use record::Record;
use reset::Reset;
use rollback::Rollback;
use run::Run;
pub(crate) use success::Success;
#[derive(Debug, PartialEq, Clone)]
pub enum BoltResponse {
Success(Success),
Failure(Failure),
Record(Record),
}
#[derive(Debug, PartialEq, Clone)]
pub enum BoltRequest {
Hello(Hello),
Run(Run),
Pull(Pull),
Discard(Discard),
Begin(Begin),
Commit(Commit),
Rollback(Rollback),
Reset(Reset),
}
pub struct HelloBuilder {
agent: BoltString,
principal: BoltString,
credentials: BoltString,
routing: Option<BoltMap>,
version: Version,
}
impl HelloBuilder {
pub fn new(principal: impl Into<BoltString>, credentials: impl Into<BoltString>) -> Self {
Self {
agent: "neo4rs".into(),
principal: principal.into(),
credentials: credentials.into(),
routing: None,
version: Version::V4,
}
}
pub fn with_routing(self, routing: impl Into<Option<BoltMap>>) -> Self {
Self {
routing: routing.into(),
..self
}
}
pub fn with_version(self, version: Version) -> Self {
Self { version, ..self }
}
pub fn build(self) -> BoltRequest {
let HelloBuilder {
agent,
principal,
credentials,
routing,
version,
} = self;
BoltRequest::hello(agent, principal, credentials, routing, version)
}
}
impl BoltRequest {
pub fn hello(
agent: BoltString,
principal: BoltString,
credentials: BoltString,
routing: Option<BoltMap>,
version: Version,
) -> BoltRequest {
let mut data = BoltMap::default();
data.put("user_agent".into(), BoltType::String(agent));
data.put("scheme".into(), "basic".into());
data.put("principal".into(), BoltType::String(principal));
data.put("credentials".into(), BoltType::String(credentials));
if version >= Version::V4_1 {
if let Some(context) = routing {
data.put("routing".into(), BoltType::Map(context));
}
}
BoltRequest::Hello(Hello::new(data))
}
pub fn run(db: &str, query: &str, params: BoltMap) -> BoltRequest {
BoltRequest::Run(Run::new(db.into(), query.into(), params))
}
pub fn pull(n: usize, qid: i64) -> BoltRequest {
BoltRequest::Pull(Pull::new(n as i64, qid))
}
pub fn discard() -> BoltRequest {
BoltRequest::Discard(Discard::default())
}
pub fn begin(db: &str) -> BoltRequest {
let begin = Begin::new([("db".into(), db.into())].into_iter().collect());
BoltRequest::Begin(begin)
}
pub fn commit() -> BoltRequest {
BoltRequest::Commit(Commit::new())
}
pub fn rollback() -> BoltRequest {
BoltRequest::Rollback(Rollback::new())
}
pub fn reset() -> BoltRequest {
BoltRequest::Reset(Reset::new())
}
}
impl BoltRequest {
pub fn into_bytes(self, version: Version) -> Result<Bytes> {
let bytes: Bytes = match self {
BoltRequest::Hello(hello) => hello.into_bytes(version)?,
BoltRequest::Run(run) => run.into_bytes(version)?,
BoltRequest::Pull(pull) => pull.into_bytes(version)?,
BoltRequest::Discard(discard) => discard.into_bytes(version)?,
BoltRequest::Begin(begin) => begin.into_bytes(version)?,
BoltRequest::Commit(commit) => commit.into_bytes(version)?,
BoltRequest::Rollback(rollback) => rollback.into_bytes(version)?,
BoltRequest::Reset(reset) => reset.into_bytes(version)?,
};
Ok(bytes)
}
}
impl BoltResponse {
pub fn parse(version: Version, mut response: Bytes) -> Result<BoltResponse> {
if Success::can_parse(version, &response) {
let success = Success::parse(version, &mut response)?;
return Ok(BoltResponse::Success(success));
}
if Failure::can_parse(version, &response) {
let failure = Failure::parse(version, &mut response)?;
return Ok(BoltResponse::Failure(failure));
}
if Record::can_parse(version, &response) {
let record = Record::parse(version, &mut response)?;
return Ok(BoltResponse::Record(record));
}
Err(Error::UnknownMessage(format!(
"unknown message {:?}",
response
)))
}
}