Browse Source

Remove Option from OscMessage arguments

Wrapping the arguments of an OscMessage in an Option was redundant
since an empty vector provides the same information.

This closes #7
add-osctime-alias-for-time-tags
Andreas Linz 1 year ago
parent
commit
9647a293e9
Signed by: alinz GPG Key ID: 9BF39809C9DA580C
7 changed files with 24 additions and 35 deletions
  1. +1
    -6
      examples/receiver.rs
  2. +3
    -3
      examples/sender.rs
  3. +2
    -6
      src/decoder.rs
  4. +6
    -8
      src/encoder.rs
  5. +3
    -3
      src/types.rs
  6. +2
    -2
      tests/decoder_test.rs
  7. +7
    -7
      tests/encoder_test.rs

+ 1
- 6
examples/receiver.rs View File

@ -40,12 +40,7 @@ fn handle_packet(packet: OscPacket) {
match packet {
OscPacket::Message(msg) => {
println!("OSC address: {}", msg.addr);
match msg.args {
Some(args) => {
println!("OSC arguments: {:?}", args);
}
None => println!("No arguments in message."),
}
println!("OSC arguments: {:?}", msg.args);
}
OscPacket::Bundle(bundle) => {
println!("OSC Bundle: {:?}", bundle);

+ 3
- 3
examples/sender.rs View File

@ -27,7 +27,7 @@ fn main() {
// switch view
let msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
addr: "/3".to_string(),
args: None,
args: vec![],
}))
.unwrap();
@ -41,14 +41,14 @@ fn main() {
let y = 0.5 + (step_size * (i % steps) as f32).cos() / 2.0;
let mut msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
addr: "/3/xy1".to_string(),
args: Some(vec![OscType::Float(x), OscType::Float(y)]),
args: vec![OscType::Float(x), OscType::Float(y)],
}))
.unwrap();
sock.send_to(&msg_buf, to_addr).unwrap();
msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
addr: "/3/xy2".to_string(),
args: Some(vec![OscType::Float(y), OscType::Float(x)]),
args: vec![OscType::Float(y), OscType::Float(x)],
}))
.unwrap();
sock.send_to(&msg_buf, to_addr).unwrap();

+ 2
- 6
src/decoder.rs View File

@ -35,13 +35,9 @@ fn decode_message(msg: &[u8]) -> Result {
if type_tags.len() > 1 {
let args: Vec<OscType> = read_osc_args(&mut cursor, type_tags)?;
Ok(OscPacket::Message(OscMessage {
addr,
args: Some(args),
}))
Ok(OscPacket::Message(OscMessage { addr, args }))
} else {
Ok(OscPacket::Message(OscMessage { addr, args: None }))
Ok(OscPacket::Message(OscMessage { addr, args: vec![] }))
}
}

+ 6
- 8
src/encoder.rs View File

@ -15,7 +15,7 @@ use byteorder::{BigEndian, ByteOrder};
///
/// let packet = OscPacket::Message(OscMessage{
/// addr: "/greet/me".to_string(),
/// args: Some(vec![OscType::String("hi!".to_string())])
/// args: vec![OscType::String("hi!".to_string())]
/// }
/// );
/// assert!(encoder::encode(&packet).is_ok())
@ -34,14 +34,12 @@ fn encode_message(msg: &OscMessage) -> Result> {
let mut type_tags: Vec<char> = vec![','];
let mut arg_bytes: Vec<u8> = Vec::new();
if let Some(ref args) = msg.args {
for arg in args {
let (bytes, tags): (Option<Vec<u8>>, String) = encode_arg(arg)?;
for arg in &msg.args {
let (bytes, tags): (Option<Vec<u8>>, String) = encode_arg(arg)?;
type_tags.extend(tags.chars());
if let Some(data) = bytes {
arg_bytes.extend(data);
}
type_tags.extend(tags.chars());
if let Some(data) = bytes {
arg_bytes.extend(data);
}
}

+ 3
- 3
src/types.rs View File

@ -100,7 +100,7 @@ pub enum OscPacket {
#[derive(Clone, Debug, PartialEq)]
pub struct OscMessage {
pub addr: String,
pub args: Option<Vec<OscType>>,
pub args: Vec<OscType>,
}
/// An OSC bundle contains zero or more OSC packets
@ -133,7 +133,7 @@ impl From for OscMessage {
fn from(s: String) -> OscMessage {
OscMessage {
addr: s,
args: None,
args: vec![],
}
}
}
@ -141,7 +141,7 @@ impl<'a> From<&'a str> for OscMessage {
fn from(s: &str) -> OscMessage {
OscMessage {
addr: s.to_string(),
args: None,
args: vec![],
}
}
}

+ 2
- 2
tests/decoder_test.rs View File

@ -18,7 +18,7 @@ fn test_decode_no_args() {
match osc_packet {
Ok(rosc::OscPacket::Message(msg)) => {
assert_eq!(raw_addr, msg.addr);
assert!(msg.args.is_none());
assert!(msg.args.is_empty());
}
Ok(_) => panic!("Expected an OscMessage!"),
Err(e) => panic!(e),
@ -87,7 +87,7 @@ fn test_decode_args() {
match decoder::decode(&merged).unwrap() {
rosc::OscPacket::Message(msg) => {
for arg in msg.args.unwrap() {
for arg in msg.args {
match arg {
rosc::OscType::Int(x) => assert_eq!(i, x),
rosc::OscType::Long(x) => assert_eq!(l, x),

+ 7
- 7
tests/encoder_test.rs View File

@ -7,7 +7,7 @@ use rosc::{OscArray, OscBundle, OscColor, OscMessage, OscMidiMessage, OscPacket,
fn test_encode_message_wo_args() {
let msg_packet = OscPacket::Message(OscMessage {
addr: "/some/addr".to_string(),
args: None,
args: vec![],
});
let enc_msg = encoder::encode(&msg_packet).unwrap();
@ -30,7 +30,7 @@ fn test_encode_message_wo_args() {
fn test_encode_message_with_args() {
let msg_packet = OscPacket::Message(OscMessage {
addr: "/another/address/1".to_string(),
args: Some(vec![
args: vec![
4i32.into(),
42i64.into(),
3.1415926f32.into(),
@ -69,7 +69,7 @@ fn test_encode_message_with_args() {
],
}
.into(),
]),
],
});
let enc_msg = encoder::encode(&msg_packet).unwrap();
@ -92,22 +92,22 @@ fn test_encode_message_with_args() {
fn test_encode_bundle() {
let msg0 = OscMessage {
addr: "/view/1".to_string(),
args: None,
args: vec![],
};
let msg1 = OscMessage {
addr: "/mixer/channel/1/amp".to_string(),
args: Some(vec![0.9f32.into()]),
args: vec![0.9f32.into()],
};
let msg2 = OscMessage {
addr: "/osc/1/freq".to_string(),
args: Some(vec![440i32.into()]),
args: vec![440i32.into()],
};
let msg3 = OscMessage {
addr: "/osc/1/phase".to_string(),
args: Some(vec![(-0.4f32).into()]),
args: vec![(-0.4f32).into()],
};
let bundle1 = OscBundle {

Loading…
Cancel
Save