use anyhow::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestFrame {
    pub header: [u8; 2],
    pub cmd: u8,
    pub length: usize,
    pub payload: Vec<u8>,
    pub checksum: u8,
    pub dts_ms: u64
}



#[async_trait]
pub trait FrameHandler<F, T>: Send {
    async fn on_frame(&mut self, frame: &F) -> Result<Option<Vec<T>>>;
}

pub fn usize_to_u16_be_bytes(n: usize) -> [u8; 2] {
    (n as u16).to_be_bytes()
}

pub fn usize_to_u16_le_bytes(n: usize) -> [u8; 2] {
    (n as u16).to_be_bytes()
}

pub fn crc8(data: &[u8]) -> u8 {
    let mut crc: u8 = 0x00;

    for &byte in data {
        crc ^= byte;
        for _ in 0..8 {
            if (crc & 0x80) != 0 {
                crc = (crc << 1) ^ 0x07;
            } else {
                crc <<= 1;
            }
        }
    }

    crc
}
