/// Opcode definitions for tcptape format
pub const NOOP: u8 = 0;
pub const CLIENT_TO_SERVER: u8 = 1;
pub const SERVER_TO_CLIENT: u8 = 2;
pub const DELAY: u8 = 3;
pub const CLIENT_TO_SERVER_EOF: u8 = 4;
pub const SERVER_TO_CLIENT_EOF: u8 = 5;

/// Write a 4-byte header: opcode (1 byte) + length (3 bytes in big-endian)
/// Panics if length doesn't fit in 3 bytes (max 0xFFFFFF = 16777215)
pub fn write_header(opcode: u8, len: usize) -> [u8; 4] {
    let len_u32 = u32::try_from(len).expect("length too large for u32");
    assert!(len_u32 <= 0xFFFFFF, "length {} does not fit in 3 bytes", len);
    [
        opcode,
        ((len_u32 >> 16) & 0xFF) as u8,
        ((len_u32 >> 8) & 0xFF) as u8,
        (len_u32 & 0xFF) as u8,
    ]
}
