1#[cfg(any(feature = "blocking-io", feature = "async-io"))]
2use crate::MAX_LINE_LEN;
3use crate::{PacketLineRef, U16_HEX_BYTES};
4
5pub type ProgressAction = std::ops::ControlFlow<()>;
10
11#[cfg(any(feature = "blocking-io", feature = "async-io"))]
12pub(crate) type ExhaustiveOutcome<'a> = (
13 bool, Option<PacketLineRef<'static>>, Option<std::io::Result<Result<PacketLineRef<'a>, crate::decode::Error>>>, );
17
18mod error {
19 use std::fmt::{Debug, Display, Formatter};
20
21 use bstr::BString;
22
23 #[derive(Debug)]
25 pub struct Error {
26 pub message: BString,
28 }
29
30 impl Display for Error {
31 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32 Display::fmt(&self.message, f)
33 }
34 }
35
36 impl std::error::Error for Error {}
37}
38pub use error::Error;
39
40pub struct StreamingPeekableIterState<T> {
42 pub(crate) read: T,
43 pub(crate) peek_buf: Vec<u8>,
44 #[cfg(any(feature = "blocking-io", feature = "async-io"))]
45 pub(crate) buf: Vec<u8>,
46 pub(crate) fail_on_err_lines: bool,
47 pub(crate) delimiters: &'static [PacketLineRef<'static>],
48 pub(crate) is_done: bool,
49 pub(crate) stopped_at: Option<PacketLineRef<'static>>,
50 #[cfg_attr(all(not(feature = "async-io"), not(feature = "blocking-io")), allow(dead_code))]
51 pub(crate) trace: bool,
52}
53
54impl<T> StreamingPeekableIterState<T> {
55 #[cfg(any(feature = "blocking-io", feature = "async-io"))]
58 pub(crate) fn new(read: T, delimiters: &'static [PacketLineRef<'static>], trace: bool) -> Self {
59 Self {
60 read,
61 #[cfg(any(feature = "blocking-io", feature = "async-io"))]
62 buf: vec![0; MAX_LINE_LEN],
63 peek_buf: Vec::new(),
64 delimiters,
65 fail_on_err_lines: false,
66 is_done: false,
67 stopped_at: None,
68 trace,
69 }
70 }
71
72 pub fn peek_buffer_replace_and_truncate(&mut self, position: usize, replace_with: u8) {
80 let position = position + U16_HEX_BYTES;
81 self.peek_buf[position] = replace_with;
82
83 let new_len = position + 1;
84 self.peek_buf.truncate(new_len);
85 self.peek_buf[..4].copy_from_slice(&crate::encode::u16_to_hex((new_len) as u16));
86 }
87
88 pub fn stopped_at(&self) -> Option<PacketLineRef<'static>> {
91 self.stopped_at
92 }
93
94 pub fn reset(&mut self) {
98 let delimiters = std::mem::take(&mut self.delimiters);
99 self.reset_with(delimiters);
100 }
101
102 pub fn reset_with(&mut self, delimiters: &'static [PacketLineRef<'static>]) {
104 self.delimiters = delimiters;
105 self.is_done = false;
106 self.stopped_at = None;
107 }
108
109 pub fn fail_on_err_lines(&mut self, value: bool) {
114 self.fail_on_err_lines = value;
115 }
116
117 pub fn replace(&mut self, read: T) -> T {
119 let prev = std::mem::replace(&mut self.read, read);
120 self.reset();
121 self.fail_on_err_lines = false;
122 prev
123 }
124}