| 1 |
mod parser; |
| 2 |
|
| 3 |
use std::io::Read; |
| 4 |
|
| 5 |
use winnow::error::ContextError; |
| 6 |
use winnow::error::ErrMode; |
| 7 |
use winnow::error::Needed; |
| 8 |
use winnow::prelude::*; |
| 9 |
use winnow::stream::Offset; |
| 10 |
|
| 11 |
fn main() -> Result<(), lexopt::Error> { |
| 12 |
let args = Args::parse()?; |
| 13 |
let input = args.input.ok_or_else(|| lexopt::Error::MissingValue { |
| 14 |
option: Some("<PATH>".to_owned()), |
| 15 |
})?; |
| 16 |
|
| 17 |
let mut file = std::fs::File::open(input).map_err(to_lexopt)?; |
| 18 |
|
| 19 |
// Intentionally starting with a small buffer to make it easier to show `Incomplete` handling |
| 20 |
let buffer_size = 10; |
| 21 |
let min_buffer_growth = 100; |
| 22 |
let buffer_growth_factor = 2; |
| 23 |
let mut buffer = circular::Buffer::with_capacity(buffer_size); |
| 24 |
loop { |
| 25 |
let read = file.read(buffer.space()).map_err(to_lexopt)?; |
| 26 |
eprintln!("read {read}"); |
| 27 |
if read == 0 { |
| 28 |
// Should be EOF since we always make sure there is `available_space` |
| 29 |
assert_ne!(buffer.available_space(), 0); |
| 30 |
assert_eq!( |
| 31 |
buffer.available_data(), |
| 32 |
0, |
| 33 |
"leftover data: {}", |
| 34 |
String::from_utf8_lossy(buffer.data()) |
| 35 |
); |
| 36 |
break; |
| 37 |
} |
| 38 |
buffer.fill(read); |
| 39 |
|
| 40 |
loop { |
| 41 |
let mut input = |
| 42 |
parser::Stream::new(std::str::from_utf8(buffer.data()).map_err(to_lexopt)?); |
| 43 |
let start = input.checkpoint(); |
| 44 |
match parser::ndjson::<ContextError>.parse_next(&mut input) { |
| 45 |
Ok(value) => { |
| 46 |
println!("{value:?}"); |
| 47 |
println!(); |
| 48 |
// Tell the buffer how much we read |
| 49 |
let consumed = input.offset_from(&start); |
| 50 |
buffer.consume(consumed); |
| 51 |
} |
| 52 |
Err(ErrMode::Backtrack(e)) | Err(ErrMode::Cut(e)) => { |
| 53 |
return Err(fmt_lexopt(e.to_string())); |
| 54 |
} |
| 55 |
Err(ErrMode::Incomplete(Needed::Size(size))) => { |
| 56 |
// Without the format telling us how much space is required, we really should |
| 57 |
// treat this the same as `Unknown` but are doing this to demonstrate how to |
| 58 |
// handle `Size`. |
| 59 |
// |
| 60 |
// Even when the format has a header to tell us `Size`, we could hit incidental |
| 61 |
// `Size(1)`s, so make sure we buffer more space than that to avoid reading |
| 62 |
// one byte at a time |
| 63 |
let head_room = size.get().max(min_buffer_growth); |
| 64 |
let new_capacity = buffer.available_data() + head_room; |
| 65 |
eprintln!("growing buffer to {new_capacity}"); |
| 66 |
buffer.grow(new_capacity); |
| 67 |
if buffer.available_space() < head_room { |
| 68 |
eprintln!("buffer shift"); |
| 69 |
buffer.shift(); |
| 70 |
} |
| 71 |
break; |
| 72 |
} |
| 73 |
Err(ErrMode::Incomplete(Needed::Unknown)) => { |
| 74 |
let new_capacity = buffer_growth_factor * buffer.capacity(); |
| 75 |
eprintln!("growing buffer to {new_capacity}"); |
| 76 |
buffer.grow(new_capacity); |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
Ok(()) |
| 84 |
} |
| 85 |
|
| 86 |
#[derive(Default)] |
| 87 |
struct Args { |
| 88 |
input: Option<std::path::PathBuf>, |
| 89 |
} |
| 90 |
|
| 91 |
impl Args { |
| 92 |
fn parse() -> Result<Self, lexopt::Error> { |
| 93 |
use lexopt::prelude::*; |
| 94 |
|
| 95 |
let mut res = Args::default(); |
| 96 |
|
| 97 |
let mut args = lexopt::Parser::from_env(); |
| 98 |
while let Some(arg) = args.next()? { |
| 99 |
match arg { |
| 100 |
Value(input) => { |
| 101 |
res.input = Some(input.into()); |
| 102 |
} |
| 103 |
_ => return Err(arg.unexpected()), |
| 104 |
} |
| 105 |
} |
| 106 |
Ok(res) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
fn to_lexopt(e: impl std::error::Error + Send + Sync + 'static) -> lexopt::Error { |
| 111 |
lexopt::Error::Custom(Box::new(e)) |
| 112 |
} |
| 113 |
|
| 114 |
fn fmt_lexopt(e: String) -> lexopt::Error { |
| 115 |
lexopt::Error::Custom(e.into()) |
| 116 |
} |