Function parse

Source
pub fn parse<T: AsRef<[u8]>>(data: T) -> BinResult<F1Packet>
Expand description

Attempts to extract F1 game packet data from a byte buffer (such as a Vec<u8>, [u8; N], or &[u8]).

§Errors

§Examples

§Basic UDP client

use f1_game_packet_parser::parse;
use std::error::Error;
use std::net::UdpSocket;

fn main() -> Result<(), Box<dyn Error>> {
    // This IP and port should be set in the game's options by default.
    let socket = UdpSocket::bind("127.0.0.1:20777")?;
    let mut buf = [0u8; 1464];

    loop {
        // Receive raw packet data from the game.
        // The buf array should be large enough for all types of packets.
        let (amt, _) = socket.recv_from(&mut buf)?;

        // Convert received bytes to an F1Packet struct and print it.
        let packet = parse(&buf[..amt])?;
        println!("{:#?}", packet);
    }
}

§Invalid/unsupported packet format

let invalid_format = 2137u16.to_le_bytes();
let parse_result = f1_game_packet_parser::parse(invalid_format);

assert!(parse_result.is_err());
assert_eq!(
    parse_result.unwrap_err().root_cause().to_string(),
    "Invalid or unsupported packet format: 2137 at 0x0"
);