Created
June 30, 2025 11:17
-
-
Save M0r13n/96ad65fc1615376ac26a3c52f1c8f1fe to your computer and use it in GitHub Desktop.
Reading AIS messages from a file in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use ais::lib; | |
use ais::sentence::{AisFragments, AisParser}; | |
use lib::std::io::BufRead; | |
use std::fs::File; | |
use std::io::BufReader; | |
use std::time::Instant; | |
fn parse_nmea_line(parser: &mut AisParser, line: &[u8]) -> Result<(), ais::errors::Error> { | |
let sentence = parser.parse(line, true)?; | |
if let AisFragments::Complete(sentence) = sentence { | |
// println!( | |
// "{:?}\t{:?}", | |
// lib::std::str::from_utf8(line).unwrap(), | |
// sentence.message | |
// ); | |
} | |
Ok(()) | |
} | |
fn main() { | |
let program_start = Instant::now(); | |
let mut parser = AisParser::new(); | |
let mut total_sentences = 0; | |
let mut parsed_sentences = 0; | |
let mut complete_sentences = 0; | |
let mut fragment_sentences = 0; | |
let mut error_count = 0; | |
println!("Starting AIS parser..."); | |
// Measure file operations | |
let file_start = Instant::now(); | |
let file = match File::open("../aisreader/messages.txt") { | |
Ok(f) => f, | |
Err(e) => { | |
eprintln!("Could not open file: {}", e); | |
return; | |
} | |
}; | |
let reader = BufReader::new(file); | |
let file_duration = file_start.elapsed(); | |
println!("File opened in {:.2?}", file_duration); | |
// Measure parsing | |
println!("Parsing sentences..."); | |
let parse_start = Instant::now(); | |
for line_result in reader.split(b'\n') { | |
let line = match line_result { | |
Ok(l) => l, | |
Err(_) => { | |
error_count += 1; | |
continue; | |
} | |
}; | |
if line.is_empty() { | |
continue; | |
} | |
total_sentences += 1; | |
parse_nmea_line(&mut parser, &line); | |
} | |
let parse_duration = parse_start.elapsed(); | |
let total_duration = program_start.elapsed(); | |
// Print detailed summary | |
println!("\n=== SUMMARY ==="); | |
println!("Decoded {} NMEA AIS messages in {:.2?}", total_sentences, parse_duration); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment