#[cfg(any(feature = "blocking-io", feature = "async-io"))]
use crate::MAX_LINE_LEN;
use crate::{PacketLineRef, U16_HEX_BYTES};
pub type ProgressAction = std::ops::ControlFlow<()>;
#[cfg(any(feature = "blocking-io", feature = "async-io"))]
pub(crate) type ExhaustiveOutcome<'a> = (
bool, Option<PacketLineRef<'static>>, Option<std::io::Result<Result<PacketLineRef<'a>, crate::decode::Error>>>, );
mod error {
use std::fmt::{Debug, Display, Formatter};
use bstr::BString;
#[derive(Debug)]
pub struct Error {
pub message: BString,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.message, f)
}
}
impl std::error::Error for Error {}
}
pub use error::Error;
pub struct StreamingPeekableIterState<T> {
pub(crate) read: T,
pub(crate) peek_buf: Vec<u8>,
#[cfg(any(feature = "blocking-io", feature = "async-io"))]
pub(crate) buf: Vec<u8>,
pub(crate) fail_on_err_lines: bool,
pub(crate) delimiters: &'static [PacketLineRef<'static>],
pub(crate) is_done: bool,
pub(crate) stopped_at: Option<PacketLineRef<'static>>,
#[cfg_attr(all(not(feature = "async-io"), not(feature = "blocking-io")), allow(dead_code))]
pub(crate) trace: bool,
}
impl<T> StreamingPeekableIterState<T> {
#[cfg(any(feature = "blocking-io", feature = "async-io"))]
pub(crate) fn new(read: T, delimiters: &'static [PacketLineRef<'static>], trace: bool) -> Self {
Self {
read,
#[cfg(any(feature = "blocking-io", feature = "async-io"))]
buf: vec![0; MAX_LINE_LEN],
peek_buf: Vec::new(),
delimiters,
fail_on_err_lines: false,
is_done: false,
stopped_at: None,
trace,
}
}
pub fn peek_buffer_replace_and_truncate(&mut self, position: usize, replace_with: u8) {
let position = position + U16_HEX_BYTES;
self.peek_buf[position] = replace_with;
let new_len = position + 1;
self.peek_buf.truncate(new_len);
self.peek_buf[..4].copy_from_slice(&crate::encode::u16_to_hex((new_len) as u16));
}
pub fn stopped_at(&self) -> Option<PacketLineRef<'static>> {
self.stopped_at
}
pub fn reset(&mut self) {
let delimiters = std::mem::take(&mut self.delimiters);
self.reset_with(delimiters);
}
pub fn reset_with(&mut self, delimiters: &'static [PacketLineRef<'static>]) {
self.delimiters = delimiters;
self.is_done = false;
self.stopped_at = None;
}
pub fn fail_on_err_lines(&mut self, value: bool) {
self.fail_on_err_lines = value;
}
pub fn replace(&mut self, read: T) -> T {
let prev = std::mem::replace(&mut self.read, read);
self.reset();
self.fail_on_err_lines = false;
prev
}
}