Browse Source

Support empty bundles

tags/v0.4.1
Gustaf Lindstedt 1 month ago
parent
commit
951f53ad1a
4 changed files with 49 additions and 3 deletions
  1. +8
    -0
      src/decoder.rs
  2. +0
    -2
      src/encoder.rs
  3. +17
    -1
      tests/decoder_test.rs
  4. +24
    -0
      tests/encoder_test.rs

+ 8
- 0
src/decoder.rs View File

@@ -56,6 +56,14 @@ fn decode_bundle(msg: &[u8]) -> Result<OscPacket> {

let mut bundle: Vec<OscPacket> = Vec::new();

// Empty bundle
if msg.len() == cursor.position() as usize {
return Ok(OscPacket::Bundle(OscBundle {
timetag: time_tag,
content: bundle,
}))
}

let mut elem_size = read_bundle_element_size(&mut cursor)?;

while msg.len() >= (cursor.position() as usize) + elem_size {


+ 0
- 2
src/encoder.rs View File

@@ -64,8 +64,6 @@ fn encode_bundle(bundle: &OscBundle) -> Result<Vec<u8>> {
}

if bundle.content.is_empty() {
// TODO: A bundle of length zero, should this really be supported?
bundle_bytes.extend([0u8; 4].iter());
return Ok(bundle_bytes);
}



+ 17
- 1
tests/decoder_test.rs View File

@@ -4,7 +4,7 @@ extern crate rosc;
use byteorder::{BigEndian, ByteOrder};
use std::mem;

use rosc::{decoder, encoder, OscType};
use rosc::{decoder, encoder, OscBundle, OscPacket, OscType};

#[test]
fn test_decode_no_args() {
@@ -25,6 +25,22 @@ fn test_decode_no_args() {
}
}

#[test]
fn test_decode_empty_bundle() {
let timetag = (4, 2);
let content = vec![];
let packet = encoder::encode(&OscPacket::Bundle(OscBundle { timetag, content })).unwrap();
let osc_packet: Result<rosc::OscPacket, rosc::OscError> = decoder::decode(&packet);
match osc_packet {
Ok(rosc::OscPacket::Bundle(bundle)) => {
assert_eq!(timetag, bundle.timetag);
assert!(bundle.content.is_empty());
}
Ok(_) => panic!("Expected an OscBundle!"),
Err(e) => panic!(e),
}
}

#[test]
fn test_decode_args() {
// /another/valid/address/123 ,fdih 3.1415 3.14159265359 12345678i32


+ 24
- 0
tests/encoder_test.rs View File

@@ -26,6 +26,30 @@ fn test_encode_message_wo_args() {
assert_eq!(*msg, dec_msg)
}

#[test]
fn test_encode_empty_bundle() {
let bundle_packet = OscPacket::Bundle(OscBundle {
timetag: (4, 2),
content: vec![],
});

let enc_bundle = encoder::encode(&bundle_packet).unwrap();
assert_eq!(enc_bundle.len() % 4, 0);
assert_eq!(enc_bundle.len(), 16);

let dec_bundle = match decoder::decode(&enc_bundle).unwrap() {
OscPacket::Bundle(m) => m,
_ => panic!("Expected OscBundle!"),
};

let bundle = match bundle_packet {
OscPacket::Bundle(ref bundle) => bundle,
_ => panic!(),
};

assert_eq!(*bundle, dec_bundle)
}

#[test]
fn test_encode_message_with_args() {
let msg_packet = OscPacket::Message(OscMessage {


Loading…
Cancel
Save