Browse Source

Replace uses of try! with ?

pull/2/head
Kamil Kisiel 3 years ago
parent
commit
2e2fef6bc1
2 changed files with 28 additions and 29 deletions
  1. +24
    -25
      src/decoder.rs
  2. +4
    -4
      src/encoder.rs

+ 24
- 25
src/decoder.rs View File

@ -28,11 +28,11 @@ pub fn decode(msg: &[u8]) -> Result {
fn decode_message(msg: &[u8]) -> Result<OscPacket> {
let mut cursor: io::Cursor<&[u8]> = io::Cursor::new(msg);
let addr: String = try!(read_osc_string(&mut cursor));
let type_tags: String = try!(read_osc_string(&mut cursor));
let addr: String = read_osc_string(&mut cursor)?;
let type_tags: String = read_osc_string(&mut cursor)?;
if type_tags.len() > 1 {
let args: Vec<OscType> = try!(read_osc_args(&mut cursor, type_tags));
let args: Vec<OscType> = read_osc_args(&mut cursor, type_tags)?;
Ok(OscPacket::Message(OscMessage {
addr: addr,
@ -49,26 +49,26 @@ fn decode_message(msg: &[u8]) -> Result {
fn decode_bundle(msg: &[u8]) -> Result<OscPacket> {
let mut cursor: io::Cursor<&[u8]> = io::Cursor::new(msg);
let bundle_tag = try!(read_osc_string(&mut cursor));
let bundle_tag = read_osc_string(&mut cursor)?;
if bundle_tag != "#bundle" {
return Err(OscError::BadBundle(format!("Wrong bundle specifier: {}", bundle_tag)));
}
let time_tag = try!(read_time_tag(&mut cursor));
let time_tag = read_time_tag(&mut cursor)?;
let mut bundle: Vec<OscPacket> = Vec::new();
let mut elem_size = try!(read_bundle_element_size(&mut cursor));
let mut elem_size = read_bundle_element_size(&mut cursor)?;
while msg.len() >= (cursor.position() as usize) + elem_size {
let packet = try!(read_bundle_element(&mut cursor, elem_size));
let packet = read_bundle_element(&mut cursor, elem_size)?;
bundle.push(packet);
if msg.len() == cursor.position() as usize {
break;
}
elem_size = try!(read_bundle_element_size(&mut cursor));
elem_size = read_bundle_element_size(&mut cursor)?;
}
Ok(OscPacket::Bundle(OscBundle {
@ -88,8 +88,7 @@ fn read_bundle_element(cursor: &mut io::Cursor<&[u8]>, elem_size: usize) -> Resu
let mut handle = cursor.take(elem_size as u64);
let cnt = try!(handle.read_to_end(&mut buf)
.map_err(OscError::ReadError));
let cnt = handle.read_to_end(&mut buf).map_err(OscError::ReadError)?;
if cnt == elem_size {
decode(&buf)
@ -100,8 +99,8 @@ fn read_bundle_element(cursor: &mut io::Cursor<&[u8]>, elem_size: usize) -> Resu
fn read_osc_string(cursor: &mut io::Cursor<&[u8]>) -> Result<String> {
let mut str_buf: Vec<u8> = Vec::new();
try!(cursor.read_until(0, &mut str_buf)
.map_err(OscError::ReadError)); // ignore returned byte count
// ignore returned byte count
cursor.read_until(0, &mut str_buf).map_err(OscError::ReadError)?;
pad_cursor(cursor);
// convert to String and remove nul bytes
String::from_utf8(str_buf)
@ -119,7 +118,7 @@ fn read_osc_args(cursor: &mut io::Cursor<&[u8]>, raw_type_tags: String) -> Resul
let mut args: Vec<OscType> = Vec::with_capacity(type_tags.len());
for tag in type_tags {
let arg: OscType = try!(read_osc_arg(cursor, tag));
let arg: OscType = read_osc_arg(cursor, tag)?;
args.push(arg);
}
Ok(args)
@ -162,9 +161,9 @@ fn read_osc_arg(cursor: &mut io::Cursor<&[u8]>, tag: char) -> Result {
}
fn read_char(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let opt_char = try!(cursor.read_u32::<BigEndian>()
let opt_char = cursor.read_u32::<BigEndian>()
.map(char::from_u32)
.map_err(OscError::ReadError));
.map_err(OscError::ReadError)?;
match opt_char {
Some(c) => Ok(OscType::Char(c)),
None => Err(OscError::BadArg("Argument is not a char!".to_string())),
@ -172,13 +171,13 @@ fn read_char(cursor: &mut io::Cursor<&[u8]>) -> Result {
}
fn read_blob(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let size: usize = try!(cursor.read_u32::<BigEndian>()
.map_err(OscError::ReadError)) as usize;
let size: usize = cursor.read_u32::<BigEndian>()
.map_err(OscError::ReadError)? as usize;
let mut byte_buf: Vec<u8> = Vec::with_capacity(size);
try!(cursor.take(size as u64)
cursor.take(size as u64)
.read_to_end(&mut byte_buf)
.map_err(OscError::ReadError));
.map_err(OscError::ReadError)?;
pad_cursor(cursor);
@ -186,17 +185,17 @@ fn read_blob(cursor: &mut io::Cursor<&[u8]>) -> Result {
}
fn read_time_tag(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let date = try!(cursor.read_u32::<BigEndian>()
.map_err(OscError::ReadError));
let frac = try!(cursor.read_u32::<BigEndian>()
.map_err(OscError::ReadError));
let date = cursor.read_u32::<BigEndian>()
.map_err(OscError::ReadError)?;
let frac = cursor.read_u32::<BigEndian>()
.map_err(OscError::ReadError)?;
Ok(OscType::Time(date, frac))
}
fn read_midi_message(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let mut buf: Vec<u8> = Vec::with_capacity(4);
try!(cursor.take(4).read_to_end(&mut buf).map_err(OscError::ReadError));
cursor.take(4).read_to_end(&mut buf).map_err(OscError::ReadError)?;
Ok(OscType::Midi(OscMidiMessage {
port: buf[0],
@ -209,7 +208,7 @@ fn read_midi_message(cursor: &mut io::Cursor<&[u8]>) -> Result {
fn read_osc_color(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let mut buf: Vec<u8> = Vec::with_capacity(4);
try!(cursor.take(4).read_to_end(&mut buf).map_err(OscError::ReadError));
cursor.take(4).read_to_end(&mut buf).map_err(OscError::ReadError)?;
Ok(OscType::Color(OscColor {
red: buf[0],

+ 4
- 4
src/encoder.rs View File

@ -36,7 +36,7 @@ fn encode_message(msg: &OscMessage) -> Result> {
if let Some(ref args) = msg.args {
for arg in args {
let (bytes, tag): (Option<Vec<u8>>, char) = try!(encode_arg(arg));
let (bytes, tag): (Option<Vec<u8>>, char) = encode_arg(arg)?;
type_tags.push(tag);
if bytes.is_some() {
@ -57,7 +57,7 @@ fn encode_bundle(bundle: &OscBundle) -> Result> {
let mut bundle_bytes: Vec<u8> = Vec::new();
bundle_bytes.extend(encode_string("#bundle".to_string()).into_iter());
match try!(encode_arg(&bundle.timetag)) {
match encode_arg(&bundle.timetag)? {
(Some(x), _) => {
bundle_bytes.extend(x.into_iter());
}
@ -75,13 +75,13 @@ fn encode_bundle(bundle: &OscBundle) -> Result> {
for packet in &bundle.content {
match *packet {
OscPacket::Message(ref m) => {
let msg = try!(encode_message(m));
let msg = encode_message(m)?;
let mut msg_size = vec![0u8; 4];
BigEndian::write_u32(&mut msg_size, msg.len() as u32);
bundle_bytes.extend(msg_size.into_iter().chain(msg.into_iter()));
}
OscPacket::Bundle(ref b) => {
let bdl = try!(encode_bundle(b));
let bdl = encode_bundle(b)?;
let mut bdl_size = vec![0u8; 4];
BigEndian::write_u32(&mut bdl_size, bdl.len() as u32);
bundle_bytes.extend(bdl_size.into_iter().chain(bdl.into_iter()));

Loading…
Cancel
Save