Browse Source

Apply cargo fmt

tags/0.2.0
Andreas Linz 1 year ago
parent
commit
dd53f62be1
Signed by: alinz <klingt.net@gmail.com> GPG Key ID: 9BF39809C9DA580C
7 changed files with 86 additions and 71 deletions
  1. +6
    -5
      benches/decoder_bench.rs
  2. +2
    -2
      examples/receiver.rs
  3. +10
    -7
      examples/sender.rs
  4. +53
    -46
      src/decoder.rs
  5. +3
    -3
      src/lib.rs
  6. +2
    -1
      tests/decoder_test.rs
  7. +10
    -7
      tests/encoder_test.rs

+ 6
- 5
benches/decoder_bench.rs View File

@@ -1,6 +1,6 @@
#![feature(test)]
extern crate test;
extern crate rosc;
extern crate test;

use self::test::Bencher;

@@ -8,9 +8,10 @@ use self::test::Bencher;
fn bench_decode(b: &mut Bencher) {
// The message was captured from the `ytterbium` lemur patch looks like this:
// OSC Bundle: OscBundle { timetag: Time(0, 1), content: [Message(OscMessage { addr: "/OSCILLATORS/OSC2/ADSR/x", args: Some([Float(0.1234567), Float(0.1234567), Float(0.1234567), Float(0.1234567)]) })] }
let raw_msg: [u8; 72] = [35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
52, 47, 79, 83, 67, 73, 76, 76, 65, 84, 79, 82, 83, 47, 79, 83, 67,
50, 47, 65, 68, 83, 82, 47, 122, 0, 0, 0, 0, 44, 102, 102, 102, 102,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let raw_msg: [u8; 72] = [
35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 52, 47, 79, 83, 67,
73, 76, 76, 65, 84, 79, 82, 83, 47, 79, 83, 67, 50, 47, 65, 68, 83, 82, 47, 122, 0, 0, 0,
0, 44, 102, 102, 102, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
b.iter(|| rosc::decoder::decode(&raw_msg).unwrap());
}

+ 2
- 2
examples/receiver.rs View File

@@ -1,9 +1,9 @@
extern crate rosc;

use rosc::OscPacket;
use std::env;
use std::net::{UdpSocket, SocketAddrV4};
use std::net::{SocketAddrV4, UdpSocket};
use std::str::FromStr;
use rosc::OscPacket;

fn main() {
let args: Vec<String> = env::args().collect();


+ 10
- 7
examples/sender.rs View File

@@ -1,11 +1,11 @@
extern crate rosc;

use std::{env, thread, f32};
use std::net::{UdpSocket, SocketAddrV4};
use rosc::encoder;
use rosc::{OscMessage, OscPacket, OscType};
use std::net::{SocketAddrV4, UdpSocket};
use std::str::FromStr;
use std::time::Duration;
use rosc::{OscPacket, OscMessage, OscType};
use rosc::encoder;
use std::{env, f32, thread};

fn get_addr_from_arg(arg: &str) -> SocketAddrV4 {
SocketAddrV4::from_str(arg).unwrap()
@@ -28,7 +28,8 @@ fn main() {
let msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
addr: "/3".to_string(),
args: None,
})).unwrap();
}))
.unwrap();

sock.send_to(&msg_buf, to_addr).unwrap();

@@ -41,13 +42,15 @@ fn main() {
let mut msg_buf = encoder::encode(&OscPacket::Message(OscMessage {
addr: "/3/xy1".to_string(),
args: Some(vec![OscType::Float(x), OscType::Float(y)]),
})).unwrap();
}))
.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)]),
})).unwrap();
}))
.unwrap();
sock.send_to(&msg_buf, to_addr).unwrap();
thread::sleep(Duration::from_millis(20));
}


+ 53
- 46
src/decoder.rs View File

@@ -1,9 +1,11 @@
use encoder;
use errors::OscError;
use types::{OscArray, OscBundle, OscColor, OscMessage, OscMidiMessage, OscPacket, OscType, Result};
use types::{
OscArray, OscBundle, OscColor, OscMessage, OscMidiMessage, OscPacket, OscType, Result,
};

use std::{io, char};
use std::io::{Read, BufRead};
use std::io::{BufRead, Read};
use std::{char, io};

use byteorder::{BigEndian, ReadBytesExt};

@@ -51,9 +53,10 @@ fn decode_bundle(msg: &[u8]) -> Result<OscPacket> {

let bundle_tag = read_osc_string(&mut cursor)?;
if bundle_tag != "#bundle" {
return Err(OscError::BadBundle(
format!("Wrong bundle specifier: {}", bundle_tag),
));
return Err(OscError::BadBundle(format!(
"Wrong bundle specifier: {}",
bundle_tag
)));
}

let time_tag = read_time_tag(&mut cursor)?;
@@ -105,9 +108,9 @@ 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();
// ignore returned byte count
cursor.read_until(0, &mut str_buf).map_err(
OscError::ReadError,
)?;
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)
@@ -145,27 +148,22 @@ fn read_osc_args(cursor: &mut io::Cursor<&[u8]>, raw_type_tags: String) -> Resul

