#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used))]
pub mod bulk;
pub mod data;
pub mod discovery;
pub mod error;
pub mod exec;
pub mod frame;
pub mod handshake;
pub mod identity;
pub mod introspection;
pub mod management;
pub(crate) mod open_tail;
pub mod room;
pub mod room_params;
pub mod secure_channel;
pub mod streaming;
pub mod strict;
pub mod threshold;
pub mod tokens;
pub mod transport;
pub mod value;
pub mod webrtc;
pub use error::{DecodeError, EncodeError};
pub use frame::{Frame, ValidationError};
pub use open_tail::Extra;
use minicbor::Decoder;
pub fn decode_frame(bytes: &[u8]) -> Result<Frame, DecodeError> {
Frame::decode_bytes(bytes)
}
pub fn encode_frame(frame: &Frame) -> Vec<u8> {
frame.encode_to_vec()
}
pub fn to_vec<T: minicbor::Encode<()>>(value: &T) -> Vec<u8> {
minicbor::to_vec(value).unwrap_or_else(|_| unreachable!("Vec<u8> writes are infallible"))
}
pub fn decode_exact<'b, T: minicbor::Decode<'b, ()>>(bytes: &'b [u8]) -> Result<T, DecodeError> {
let mut d = Decoder::new(bytes);
let value = T::decode(&mut d, &mut ()).map_err(DecodeError::from_minicbor)?;
if d.position() != bytes.len() {
return Err(DecodeError::TrailingBytes {
consumed: d.position(),
total: bytes.len(),
});
}
Ok(value)
}
pub use minicbor;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entry_points_round_trip() {
let frame = Frame::Ping(crate::transport::PingFrame);
let bytes = encode_frame(&frame);
assert_eq!(decode_frame(&bytes).expect("decode"), frame);
assert!(decode_frame(&{
let mut with_trailer = bytes.clone();
with_trailer.extend_from_slice(&[0x00]);
with_trailer
})
.is_err());
}
#[test]
fn decode_exact_rejects_trailing() {
let bytes = to_vec(&crate::identity::DeviceId([1; 32]));
let mut with_trailer = bytes.clone();
with_trailer.push(0x01);
assert!(decode_exact::<crate::identity::DeviceId>(&bytes).is_ok());
assert!(decode_exact::<crate::identity::DeviceId>(&with_trailer).is_err());
}
#[test]
fn indefinite_length_map_rejected() {
let bytes: Vec<u8> = [
0xbfu8, 0x64, b't', b'y', b'p', b'e', 0x64, b'p', b'i', b'n', b'g', 0xff,
]
.to_vec();
assert!(decode_frame(&bytes).is_err());
}
}