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