serde_osc/
lib.rs

1//! # OSC protocol
2//!
3//! Open Sound Control (OSC) is an open, transport-independent, message-based
4//! protocol developed for communication among computers, sound synthesizers,
5//! and other multimedia devices.
6//!
7//! It mixes human-readable API endpoints (termed "addresses") with binary-encoded
8//! arguments and headers. An example OSC message is
9//!
10//! ```norun
11//! b"\0\0\0\x24/audio/play\0,ifb\0\0\0\0\0\0\0\x01x71\x44\x68\x00\0\0\0\x04\xDE\xAD\xBE\xEF"
12//! ```
13//!
14//! In this example, the first 4 bytes signify the length of the message;
15//! the null-terminated string "/audio/play" signifies the endpoint (which
16//! component the message is intended for);
17//! the ",ifb" string indicates that there are 3 arguments: one `i32`, one `f32`
18//! and one binary "blob" (`u8` array). The rest of the message is the payload,
19//! i.e., the values corresponding to each of these arguments.
20//! The full specification can be found at [http://opensoundcontrol.org/spec-1_0].
21//! Serde_osc implements version 1.0 of the spec (i.e. 'i', 'f', 's' and 'b'
22//! argument types).
23//!
24//! # Serde_osc usage
25//!
26//! Generic encoding of OSC packets is intended to be done via [`serde_osc::to_write`]
27//! with decoding done by [`serde_osc::from_read`]. These work with any data
28//! sink/source that implements `std::io::Write` or `std::io::Read`, respectively.
29//!
30//! Convenience functions are also provided for some common formats; see
31//! [`serde_osc::to_vec`] and [`serde_osc::from_vec`].
32//!
33//! [`serde_osc::to_write`]: ser/fn.to_write.html
34//! [`serde_osc::from_read`]: de/fn.from_read.html
35//! [`serde_osc::to_vec`]: ser/fn.to_vec.html
36//! [`serde_osc::from_vec`]: de/fn.from_vec.html
37//! [http://opensoundcontrol.org/spec-1_0]: http://opensoundcontrol.org/spec-1_0
38//!
39//! # Examples
40//!
41//! The following example serializes a struct into a `Vec<u8>`, formatted as an
42//! OSC packet, and then deserializes the OSC packet back into the struct.
43//!
44//! ```
45//! #[macro_use]
46//! extern crate serde_derive;
47//! extern crate serde;
48//! extern crate serde_bytes;
49//! extern crate serde_osc;
50//!
51//! use serde_bytes::ByteBuf;
52//! use serde_osc::{de, ser};
53//!
54//! /// Struct we'll serialize.
55//! /// This represents a single OSC message with three arguments:
56//! ///   one of type 'i', 'f' and 'b', encoded in the order they appear in the struct.
57//! #[derive(Debug, Deserialize, Serialize)]
58//! struct Message {
59//!     address: String,
60//!     // ByteBuf is the object we use for OSC "blobs".
61//!     // It's a thin wrapper over Vec<u8> provided by Serde that allows
62//!     // for more computationally-efficient serialization/deserialization.
63//!     args: (i32, f32, ByteBuf),
64//! }
65//!
66//! fn main() {
67//!     let message = Message {
68//!         address: "/audio/play".to_owned(),
69//!         args: (
70//!             1,
71//!             44100.0f32,
72//!             ByteBuf::from(vec![0xde, 0xad, 0xbe, 0xef]),
73//!         )
74//!     };
75//!     println!("Serializing {:?}", message);
76//!
77//!     // Serialize the message to an OSC packet stored in a Vec<u8>
78//!     let as_vec = ser::to_vec(&message).unwrap();
79//!     println!("Serialied to: {:?}", as_vec);
80//!
81//!     // Deserialize an OSC packet contained in a Vec<u8> into the Message struct
82//!     let received: Message = de::from_slice(&as_vec).unwrap();
83//!     println!("Received: {:?}", received);
84//! }
85//! ```
86
87
88#![feature(try_from)]
89
90extern crate byteorder;
91#[macro_use]
92extern crate serde;
93
94/// Errors returned upon serialization/deserialization failure.
95pub mod error;
96/// OSC packet deserialization framework.
97pub mod de;
98/// OSC packet serialization framework.
99pub mod ser;
100
101pub use de::{from_read, from_slice};
102pub use ser::{to_write, to_vec};