Skip to content

Commit

Permalink
[git-packetline] Use io::(Result|Error) everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed May 15, 2021
1 parent 34f48c3 commit 374f129
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 37 deletions.
19 changes: 10 additions & 9 deletions git-packetline/src/encode/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ impl<'a, W: AsyncWrite + Unpin> LineWriter<'a, W> {
}
}

fn into_io_err(err: Error) -> io::Error {
io::Error::new(io::ErrorKind::Other, err)
}

impl<W: AsyncWrite + Unpin> AsyncWrite for LineWriter<'_, W> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, data: &[u8]) -> Poll<io::Result<usize>> {
use futures_lite::ready;
fn into_io_err(err: Error) -> io::Error {
io::Error::new(io::ErrorKind::Other, err)
}
let mut this = self.project();
loop {
match &mut this.state {
Expand Down Expand Up @@ -140,13 +141,13 @@ async fn prefixed_and_suffixed_data_to_write(
data: &[u8],
suffix: &[u8],
mut out: impl AsyncWrite + Unpin,
) -> Result<usize, Error> {
) -> io::Result<usize> {
let data_len = prefix.len() + data.len() + suffix.len();
if data_len > MAX_DATA_LEN {
return Err(Error::DataLengthLimitExceeded(data_len));
return Err(into_io_err(Error::DataLengthLimitExceeded(data_len)));
}
if data.is_empty() {
return Err(Error::DataIsEmpty);
return Err(into_io_err(Error::DataIsEmpty));
}

let data_len = data_len + 4;
Expand All @@ -163,16 +164,16 @@ async fn prefixed_and_suffixed_data_to_write(
Ok(data_len)
}

async fn prefixed_data_to_write(prefix: &[u8], data: &[u8], out: impl AsyncWrite + Unpin) -> Result<usize, Error> {
async fn prefixed_data_to_write(prefix: &[u8], data: &[u8], out: impl AsyncWrite + Unpin) -> io::Result<usize> {
prefixed_and_suffixed_data_to_write(prefix, data, &[], out).await
}

/// Write a `text` message to `out`, which is assured to end in a newline.
pub async fn text_to_write(text: &[u8], out: impl AsyncWrite + Unpin) -> Result<usize, Error> {
pub async fn text_to_write(text: &[u8], out: impl AsyncWrite + Unpin) -> io::Result<usize> {
prefixed_and_suffixed_data_to_write(&[], text, &[b'\n'], out).await
}

/// Write a `data` message to `out`.
pub async fn data_to_write(data: &[u8], out: impl AsyncWrite + Unpin) -> Result<usize, Error> {
pub async fn data_to_write(data: &[u8], out: impl AsyncWrite + Unpin) -> io::Result<usize> {
prefixed_data_to_write(&[], data, out).await
}
19 changes: 11 additions & 8 deletions git-packetline/src/encode/blocking_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ pub fn flush_to_write(mut out: impl io::Write) -> io::Result<usize> {
}

/// Write an error `message` to `out`.
pub fn error_to_write(message: &[u8], out: impl io::Write) -> Result<usize, Error> {
pub fn error_to_write(message: &[u8], out: impl io::Write) -> io::Result<usize> {
prefixed_data_to_write(ERR_PREFIX, message, out)
}

/// Write `data` of `kind` to `out` using side-band encoding.
pub fn band_to_write(kind: Channel, data: &[u8], out: impl io::Write) -> Result<usize, Error> {
pub fn band_to_write(kind: Channel, data: &[u8], out: impl io::Write) -> io::Result<usize> {
prefixed_data_to_write(&[kind as u8], data, out)
}

/// Write a `data` message to `out`.
pub fn data_to_write(data: &[u8], out: impl io::Write) -> Result<usize, Error> {
pub fn data_to_write(data: &[u8], out: impl io::Write) -> io::Result<usize> {
prefixed_data_to_write(&[], data, out)
}

/// Write a `text` message to `out`, which is assured to end in a newline.
pub fn text_to_write(text: &[u8], out: impl io::Write) -> Result<usize, Error> {
pub fn text_to_write(text: &[u8], out: impl io::Write) -> io::Result<usize> {
prefixed_and_suffixed_data_to_write(&[], text, &[b'\n'], out)
}

fn prefixed_data_to_write(prefix: &[u8], data: &[u8], out: impl io::Write) -> Result<usize, Error> {
fn prefixed_data_to_write(prefix: &[u8], data: &[u8], out: impl io::Write) -> io::Result<usize> {
prefixed_and_suffixed_data_to_write(prefix, data, &[], out)
}

Expand All @@ -46,13 +46,16 @@ fn prefixed_and_suffixed_data_to_write(
data: &[u8],
suffix: &[u8],
mut out: impl io::Write,
) -> Result<usize, Error> {
) -> io::Result<usize> {
let data_len = prefix.len() + data.len() + suffix.len();
if data_len > MAX_DATA_LEN {
return Err(Error::DataLengthLimitExceeded(data_len));
return Err(io::Error::new(
io::ErrorKind::Other,
Error::DataLengthLimitExceeded(data_len),
));
}
if data.is_empty() {
return Err(Error::DataIsEmpty);
return Err(io::Error::new(io::ErrorKind::Other, Error::DataIsEmpty));
}

let data_len = data_len + 4;
Expand Down
5 changes: 0 additions & 5 deletions git-packetline/src/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ quick_error! {
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
Io(err: std::io::Error) {
display("An error occurred while writing")
from()
source(err)
}
DataLengthLimitExceeded(length_in_bytes: usize) {
display("Cannot encode more than {} bytes, got {}", MAX_DATA_LEN, length_in_bytes)
}
Expand Down
8 changes: 4 additions & 4 deletions git-packetline/src/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod blocking_io {
/// Serialize this instance to `out`, returning the amount of bytes written.
///
/// The data written to `out` can be decoded with [`Borrowed::decode_band()]`.
pub fn to_write(&self, out: impl io::Write) -> Result<usize, encode::Error> {
pub fn to_write(&self, out: impl io::Write) -> io::Result<usize> {
match self {
Band::Data(d) => encode::band_to_write(Channel::Data, d, out),
Band::Progress(d) => encode::band_to_write(Channel::Progress, d, out),
Expand All @@ -156,7 +156,7 @@ mod blocking_io {

impl<'a> Text<'a> {
/// Serialize this instance to `out`, appending a newline if there is none, returning the amount of bytes written.
pub fn to_write(&self, out: impl io::Write) -> Result<usize, encode::Error> {
pub fn to_write(&self, out: impl io::Write) -> io::Result<usize> {
encode::text_to_write(self.0, out)
}
}
Expand All @@ -165,14 +165,14 @@ mod blocking_io {
/// Serialize this line as error to `out`.
///
/// This includes a marker to allow decoding it outside of a side-band channel, returning the amount of bytes written.
pub fn to_write(&self, out: impl io::Write) -> Result<usize, encode::Error> {
pub fn to_write(&self, out: impl io::Write) -> io::Result<usize> {
encode::error_to_write(self.0, out)
}
}

impl<'a> PacketLine<'a> {
/// Serialize this instance to `out` in git `packetline` format, returning the amount of bytes written to `out`.
pub fn to_write(&self, out: impl io::Write) -> Result<usize, encode::Error> {
pub fn to_write(&self, out: impl io::Write) -> io::Result<usize> {
match self {
PacketLine::Data(d) => encode::data_to_write(d, out),
PacketLine::Flush => encode::flush_to_write(out).map_err(Into::into),
Expand Down
11 changes: 1 addition & 10 deletions git-packetline/src/write/blocking_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,7 @@ impl<T: io::Write> io::Write for Writer<T> {
crate::encode::data_to_write(data, &mut self.inner)
} else {
crate::encode::text_to_write(data, &mut self.inner)
}
.map_err(|err| {
use crate::encode::Error::*;
match err {
Io(err) => err,
DataIsEmpty | DataLengthLimitExceeded(_) => {
unreachable!("We are handling empty and large data here, so this can't ever happen")
}
}
})?;
}?;
// subtract header (and trailng NL) because write-all can't handle writing more than it passes in
written -= U16_HEX_BYTES + if self.binary { 0 } else { 1 };
buf = rest;
Expand Down
1 change: 0 additions & 1 deletion git-transport/src/client/blocking/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ impl<'a> RequestWriter<'a> {
MessageKind::Text(t) => git_packetline::immutable::Text::from(t).to_write(self.writer.inner_mut()),
}
.map(|_| ())
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
}
}

Expand Down

0 comments on commit 374f129

Please sign in to comment.