use packet_parser::PacketFlow;
use pcap::Capture;
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pcap_file_path = Path::new(
        "/home/erdt-cyber/rust/icsmaster/pcap/opc/opc-ua-ap-method-wireshark-freeze.pcap",
    );
    println!("Reading pcap file: {}", pcap_file_path.display());
    let mut cap = Capture::from_file(pcap_file_path)?;
    let mut id = 0;
    loop {
        let packet = match cap.next_packet() {
            Ok(p) => p,
            Err(pcap::Error::NoMorePackets) => break,
            Err(e) => {
                eprintln!("pcap read error: {e}");
                continue;
            }
        };

        let parsed = PacketFlow::try_from(packet.data);

        match parsed {
            Ok(flow) => {
                id += 1;
                let owned = flow.to_owned();
                println!("Packet {id}: {owned}");
            }
            Err(e) => {
                id += 1;
                eprintln!("parse error: {e} for packet {id}");
            }
        }
    }

    Ok(())
}
