Skip to content

Commit 97adf66

Browse files
committed
errors: replace From<&> conversions with a trait
Partially fixes #177.
1 parent 34715e5 commit 97adf66

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

scylla-rust-wrapper/src/cass_error.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ use scylla::transport::errors::*;
44
pub(crate) use crate::cass_error_types::{CassError, CassErrorSource};
55
use crate::query_error::CassErrorResult;
66

7-
// TODO: From<ref> is bad practice. Will be replaced in the next commit.
8-
impl From<&CassErrorResult> for CassError {
9-
fn from(error: &CassErrorResult) -> Self {
10-
match error {
11-
CassErrorResult::Query(query_error) => CassError::from(query_error),
7+
pub trait ToCassError {
8+
fn to_cass_error(&self) -> CassError;
9+
}
10+
11+
impl ToCassError for CassErrorResult {
12+
fn to_cass_error(&self) -> CassError {
13+
match self {
14+
CassErrorResult::Query(query_error) => query_error.to_cass_error(),
1215
}
1316
}
1417
}
1518

16-
impl From<&QueryError> for CassError {
17-
fn from(error: &QueryError) -> Self {
18-
match error {
19-
QueryError::DbError(db_error, _string) => CassError::from(db_error),
20-
QueryError::BadQuery(bad_query) => CassError::from(bad_query),
19+
impl ToCassError for QueryError {
20+
fn to_cass_error(&self) -> CassError {
21+
match self {
22+
QueryError::DbError(db_error, _string) => db_error.to_cass_error(),
23+
QueryError::BadQuery(bad_query) => bad_query.to_cass_error(),
2124
QueryError::ProtocolError(_str) => CassError::CASS_ERROR_SERVER_PROTOCOL_ERROR,
2225
QueryError::TimeoutError => CassError::CASS_ERROR_LIB_REQUEST_TIMED_OUT, // This may be either read or write timeout error
2326
QueryError::UnableToAllocStreamId => CassError::CASS_ERROR_LIB_NO_STREAMS,
@@ -42,9 +45,9 @@ impl From<&QueryError> for CassError {
4245
}
4346
}
4447

45-
impl From<&DbError> for CassError {
46-
fn from(error: &DbError) -> Self {
47-
match error {
48+
impl ToCassError for DbError {
49+
fn to_cass_error(&self) -> CassError {
50+
match self {
4851
DbError::ServerError => CassError::CASS_ERROR_SERVER_SERVER_ERROR,
4952
DbError::ProtocolError => CassError::CASS_ERROR_SERVER_PROTOCOL_ERROR,
5053
DbError::AuthenticationError => CassError::CASS_ERROR_SERVER_BAD_CREDENTIALS,
@@ -72,9 +75,9 @@ impl From<&DbError> for CassError {
7275
}
7376
}
7477

75-
impl From<&BadQuery> for CassError {
76-
fn from(error: &BadQuery) -> Self {
77-
match error {
78+
impl ToCassError for BadQuery {
79+
fn to_cass_error(&self) -> CassError {
80+
match self {
7881
BadQuery::SerializeValuesError(_serialize_values_error) => {
7982
CassError::CASS_ERROR_LAST_ENTRY
8083
}
@@ -91,9 +94,9 @@ impl From<&BadQuery> for CassError {
9194
}
9295
}
9396

94-
impl From<&NewSessionError> for CassError {
95-
fn from(error: &NewSessionError) -> Self {
96-
match error {
97+
impl ToCassError for NewSessionError {
98+
fn to_cass_error(&self) -> CassError {
99+
match self {
97100
NewSessionError::FailedToResolveAnyHostname(_hostnames) => {
98101
CassError::CASS_ERROR_LIB_NO_HOSTS_AVAILABLE
99102
}
@@ -127,9 +130,9 @@ impl From<&NewSessionError> for CassError {
127130
}
128131
}
129132

130-
impl From<&BadKeyspaceName> for CassError {
131-
fn from(error: &BadKeyspaceName) -> Self {
132-
match error {
133+
impl ToCassError for BadKeyspaceName {
134+
fn to_cass_error(&self) -> CassError {
135+
match self {
133136
BadKeyspaceName::Empty => CassError::CASS_ERROR_LAST_ENTRY,
134137
BadKeyspaceName::TooLong(_string, _usize) => CassError::CASS_ERROR_LAST_ENTRY,
135138
BadKeyspaceName::IllegalCharacter(_string, _char) => CassError::CASS_ERROR_LAST_ENTRY,

scylla-rust-wrapper/src/future.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::argconv::*;
22
use crate::cass_error::CassError;
33
use crate::cass_error::CassErrorMessage;
4+
use crate::cass_error::ToCassError;
45
use crate::prepared::CassPrepared;
56
use crate::query_error::CassErrorResult;
67
use crate::query_result::CassResult;
@@ -320,7 +321,7 @@ pub unsafe extern "C" fn cass_future_ready(future_raw: *const CassFuture) -> cas
320321
#[no_mangle]
321322
pub unsafe extern "C" fn cass_future_error_code(future_raw: *const CassFuture) -> CassError {
322323
ptr_to_ref(future_raw).with_waited_result(|r: &mut CassFutureResult| match r {
323-
Ok(CassResultValue::QueryError(err)) => CassError::from(err.as_ref()),
324+
Ok(CassResultValue::QueryError(err)) => err.to_cass_error(),
324325
Err((err, _)) => *err,
325326
_ => CassError::CASS_OK,
326327
})

scylla-rust-wrapper/src/query_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub unsafe extern "C" fn cass_error_result_free(error_result: *const CassErrorRe
5555
#[no_mangle]
5656
pub unsafe extern "C" fn cass_error_result_code(error_result: *const CassErrorResult) -> CassError {
5757
let error_result: &CassErrorResult = ptr_to_ref(error_result);
58-
CassError::from(error_result)
58+
error_result.to_cass_error()
5959
}
6060

6161
#[no_mangle]

scylla-rust-wrapper/src/session.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl CassSessionInner {
128128
let session = session_builder
129129
.build()
130130
.await
131-
.map_err(|err| (CassError::from(&err), err.msg()))?;
131+
.map_err(|err| (err.to_cass_error(), err.msg()))?;
132132

133133
*session_guard = Some(CassSessionInner {
134134
session,
@@ -538,7 +538,7 @@ pub unsafe extern "C" fn cass_session_prepare_from_existing(
538538
let prepared = session
539539
.prepare(query.query.clone())
540540
.await
541-
.map_err(|err| (CassError::from(&err), err.msg()))?;
541+
.map_err(|err| (err.to_cass_error(), err.msg()))?;
542542

543543
Ok(CassResultValue::Prepared(Arc::new(
544544
CassPrepared::new_from_prepared_statement(prepared),
@@ -582,7 +582,7 @@ pub unsafe extern "C" fn cass_session_prepare_n(
582582
let mut prepared = session
583583
.prepare(query)
584584
.await
585-
.map_err(|err| (CassError::from(&err), err.msg()))?;
585+
.map_err(|err| (err.to_cass_error(), err.msg()))?;
586586

587587
// Set Cpp Driver default configuration for queries:
588588
prepared.set_consistency(Consistency::One);

0 commit comments

Comments
 (0)