git_packetline/read/
mod.rs1#[cfg(any(feature = "blocking-io", feature = "async-io"))]
2use crate::MAX_LINE_LEN;
3use crate::{PacketLineRef, StreamingPeekableIter, U16_HEX_BYTES};
4
5#[cfg(any(feature = "blocking-io", feature = "async-io"))]
6type ExhaustiveOutcome<'a> = (
7 bool, Option<PacketLineRef<'static>>, Option<std::io::Result<Result<PacketLineRef<'a>, crate::decode::Error>>>, );
11
12mod error {
13 use std::fmt::{Debug, Display, Formatter};
14
15 use bstr::BString;
16
17 #[derive(Debug)]
20 pub struct Error {
21 pub message: BString,
23 }
24
25 impl Display for Error {
26 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27 Display::fmt(&self.message, f)
28 }
29 }
30
31 impl std::error::Error for Error {}
32}
33pub use error::Error;
34
35impl<T> StreamingPeekableIter<T> {
36 pub fn new(read: T, delimiters: &'static [PacketLineRef<'static>]) -> Self {
38 StreamingPeekableIter {
39 read,
40 #[cfg(any(feature = "blocking-io", feature = "async-io"))]
41 buf: vec![0; MAX_LINE_LEN],
42 peek_buf: Vec::new(),
43 delimiters,
44 fail_on_err_lines: false,
45 is_done: false,
46 stopped_at: None,
47 }
48 }
49
50 pub fn peek_buffer_replace_and_truncate(&mut self, position: usize, replace_with: u8) {
58 let position = position + U16_HEX_BYTES;
59 self.peek_buf[position] = replace_with;
60
61 let new_len = position + 1;
62 self.peek_buf.truncate(new_len);
63 self.peek_buf[..4].copy_from_slice(&crate::encode::u16_to_hex((new_len) as u16));
64 }
65
66 pub fn stopped_at(&self) -> Option<PacketLineRef<'static>> {
69 self.stopped_at
70 }
71
72 pub fn reset(&mut self) {
76 let delimiters = std::mem::take(&mut self.delimiters);
77 self.reset_with(delimiters);
78 }
79
80 pub fn reset_with(&mut self, delimiters: &'static [PacketLineRef<'static>]) {
82 self.delimiters = delimiters;
83 self.is_done = false;
84 self.stopped_at = None;
85 }
86
87 pub fn fail_on_err_lines(&mut self, value: bool) {
92 self.fail_on_err_lines = value;
93 }
94
95 pub fn replace(&mut self, read: T) -> T {
97 let prev = std::mem::replace(&mut self.read, read);
98 self.reset();
99 self.fail_on_err_lines = false;
100 prev
101 }
102
103 pub fn into_inner(self) -> T {
105 self.read
106 }
107}
108
109#[cfg(feature = "blocking-io")]
110mod blocking_io;
111
112#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
113mod async_io;
114
115mod sidebands;
116#[cfg(any(feature = "blocking-io", feature = "async-io"))]
117pub use sidebands::WithSidebands;