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
binrw::Error::AssertFailwhen a certain field’s value is outside the expected range. This generally applies to_indexfields, percentage values, and fields that have the aforementioned range specified in their documentationbinrw::Error::BadMagicwhenF1PacketEventhas a code that doesn’t match any known event typebinrw::Error::Customwhen the parser encounters an invalid bool value in a read byte (i.e. neither 0, nor 1)binrw::Error::EnumErrorswhen there’s no matching value for an enum field
§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"
);