use std::convert::TryFrom;
use std::sync::mpsc::{Receiver, Sender};
use crate::{Bytes, Event};
pub mod am;
pub mod raw;
pub struct DispatcherHandle {
pub tx: Sender<Event<Bytes>>,
pub rx: Receiver<Event<Bytes>>,
pub stopper: Box<dyn FnOnce() -> Result<(), &'static str>>,
}
#[derive(Debug)]
pub struct DispatchPacket {
pub dispatch: u8,
pub payload: Vec<u8>,
}
pub trait Dispatcher {
fn dispatch_byte(&self) -> u8;
fn get_handle(&mut self) -> DispatcherHandle;
}
impl DispatcherHandle {
pub fn new(tx: Sender<Event<Bytes>>, rx: Receiver<Event<Bytes>>) -> DispatcherHandle {
DispatcherHandle::with_stopper(tx, rx, Box::new(|| Ok(())))
}
pub fn with_stopper(
tx: Sender<Event<Bytes>>,
rx: Receiver<Event<Bytes>>,
stopper: Box<dyn FnOnce() -> Result<(), &'static str>>,
) -> DispatcherHandle {
DispatcherHandle { tx, rx, stopper }
}
}
impl TryFrom<Vec<u8>> for DispatchPacket {
type Error = &'static str;
fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
match data.len() {
0 => Err("Data length is 0!"),
_ => Ok(DispatchPacket {
dispatch: data[0],
payload: data[1..].to_vec(),
}),
}
}
}
impl Into<Vec<u8>> for DispatchPacket {
fn into(self) -> Vec<u8> {
let mut result = Vec::with_capacity(1 + self.payload.len());
result.extend([self.dispatch].iter().chain(self.payload.iter()));
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bytes_to_dispatchpacket_normal() {
let data = vec![1, 2, 3, 4];
let packet = DispatchPacket::try_from(data).unwrap();
assert_eq!(packet.dispatch, 1);
assert_eq!(packet.payload, vec![2, 3, 4]);
}
#[test]
fn test_bytes_to_dispatchpacket_no_data() {
let data = vec![1];
let packet = DispatchPacket::try_from(data).unwrap();
assert_eq!(packet.dispatch, 1);
assert_eq!(packet.payload, vec![]);
}
#[test]
fn test_bytes_to_dispatchpacket_zero_length() {
let data = vec![];
let error = DispatchPacket::try_from(data).unwrap_err();
assert_eq!(error, "Data length is 0!");
}
#[test]
fn test_dispatchpacket_into_bytes() {
let packet = DispatchPacket {
dispatch: 5,
payload: vec![1, 2, 3],
};
let bytes: Vec<u8> = packet.into();
assert_eq!(bytes, vec![5, 1, 2, 3]);
}
}