fn read_osc_arg(cursor: &mut io::Cursor<&[u8]>, tag: char) -> Result<OscType> {
match tag {
'f' => {
cursor.read_f32::<BigEndian>().map(OscType::Float).map_err(
OscError::ReadError,
)
}
'd' => {
cursor
.read_f64::<BigEndian>()
.map(OscType::Double)
.map_err(OscError::ReadError)
}
'i' => {
cursor.read_i32::<BigEndian>().map(OscType::Int).map_err(
OscError::ReadError,
)
}
'h' => {
cursor.read_i64::<BigEndian>().map(OscType::Long).map_err(
OscError::ReadError,
)
}
'f' => cursor
.read_f32::<BigEndian>()
.map(OscType::Float)
.map_err(OscError::ReadError),
'd' => cursor
.read_f64::<BigEndian>()
.map(OscType::Double)
.map_err(OscError::ReadError),
'i' => cursor
.read_i32::<BigEndian>()
.map(OscType::Int)
.map_err(OscError::ReadError),
'h' => cursor
.read_i64::<BigEndian>()
.map(OscType::Long)
.map_err(OscError::ReadError),
's' => read_osc_string(cursor).map(OscType::String),
't' => read_time_tag(cursor),
'b' => read_blob(cursor),
@@ -176,16 +174,18 @@ fn read_osc_arg(cursor: &mut io::Cursor<&[u8]>, tag: char) -> Result<OscType> {
'I' => Ok(OscType::Inf),
'c' => read_char(cursor),
'm' => read_midi_message(cursor),
_ => Err(OscError::BadArg(
format!("Type tag \"{}\" is not implemented!", tag),
)),
_ => Err(OscError::BadArg(format!(
"Type tag \"{}\" is not implemented!",
tag
))),
}
}

fn read_char(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let opt_char = cursor.read_u32::<BigEndian>().map(char::from_u32).map_err(
OscError::ReadError,
)?;
let opt_char = cursor
.read_u32::<BigEndian>()
.map(char::from_u32)
.map_err(OscError::ReadError)?;
match opt_char {
Some(c) => Ok(OscType::Char(c)),
None => Err(OscError::BadArg("Argument is not a char!".to_string())),
@@ -193,7 +193,9 @@ fn read_char(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
}

fn read_blob(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let size: usize = 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);

cursor
@@ -207,17 +209,22 @@ fn read_blob(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
}

fn read_time_tag(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let date = cursor.read_u32::<BigEndian>().map_err(OscError::ReadError)?;
let frac = 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);
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],
@@ -225,14 +232,14 @@ fn read_midi_message(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
data1: buf[2],
data2: buf[3],
}))

}

fn read_osc_color(cursor: &mut io::Cursor<&[u8]>) -> Result<OscType> {
let mut buf: Vec<u8> = Vec::with_capacity(4);
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],


+ 3
- 3
src/lib.rs View File

@@ -3,13 +3,13 @@

extern crate byteorder;

/// OSC data types, see [OSC 1.0 specification](http://opensoundcontrol.org/spec-1_0) for details.
mod types;
/// Crate specific error types.
mod errors;
/// OSC data types, see [OSC 1.0 specification](http://opensoundcontrol.org/spec-1_0) for details.
mod types;

pub use types::*;
pub use errors::*;
pub use types::*;

/// Provides a decoding method for OSC packets.
pub mod decoder;


+ 2
- 1
tests/decoder_test.rs View File

@@ -79,7 +79,8 @@ fn test_decode_args() {
.map(|x| *x)
.collect::<Vec<u8>>();

let merged: Vec<u8> = addr.into_iter()
let merged: Vec<u8> = addr
.into_iter()
.chain(type_tags.into_iter())
.chain(args)
.collect::<Vec<u8>>();


+ 10
- 7
tests/encoder_test.rs View File

@@ -49,7 +49,8 @@ fn test_encode_message_with_args() {
status: 41,
data1: 42,
data2: 129,
}.into(),
}
.into(),
OscColor {
red: 255,
green: 192,
@@ -60,12 +61,14 @@ fn test_encode_message_with_args() {
OscArray {
content: vec![
42i32.into(),
OscArray{
content: vec![1.23.into(), 3.21.into()]
}.into(),
"Yay".into()
]
}.into(),
OscArray {
content: vec![1.23.into(), 3.21.into()],
}
.into(),
"Yay".into(),
],
}
.into(),
]),
});



Loading…
Cancel
Save