Skip to content

Commit d199c74

Browse files
committed
Add NonErrorResponse and NonErrorQueryResponse
Response enum can contain Response::Error. Sometimes we would like to make sure that some response is not an error response. NonErrorResponse is an enum which has all variants of Response except for Error. NonErrorQueryResponse is like QueryRespose where the response field is a NonErrorResponse instead of Response. Signed-off-by: Jan Ciolek <[email protected]>
1 parent 3ee5f33 commit d199c74

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

scylla-cql/src/frame/response/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod event;
55
pub mod result;
66
pub mod supported;
77

8-
use crate::frame::frame_errors::ParseError;
8+
use crate::{errors::QueryError, frame::frame_errors::ParseError};
99
use num_enum::TryFromPrimitive;
1010

1111
pub use error::Error;
@@ -57,4 +57,29 @@ impl Response {
5757

5858
Ok(response)
5959
}
60+
61+
pub fn into_non_error_response(self) -> Result<NonErrorResponse, QueryError> {
62+
Ok(match self {
63+
Response::Error(err) => return Err(QueryError::from(err)),
64+
Response::Ready => NonErrorResponse::Ready,
65+
Response::Result(res) => NonErrorResponse::Result(res),
66+
Response::Authenticate(auth) => NonErrorResponse::Authenticate(auth),
67+
Response::AuthSuccess(auth_succ) => NonErrorResponse::AuthSuccess(auth_succ),
68+
Response::AuthChallenge(auth_chal) => NonErrorResponse::AuthChallenge(auth_chal),
69+
Response::Supported(sup) => NonErrorResponse::Supported(sup),
70+
Response::Event(eve) => NonErrorResponse::Event(eve),
71+
})
72+
}
73+
}
74+
75+
// A Response which can not be Response::Error
76+
#[derive(Debug)]
77+
pub enum NonErrorResponse {
78+
Ready,
79+
Result(result::Result),
80+
Authenticate(authenticate::Authenticate),
81+
AuthSuccess(authenticate::AuthSuccess),
82+
AuthChallenge(authenticate::AuthChallenge),
83+
Supported(Supported),
84+
Event(event::Event),
6085
}

scylla/src/transport/connection.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::batch::{Batch, BatchStatement};
3232
use crate::frame::{
3333
self,
3434
request::{self, batch, execute, query, register, Request},
35-
response::{event::Event, result, Response, ResponseOpcode},
35+
response::{event::Event, result, NonErrorResponse, Response, ResponseOpcode},
3636
server_event_type::EventType,
3737
value::{BatchValues, ValueList},
3838
FrameParams, SerializedRequest,
@@ -142,6 +142,13 @@ pub struct QueryResponse {
142142
pub warnings: Vec<String>,
143143
}
144144

145+
// A QueryResponse in which response can not be Response::Error
146+
pub struct NonErrorQueryResponse {
147+
pub response: NonErrorResponse,
148+
pub tracing_id: Option<Uuid>,
149+
pub warnings: Vec<String>,
150+
}
151+
145152
/// Result of Session::batch(). Contains no rows, only some useful information.
146153
pub struct BatchResult {
147154
/// Warnings returned by the database
@@ -165,15 +172,28 @@ impl QueryResponse {
165172
}
166173
}
167174

175+
pub fn into_non_error_query_response(self) -> Result<NonErrorQueryResponse, QueryError> {
176+
Ok(NonErrorQueryResponse {
177+
response: self.response.into_non_error_response()?,
178+
tracing_id: self.tracing_id,
179+
warnings: self.warnings,
180+
})
181+
}
182+
183+
pub fn into_query_result(self) -> Result<QueryResult, QueryError> {
184+
self.into_non_error_query_response()?.into_query_result()
185+
}
186+
}
187+
188+
impl NonErrorQueryResponse {
168189
pub fn into_query_result(self) -> Result<QueryResult, QueryError> {
169190
let (rows, paging_state, col_specs) = match self.response {
170-
Response::Error(err) => return Err(err.into()),
171-
Response::Result(result::Result::Rows(rs)) => (
191+
NonErrorResponse::Result(result::Result::Rows(rs)) => (
172192
Some(rs.rows),
173193
rs.metadata.paging_state,
174194
rs.metadata.col_specs,
175195
),
176-
Response::Result(_) => (None, None, vec![]),
196+
NonErrorResponse::Result(_) => (None, None, vec![]),
177197
_ => {
178198
return Err(QueryError::ProtocolError(
179199
"Unexpected server response, expected Result or Error",

0 commit comments

Comments
 (0)