Skip to content

Commit

Permalink
Merge pull request AlexPikalov#132 from AlexPikalov/revise-unwraps
Browse files Browse the repository at this point in the history
Revise unwraps
  • Loading branch information
AlexPikalov committed Apr 6, 2017
2 parents eb35568 + c53b230 commit 8ca92a4
Show file tree
Hide file tree
Showing 27 changed files with 765 additions and 576 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ into appropriate Rust type:
```rust
use cdrs::error::{Result as CResult};

let res_body = parsed.get_body();
let res_body = parsed.get_body().unwrap();
let rows = res_body.into_rows().unwrap();
let messages: Vec<CResult<Message>> = rows
.iter()
Expand All @@ -303,7 +303,7 @@ struct Author {

//...
use cdrs::error::{Result as CResult};
let res_body = parsed.get_body();
let res_body = parsed.get_body().unwrap();
let rows = res_body.into_rows().unwrap();
let messages: Vec<CAuthor> = rows
.iter()
Expand Down Expand Up @@ -345,6 +345,7 @@ Prepare-execute query is also supported:
let executed = session.execute(query_id, execution_params, false, false)
.unwrap()
.get_body()
.unwrap()
.into_set_keyspace()
.unwrap();
```
Expand Down
10 changes: 10 additions & 0 deletions examples/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ fn prepare_query(session: &mut Session<NoneAuthenticator, TransportTcp>,
.prepare(query.to_string(), false, false)
.unwrap()
.get_body()
.unwrap()
.into_prepared()
.unwrap()
.id
Expand Down Expand Up @@ -270,6 +271,7 @@ fn select_all_ints(session: &mut Session<NoneAuthenticator, TransportTcp>) -> bo
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -339,6 +341,7 @@ fn select_table_str(session: &mut Session<NoneAuthenticator, TransportTcp>) -> b
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -388,6 +391,7 @@ fn select_table_list(session: &mut Session<NoneAuthenticator, TransportTcp>) ->
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -459,6 +463,7 @@ fn select_table_map(session: &mut Session<NoneAuthenticator, TransportTcp>) -> b
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -533,6 +538,7 @@ fn select_table_udt(session: &mut Session<NoneAuthenticator, TransportTcp>) -> b
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -570,6 +576,7 @@ fn select_table_bool(session: &mut Session<NoneAuthenticator, TransportTcp>) ->
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -609,6 +616,7 @@ fn select_table_uuid(session: &mut Session<NoneAuthenticator, TransportTcp>) ->
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -647,6 +655,7 @@ fn select_table_float(session: &mut Session<NoneAuthenticator, TransportTcp>) ->
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down Expand Up @@ -686,6 +695,7 @@ fn select_table_blob(session: &mut Session<NoneAuthenticator, TransportTcp>) ->
.query(select_query, false, false)
.unwrap()
.get_body()
.unwrap()
.into_rows()
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions examples/batch_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn main() {
.prepare(prepare_query, with_tracing, with_warnings)
.unwrap()
.get_body()
.unwrap()
.into_prepared()
.unwrap();

Expand Down
2 changes: 2 additions & 0 deletions examples/prepare_execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn main() {
.prepare(create_table_cql, with_tracing, with_warnings)
.unwrap()
.get_body()
.unwrap()
.into_prepared()
.unwrap();

Expand All @@ -39,6 +40,7 @@ fn main() {
.execute(query_id, execution_params, false, false)
.unwrap()
.get_body()
.unwrap()
.into_set_keyspace()
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/read_table_into_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() {

match query_op {
Ok(res) => {
let res_body = res.get_body();
let res_body = res.get_body().unwrap();
if let Some(rows) = res_body.into_rows() {
let employees: Vec<Employee> = rows.iter()
.map(|row| {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() {
match query_op {
Ok(res) => {
let res_body = res.get_body();
if let Some(rows) = res_body.into_rows() {
if let Some(rows) = res_body.unwrap().into_rows() {
let employees: Vec<Employee> = rows.iter()
.map(|row| {
let mut employee = Employee { ..Default::default() };
Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a, T: Authenticator + 'a, X: CDRSTransport + 'a> CDRS<T, X> {
parse_frame(&mut self.transport, &self.compressor)
.map(|frame| {
match frame.get_body() {
ResponseBody::Supported(ref supported_body) => supported_body.data.clone(),
Ok(ResponseBody::Supported(ref supported_body)) => supported_body.data.clone(),
_ => unreachable!(),
}
})
Expand All @@ -79,7 +79,7 @@ impl<'a, T: Authenticator + 'a, X: CDRSTransport + 'a> CDRS<T, X> {
}

if start_response.opcode == Opcode::Authenticate {
let body = start_response.get_body();
let body = start_response.get_body()?;
let authenticator =
body.get_authenticator()
.expect("Cassandra Server did communicate that it needed password
Expand Down
127 changes: 69 additions & 58 deletions src/consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
use std::io;
use std::convert::From;
use std::default::Default;
use super::{IntoBytes, FromCursor};
use super::types::*;
use super::FromBytes;

use {IntoBytes, FromCursor, FromBytes};
use error;
use types::*;

/// `Consistency` is an enum which represents Cassandra's consistency levels.
/// To find more details about each consistency level please refer to Cassandra official docs.
Expand Down Expand Up @@ -99,46 +100,48 @@ impl IntoBytes for Consistency {

impl From<i32> for Consistency {
fn from(bytes: i32) -> Consistency {
return match bytes {
0x0000 => Consistency::Any,
0x0001 => Consistency::One,
0x0002 => Consistency::Two,
0x0003 => Consistency::Three,
0x0004 => Consistency::Quorum,
0x0005 => Consistency::All,
0x0006 => Consistency::LocalQuorum,
0x0007 => Consistency::EachQuorum,
0x0008 => Consistency::Serial,
0x0009 => Consistency::LocalSerial,
0x000A => Consistency::LocalOne,
_ => Consistency::Unknown,
};
match bytes {
0x0000 => Consistency::Any,
0x0001 => Consistency::One,
0x0002 => Consistency::Two,
0x0003 => Consistency::Three,
0x0004 => Consistency::Quorum,
0x0005 => Consistency::All,
0x0006 => Consistency::LocalQuorum,
0x0007 => Consistency::EachQuorum,
0x0008 => Consistency::Serial,
0x0009 => Consistency::LocalSerial,
0x000A => Consistency::LocalOne,
_ => Consistency::Unknown,
}
}
}

impl FromBytes for Consistency {
fn from_bytes(bytes: &[u8]) -> Consistency {
return match from_bytes(bytes) {
0x0000 => Consistency::Any,
0x0001 => Consistency::One,
0x0002 => Consistency::Two,
0x0003 => Consistency::Three,
0x0004 => Consistency::Quorum,
0x0005 => Consistency::All,
0x0006 => Consistency::LocalQuorum,
0x0007 => Consistency::EachQuorum,
0x0008 => Consistency::Serial,
0x0009 => Consistency::LocalSerial,
0x000A => Consistency::LocalOne,
_ => Consistency::Unknown,
};
fn from_bytes(bytes: &[u8]) -> error::Result<Consistency> {
try_from_bytes(bytes)
.map_err(Into::into)
.map(|b| match b {
0x0000 => Consistency::Any,
0x0001 => Consistency::One,
0x0002 => Consistency::Two,
0x0003 => Consistency::Three,
0x0004 => Consistency::Quorum,
0x0005 => Consistency::All,
0x0006 => Consistency::LocalQuorum,
0x0007 => Consistency::EachQuorum,
0x0008 => Consistency::Serial,
0x0009 => Consistency::LocalSerial,
0x000A => Consistency::LocalOne,
_ => Consistency::Unknown,
})
}
}

impl FromCursor for Consistency {
fn from_cursor(mut cursor: &mut io::Cursor<&[u8]>) -> Consistency {
let consistency_num = CIntShort::from_cursor(&mut cursor) as i32;
return Consistency::from(consistency_num);
fn from_cursor(mut cursor: &mut io::Cursor<&[u8]>) -> error::Result<Consistency> {
let consistency_num = CIntShort::from_cursor(&mut cursor)? as i32;
Ok(Consistency::from(consistency_num))
}
}

Expand Down Expand Up @@ -184,43 +187,51 @@ mod tests {

#[test]
fn test_consistency_from_bytes() {
assert_eq!(Consistency::from_bytes(&[0, 0]), Consistency::Any);
assert_eq!(Consistency::from_bytes(&[0, 1]), Consistency::One);
assert_eq!(Consistency::from_bytes(&[0, 2]), Consistency::Two);
assert_eq!(Consistency::from_bytes(&[0, 3]), Consistency::Three);
assert_eq!(Consistency::from_bytes(&[0, 4]), Consistency::Quorum);
assert_eq!(Consistency::from_bytes(&[0, 5]), Consistency::All);
assert_eq!(Consistency::from_bytes(&[0, 6]), Consistency::LocalQuorum);
assert_eq!(Consistency::from_bytes(&[0, 7]), Consistency::EachQuorum);
assert_eq!(Consistency::from_bytes(&[0, 8]), Consistency::Serial);
assert_eq!(Consistency::from_bytes(&[0, 9]), Consistency::LocalSerial);
assert_eq!(Consistency::from_bytes(&[0, 10]), Consistency::LocalOne);
assert_eq!(Consistency::from_bytes(&[0, 11]), Consistency::Unknown);
assert_eq!(Consistency::from_bytes(&[0, 0]).unwrap(), Consistency::Any);
assert_eq!(Consistency::from_bytes(&[0, 1]).unwrap(), Consistency::One);
assert_eq!(Consistency::from_bytes(&[0, 2]).unwrap(), Consistency::Two);
assert_eq!(Consistency::from_bytes(&[0, 3]).unwrap(),
Consistency::Three);
assert_eq!(Consistency::from_bytes(&[0, 4]).unwrap(),
Consistency::Quorum);
assert_eq!(Consistency::from_bytes(&[0, 5]).unwrap(), Consistency::All);
assert_eq!(Consistency::from_bytes(&[0, 6]).unwrap(),
Consistency::LocalQuorum);
assert_eq!(Consistency::from_bytes(&[0, 7]).unwrap(),
Consistency::EachQuorum);
assert_eq!(Consistency::from_bytes(&[0, 8]).unwrap(),
Consistency::Serial);
assert_eq!(Consistency::from_bytes(&[0, 9]).unwrap(),
Consistency::LocalSerial);
assert_eq!(Consistency::from_bytes(&[0, 10]).unwrap(),
Consistency::LocalOne);
assert_eq!(Consistency::from_bytes(&[0, 11]).unwrap(),
Consistency::Unknown);
}

#[test]
fn test_consistency_from_cursor() {
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 0])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 0])).unwrap(),
Consistency::Any);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 1])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 1])).unwrap(),
Consistency::One);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 2])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 2])).unwrap(),
Consistency::Two);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 3])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 3])).unwrap(),
Consistency::Three);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 4])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 4])).unwrap(),
Consistency::Quorum);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 5])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 5])).unwrap(),
Consistency::All);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 6])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 6])).unwrap(),
Consistency::LocalQuorum);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 7])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 7])).unwrap(),
Consistency::EachQuorum);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 8])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 8])).unwrap(),
Consistency::Serial);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 9])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 9])).unwrap(),
Consistency::LocalSerial);
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 10])),
assert_eq!(Consistency::from_cursor(&mut Cursor::new(&[0, 10])).unwrap(),
Consistency::LocalOne);
}

Expand Down
3 changes: 2 additions & 1 deletion src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ impl<X: CDRSTransport> Listener<X> {
pub fn start(&mut self, compressor: &Compression) -> error::Result<()> {
loop {
let event_opt = try!(parse_frame(&mut self.transport, compressor))
.get_body()
.get_body()?
.into_server_event();

let event = if event_opt.is_some() {
// unwrap is safe is we've checked that event_opt.is_some()
event_opt.unwrap().event as ServerEvent
} else {
continue;
Expand Down

0 comments on commit 8ca92a4

Please sign in to comment.