Skip to content

Commit 2dd2687

Browse files
authored
Add ChunkReader for convenience (#44)
1 parent 3cec113 commit 2dd2687

9 files changed

Lines changed: 404 additions & 234 deletions

β€Žavr_demo/examples/test_streamparser.rsβ€Ž

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use avr_demo as _;
66
use avr_demo::stack_measurement::*;
7-
use picojson::{self, Event, ParseError, PullParser, Reader, StreamParser};
7+
use picojson::{self, ChunkReader, Event, ParseError, PullParser, StreamParser};
88

99
#[allow(unused_imports)]
1010
use picojson::ArrayBitStack;
@@ -71,37 +71,6 @@ struct Doc<'b> {
7171
status: &'b str,
7272
}
7373

74-
// Simple Reader implementation for testing that wraps a slice
75-
struct SliceReader<'a> {
76-
data: &'a [u8],
77-
position: usize,
78-
}
79-
80-
impl<'a> SliceReader<'a> {
81-
fn new(data: &'a [u8]) -> Self {
82-
Self { data, position: 0 }
83-
}
84-
}
85-
86-
impl<'a> Reader for SliceReader<'a> {
87-
type Error = ();
88-
89-
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
90-
let remaining = self.data.len().saturating_sub(self.position);
91-
if remaining == 0 {
92-
return Ok(0);
93-
}
94-
95-
let to_copy = remaining.min(buf.len());
96-
if let Ok(copied) = copy_subslice(self.data, self.position, buf, 0, to_copy) {
97-
self.position = self.position.wrapping_add(copied);
98-
Ok(copied)
99-
} else {
100-
Err(())
101-
}
102-
}
103-
}
104-
10574
#[derive(Debug, Clone, Copy, PartialEq)]
10675
enum KeyContext {
10776
None,
@@ -117,7 +86,7 @@ fn parse_json<'b>(json_data: &[u8], scratch: &'b mut [u8]) -> Result<Doc<'b>, Pa
11786

11887
// Create a streaming buffer for StreamParser (balanced size for testing)
11988
let mut stream_buffer = [0u8; 12];
120-
let reader = SliceReader::new(json_data);
89+
let reader = ChunkReader::full_slice(json_data);
12190
let mut parser = StreamParser::<_, PicoConfig>::with_config(reader, &mut stream_buffer);
12291

12392
let mut key_context = KeyContext::None;

β€Žpicojson/examples/stream_parser_demo.rsβ€Ž

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,9 @@
11
// Example demonstrating StreamParser with a Reader over a fixed-size array
22

3-
use picojson::{Event, ParseError, PullParser, Reader, StreamParser};
4-
5-
/// Simple Reader implementation that reads from a fixed-size byte array
6-
/// This simulates reading from a stream, network socket, or any other byte source
7-
struct ArrayReader<'a> {
8-
data: &'a [u8],
9-
position: usize,
10-
chunk_size: usize, // Simulate streaming by reading in chunks
11-
}
12-
13-
impl<'a> ArrayReader<'a> {
14-
/// Create a new ArrayReader from a byte slice
15-
/// chunk_size controls how many bytes are read at once (simulates network packets)
16-
fn new(data: &'a [u8], chunk_size: usize) -> Self {
17-
Self {
18-
data,
19-
position: 0,
20-
chunk_size,
21-
}
22-
}
23-
}
24-
25-
impl<'a> Reader for ArrayReader<'a> {
26-
type Error = std::io::Error;
27-
28-
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
29-
let remaining = self.data.len().saturating_sub(self.position);
30-
if remaining == 0 {
31-
return Ok(0); // EOF
32-
}
33-
34-
// Read at most chunk_size bytes to simulate streaming behavior
35-
let to_read = remaining.min(buf.len()).min(self.chunk_size);
36-
let end_pos = self.position + to_read;
37-
38-
buf[..to_read].copy_from_slice(&self.data[self.position..end_pos]);
39-
self.position = end_pos;
40-
41-
println!(
42-
" πŸ“– Reader: read {} bytes (pos: {}/{})",
43-
to_read,
44-
self.position,
45-
self.data.len()
46-
);
47-
Ok(to_read)
48-
}
49-
}
3+
use picojson::{ChunkReader, Event, ParseError, PullParser, StreamParser};
504

515
fn main() -> Result<(), ParseError> {
52-
println!("πŸš€ StreamParser Demo with ArrayReader");
6+
println!("πŸš€ StreamParser Demo with ChunkReader");
537
println!("=====================================");
548

559
// Test JSON with various data types including escape sequences
@@ -59,15 +13,15 @@ fn main() -> Result<(), ParseError> {
5913
println!("πŸ“ Total size: {} bytes", json.len());
6014
println!();
6115

62-
// Create ArrayReader that reads in small chunks (simulates network streaming)
63-
let reader = ArrayReader::new(json, 8); // Read 8 bytes at a time
16+
// Create ChunkReader that reads in small chunks (simulates network streaming)
17+
let reader = ChunkReader::new(json, 8); // Read 8 bytes at a time
6418

6519
// Create StreamParser with a reasonably sized buffer
6620
let mut buffer = [0u8; 256];
6721
let buffer_size = buffer.len();
6822
let mut parser = StreamParser::new(reader, &mut buffer);
6923

70-
println!("πŸ”„ Starting StreamParser with streaming ArrayReader:");
24+
println!("πŸ”„ Starting StreamParser with streaming ChunkReader:");
7125
println!(" Buffer size: {} bytes", buffer_size);
7226
println!(" Chunk size: 8 bytes (simulates small network packets)");
7327
println!();

0 commit comments

Comments
Β (0)