dis-rs 0.13.0

An implementation of the Distributed Interactive Simulation protocol (IEEE-1278.1) in Rust. This main crate contains PDU implementations and facilities to read/write PDUs from Rust data structures to the wire format and vice versa. It supports versions 6 and 7 of the protocol.
Documentation
use dis_rs::model::Pdu;

const FILE_EXT_PCAP: &str = "pcap";
const FILE_EXT_XMSN: &str = "xmsn";

fn main() {
    #[cfg(feature = "hotpath")]
    let guard = hotpath::FunctionsGuardBuilder::new("pdu_parser_allocations")
        .percentiles(&[50, 95, 99])
        .format(hotpath::Format::Table)
        .build();

    let file_name = std::env::args()
        .nth(1)
        .expect("Filename as first argument expected.");

    let bytes = match file_name.to_lowercase().split('.').next_back() {
        Some(FILE_EXT_PCAP) => {
            println!("Opening .{FILE_EXT_PCAP} file: {file_name}");
            read_pcap_file(&file_name)
        }
        Some(FILE_EXT_XMSN) => {
            println!("Opening .{FILE_EXT_XMSN} file: {file_name}");
            read_xmsn_file(&file_name)
        }
        Some(_) | None => {
            eprintln!("Unknown file format or no file specified: {file_name}");
            eprintln!("Expecting '.{FILE_EXT_PCAP}' or '.{FILE_EXT_XMSN}' files.");
            return;
        }
    };
    println!("bytes len: {}", bytes.len());
    let pdus = parse_dis(&bytes);

    println!("Parsed {} PDUs.", pdus.len());

    #[cfg(feature = "hotpath")]
    drop(guard);
}

#[cfg(feature = "pcap-file")]
fn read_pcap_file(file_name: &str) -> Vec<u8> {
    const NETWORK_STACK_HEADERS_LENGTH: usize = 42; // Ethernet/IP/UDP headers
    let file_in = std::fs::File::open(file_name).expect("Error opening .{FILE_EXT_PCAP} file");
    let mut pcap_reader = pcap_file::pcap::PcapReader::new(file_in).unwrap();

    let mut bytes = Vec::new();
    while let Some(pkt) = pcap_reader.next_packet() {
        let pkt = pkt.expect("Error opening packet");
        let data = &pkt.data[NETWORK_STACK_HEADERS_LENGTH..];
        // let mut parsed = dis_rs::parse(data).expect("Expected a well-formed PDU");
        bytes.append(&mut data.to_vec());
    }
    bytes
}

#[cfg(not(feature = "pcap-file"))]
fn read_pcap_file(_file_name: &str) -> Vec<u8> {
    vec![]
}

fn read_xmsn_file(file_name: &str) -> Vec<u8> {
    std::fs::read(file_name).expect("Error opening and reading .{FILE_EXT_XMSN} file")
}

fn parse_dis(bytes: &Vec<u8>) -> Vec<Pdu> {
    dis_rs::parse(bytes.as_slice()).expect("Expected well formed PDUs.")
}