Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(wip) Iterator based parsing #142

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add stream parser with example
  • Loading branch information
trkohler committed Jan 4, 2025
commit 190d9983bb9a5bebbd3c459533cc4bbc7456d25d
33 changes: 33 additions & 0 deletions examples/parse_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::io::{Cursor, Read};
use nmea::stream::NmeaStreamParser;

fn main() {
// Example data source: a Cursor over a byte slice
let data = b"$GPGLL,4916.45,N,12311.12,W,225444,A,*1D\r\n$GPGLL,4916.45,N,12311.12,W,225444,A,*1D\r\n";
let mut reader = Cursor::new(data);

let mut parser = NmeaStreamParser::new();

let chunk_size = 10;
let mut buffer = vec![0; chunk_size];

// Read and process data in chunks
while let Ok(n) = reader.read(&mut buffer) {
if n == 0 {
break; // End of stream
}

// Process the chunk and get complete messages
let messages = parser.process_chunk(&buffer[..n]);

// Handle each complete message
for message in messages {
// Convert the message from Vec<u8> to a string for display
if let Ok(message_str) = String::from_utf8(message) {
println!("Parsed message: {}", message_str);
} else {
eprintln!("Failed to convert message to string");
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@
mod error;
pub(crate) mod parse;
mod parser;
pub mod stream;

pub mod sentences;

1 change: 1 addition & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -245,6 +245,7 @@ impl<'a> Nmea {
/// - and other
///
/// The type of sentence is returned if implemented and valid.
// MARK: parse which currently works based on strings
pub fn parse(&mut self, sentence: &'a str) -> Result<SentenceType, Error<'a>> {
match parse_str(sentence)? {
ParseResult::VTG(vtg) => {
6 changes: 3 additions & 3 deletions src/stream.rs
Original file line number Diff line number Diff line change
@@ -5,20 +5,20 @@
//
// we should 'chunkinate' the bytes based on the \r\n ending

struct NmeaStreamParser {
pub struct NmeaStreamParser {
buffer: Vec<u8>,
separator: Vec<u8>,
}

impl NmeaStreamParser {
fn new() -> Self {
pub fn new() -> Self {
NmeaStreamParser {
buffer: Vec::new(),
separator: b"\r\n".to_vec(),
}
}

fn process_chunk(&mut self, chunk: &[u8]) -> Vec<Vec<u8>> {
pub fn process_chunk(&mut self, chunk: &[u8]) -> Vec<Vec<u8>> {
self.buffer.extend_from_slice(chunk);
let mut messages = Vec::new();

@@ -59,7 +59,7 @@

fn next(&mut self) -> Option<Self::Item> {
let mut chunk = vec![0; self.chunk_size];
loop {

Check warning on line 62 in src/stream.rs

Codecov / codecov/patch

src/stream.rs#L62

Added line #L62 was not covered by tests
match self.reader.read(&mut chunk) {
Ok(0) => return None, // End of stream
Ok(n) => {
@@ -68,7 +68,7 @@
return Some(Ok(messages.remove(0)));
}
}
Err(e) => return Some(Err(e)),

Check warning on line 71 in src/stream.rs

Codecov / codecov/patch

src/stream.rs#L71

Added line #L71 was not covered by tests
}
}
}
Loading
Oops, something went wrong.