gix_transport/client/blocking_io/
bufread_ext.rsuse std::{
io,
ops::{Deref, DerefMut},
};
use gix_packetline::{read::ProgressAction, PacketLineRef};
use crate::{
client::{Error, MessageKind},
Protocol,
};
pub type HandleProgress<'a> = Box<dyn FnMut(bool, &[u8]) -> ProgressAction + 'a>;
pub trait ReadlineBufRead: io::BufRead {
fn readline(
&mut self,
) -> Option<io::Result<Result<gix_packetline::PacketLineRef<'_>, gix_packetline::decode::Error>>>;
fn readline_str(&mut self, line: &mut String) -> io::Result<usize>;
}
pub trait ExtendedBufRead<'a>: ReadlineBufRead {
fn set_progress_handler(&mut self, handle_progress: Option<HandleProgress<'a>>);
fn peek_data_line(&mut self) -> Option<io::Result<Result<&[u8], Error>>>;
fn reset(&mut self, version: Protocol);
fn stopped_at(&self) -> Option<MessageKind>;
}
impl<T: ReadlineBufRead + ?Sized> ReadlineBufRead for Box<T> {
fn readline(&mut self) -> Option<io::Result<Result<PacketLineRef<'_>, gix_packetline::decode::Error>>> {
ReadlineBufRead::readline(self.deref_mut())
}
fn readline_str(&mut self, line: &mut String) -> io::Result<usize> {
ReadlineBufRead::readline_str(self.deref_mut(), line)
}
}
impl<'a, T: ExtendedBufRead<'a> + ?Sized + 'a> ExtendedBufRead<'a> for Box<T> {
fn set_progress_handler(&mut self, handle_progress: Option<HandleProgress<'a>>) {
self.deref_mut().set_progress_handler(handle_progress);
}
fn peek_data_line(&mut self) -> Option<io::Result<Result<&[u8], Error>>> {
self.deref_mut().peek_data_line()
}
fn reset(&mut self, version: Protocol) {
self.deref_mut().reset(version);
}
fn stopped_at(&self) -> Option<MessageKind> {
self.deref().stopped_at()
}
}
impl<T: io::Read> ReadlineBufRead for gix_packetline::read::WithSidebands<'_, T, fn(bool, &[u8]) -> ProgressAction> {
fn readline(&mut self) -> Option<io::Result<Result<PacketLineRef<'_>, gix_packetline::decode::Error>>> {
self.read_data_line()
}
fn readline_str(&mut self, line: &mut String) -> io::Result<usize> {
self.read_line_to_string(line)
}
}
impl<'a, T: io::Read> ReadlineBufRead for gix_packetline::read::WithSidebands<'a, T, HandleProgress<'a>> {
fn readline(&mut self) -> Option<io::Result<Result<PacketLineRef<'_>, gix_packetline::decode::Error>>> {
self.read_data_line()
}
fn readline_str(&mut self, line: &mut String) -> io::Result<usize> {
self.read_line_to_string(line)
}
}
impl<'a, T: io::Read> ExtendedBufRead<'a> for gix_packetline::read::WithSidebands<'a, T, HandleProgress<'a>> {
fn set_progress_handler(&mut self, handle_progress: Option<HandleProgress<'a>>) {
self.set_progress_handler(handle_progress);
}
fn peek_data_line(&mut self) -> Option<io::Result<Result<&[u8], Error>>> {
match self.peek_data_line() {
Some(Ok(Ok(line))) => Some(Ok(Ok(line))),
Some(Ok(Err(err))) => Some(Ok(Err(err.into()))),
Some(Err(err)) => Some(Err(err)),
None => None,
}
}
fn reset(&mut self, version: Protocol) {
match version {
Protocol::V0 | Protocol::V1 => self.reset_with(&[gix_packetline::PacketLineRef::Flush]),
Protocol::V2 => self.reset_with(&[
gix_packetline::PacketLineRef::Delimiter,
gix_packetline::PacketLineRef::Flush,
]),
}
}
fn stopped_at(&self) -> Option<MessageKind> {
self.stopped_at().map(|l| match l {
gix_packetline::PacketLineRef::Flush => MessageKind::Flush,
gix_packetline::PacketLineRef::Delimiter => MessageKind::Delimiter,
gix_packetline::PacketLineRef::ResponseEnd => MessageKind::ResponseEnd,
gix_packetline::PacketLineRef::Data(_) => unreachable!("data cannot be a delimiter"),
})
}
}