Skip to main content

process_file/
process_file.rs

1use std::fs::File;
2
3use libaprs_engine::{
4    read_all_with_limit, Engine, EngineResult, LineTransport, Policy, DEFAULT_TRANSPORT_READ_LIMIT,
5};
6
7fn main() -> std::io::Result<()> {
8    let Some(path) = std::env::args().nth(1) else {
9        eprintln!("usage: process_file <packets.aprs>");
10        std::process::exit(2);
11    };
12
13    let input = read_all_with_limit(File::open(path)?, DEFAULT_TRANSPORT_READ_LIMIT)?;
14    let mut engine = Engine::new(Policy::strict());
15
16    for packet_bytes in LineTransport::new(&input).packets() {
17        match engine.process(packet_bytes) {
18            EngineResult::Accepted { packet } => {
19                let summary = packet.summary();
20                println!(
21                    "accepted source={} destination={} semantic={}",
22                    String::from_utf8_lossy(summary.source),
23                    String::from_utf8_lossy(summary.destination),
24                    summary.semantic
25                );
26            }
27            EngineResult::Rejected { reason, .. } => eprintln!("rejected: {}", reason.code()),
28            EngineResult::ParseError(error) => eprintln!("malformed: {}", error.code()),
29        }
30    }
31
32    Ok(())
33}