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
use std::io::Cursor;
use crate::error;
use crate::frame::frame_auth_challenge::*;
use crate::frame::frame_auth_success::BodyReqAuthSuccess;
use crate::frame::frame_authenticate::BodyResAuthenticate;
use crate::frame::frame_error::CDRSError;
use crate::frame::frame_event::BodyResEvent;
use crate::frame::frame_result::{
BodyResResultPrepared, BodyResResultRows, BodyResResultSetKeyspace, BodyResResultVoid,
ResResultBody, RowsMetadata,
};
use crate::frame::frame_supported::*;
use crate::frame::FromCursor;
use crate::frame::Opcode;
use crate::types::rows::Row;
#[derive(Debug)]
pub enum ResponseBody {
Error(CDRSError),
Startup,
Ready(BodyResResultVoid),
Authenticate(BodyResAuthenticate),
Options,
Supported(BodyResSupported),
Query,
Result(ResResultBody),
Prepare,
Execute,
Register,
Event(BodyResEvent),
Batch,
AuthChallenge(BodyResAuthChallenge),
AuthResponse,
AuthSuccess(BodyReqAuthSuccess),
}
impl ResponseBody {
pub fn from(bytes: &[u8], response_type: &Opcode) -> error::Result<ResponseBody> {
let mut cursor: Cursor<&[u8]> = Cursor::new(bytes);
Ok(match *response_type {
Opcode::Startup => unreachable!(),
Opcode::Options => unreachable!(),
Opcode::Query => unreachable!(),
Opcode::Prepare => unreachable!(),
Opcode::Execute => unreachable!(),
Opcode::Register => unreachable!(),
Opcode::Batch => unreachable!(),
Opcode::AuthResponse => unreachable!(),
Opcode::Error => ResponseBody::Error(CDRSError::from_cursor(&mut cursor)?),
Opcode::Ready => ResponseBody::Ready(BodyResResultVoid::from_cursor(&mut cursor)?),
Opcode::Authenticate => {
ResponseBody::Authenticate(BodyResAuthenticate::from_cursor(&mut cursor)?)
}
Opcode::Supported => {
ResponseBody::Supported(BodyResSupported::from_cursor(&mut cursor)?)
}
Opcode::Result => ResponseBody::Result(ResResultBody::from_cursor(&mut cursor)?),
Opcode::Event => ResponseBody::Event(BodyResEvent::from_cursor(&mut cursor)?),
Opcode::AuthChallenge => {
ResponseBody::AuthChallenge(BodyResAuthChallenge::from_cursor(&mut cursor)?)
}
Opcode::AuthSuccess => {
ResponseBody::AuthSuccess(BodyReqAuthSuccess::from_cursor(&mut cursor)?)
}
})
}
pub fn into_rows(self) -> Option<Vec<Row>> {
match self {
ResponseBody::Result(res) => res.into_rows(),
_ => None,
}
}
pub fn as_rows_metadata(&self) -> Option<RowsMetadata> {
match *self {
ResponseBody::Result(ref res) => res.as_rows_metadata(),
_ => None,
}
}
pub fn as_cols(&self) -> Option<&BodyResResultRows> {
match *self {
ResponseBody::Result(ref res) => match res {
&ResResultBody::Rows(ref rows) => Some(rows),
_ => None,
},
_ => None,
}
}
pub fn into_prepared(self) -> Option<BodyResResultPrepared> {
match self {
ResponseBody::Result(res) => res.into_prepared(),
_ => None,
}
}
pub fn into_set_keyspace(self) -> Option<BodyResResultSetKeyspace> {
match self {
ResponseBody::Result(res) => res.into_set_keyspace(),
_ => None,
}
}
pub fn into_server_event(self) -> Option<BodyResEvent> {
match self {
ResponseBody::Event(event) => Some(event),
_ => None,
}
}
pub fn get_authenticator<'a>(&'a self) -> Option<&'a str> {
match *self {
ResponseBody::Authenticate(ref auth) => Some(auth.data.as_str()),
_ => None,
}
}
}