Browse Source

[git-packetline] refactor

pull/86/head
Sebastian Thiel 1 month ago
parent
commit
e8b2dd1188
No known key found for this signature in database GPG Key ID: 9CB5EE7895E8268B
  1. 49
      git-packetline/src/immutable/async_io.rs
  2. 47
      git-packetline/src/immutable/blocking_io.rs
  3. 104
      git-packetline/src/immutable/mod.rs
  4. 2
      git-packetline/tests/blocking/read/mod.rs
  5. 2
      git-packetline/tests/blocking/read/sideband.rs

49
git-packetline/src/immutable/async_io.rs

@ -0,0 +1,49 @@
use crate::{
encode,
immutable::{Band, Error, Text},
Channel, PacketLine,
};
use futures_io::AsyncWrite;
use std::io;
impl<'a> Band<'a> {
/// Serialize this instance to `out`, returning the amount of bytes written.
///
/// The data written to `out` can be decoded with [`Borrowed::decode_band()]`.
pub async fn to_write(&self, out: impl AsyncWrite + Unpin) -> 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),
Band::Error(d) => encode::band_to_write(Channel::Error, d, out),
}
.await
}
}
impl<'a> Text<'a> {
/// Serialize this instance to `out`, appending a newline if there is none, returning the amount of bytes written.
pub async fn to_write(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
encode::text_to_write(self.0, out).await
}
}
impl<'a> Error<'a> {
/// 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 async fn to_write(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
encode::error_to_write(self.0, out).await
}
}
impl<'a> PacketLine<'a> {
/// Serialize this instance to `out` in git `packetline` format, returning the amount of bytes written to `out`.
pub async fn to_write(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
match self {
PacketLine::Data(d) => encode::data_to_write(d, out).await,
PacketLine::Flush => encode::flush_to_write(out).await,
PacketLine::Delimiter => encode::delim_to_write(out).await,
PacketLine::ResponseEnd => encode::response_end_to_write(out).await,
}
}
}

47
git-packetline/src/immutable/blocking_io.rs

@ -0,0 +1,47 @@
use crate::{
encode,
immutable::{Band, Error, Text},
Channel, PacketLine,
};
use std::io;
impl<'a> Band<'a> {
/// 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) -> 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),
Band::Error(d) => encode::band_to_write(Channel::Error, d, out),
}
}
}
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) -> io::Result<usize> {
encode::text_to_write(self.0, out)
}
}
impl<'a> Error<'a> {
/// 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) -> 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) -> io::Result<usize> {
match self {
PacketLine::Data(d) => encode::data_to_write(d, out),
PacketLine::Flush => encode::flush_to_write(out),
PacketLine::Delimiter => encode::delim_to_write(out),
PacketLine::ResponseEnd => encode::response_end_to_write(out),
}
}
}

104
git-packetline/src/immutable.rs → git-packetline/src/immutable/mod.rs

@ -131,106 +131,8 @@ pub enum Band<'a> {
/// A band carrying user readable errors.
Error(&'a [u8]),
}
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
mod blocking_io {
use crate::{
encode,
immutable::{Band, Error, Text},
Channel, PacketLine,
};
use futures_io::AsyncWrite;
use std::io;
impl<'a> Band<'a> {
/// Serialize this instance to `out`, returning the amount of bytes written.
///
/// The data written to `out` can be decoded with [`Borrowed::decode_band()]`.
pub async fn to_write(&self, out: impl AsyncWrite + Unpin) -> 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),
Band::Error(d) => encode::band_to_write(Channel::Error, d, out),
}
.await
}
}
impl<'a> Text<'a> {
/// Serialize this instance to `out`, appending a newline if there is none, returning the amount of bytes written.
pub async fn to_write(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
encode::text_to_write(self.0, out).await
}
}
impl<'a> Error<'a> {
/// 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 async fn to_write(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
encode::error_to_write(self.0, out).await
}
}
impl<'a> PacketLine<'a> {
/// Serialize this instance to `out` in git `packetline` format, returning the amount of bytes written to `out`.
pub async fn to_write(&self, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
match self {
PacketLine::Data(d) => encode::data_to_write(d, out).await,
PacketLine::Flush => encode::flush_to_write(out).await,
PacketLine::Delimiter => encode::delim_to_write(out).await,
PacketLine::ResponseEnd => encode::response_end_to_write(out).await,
}
}
}
}
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
mod async_io;
#[cfg(feature = "blocking-io")]
mod blocking_io {
use crate::{
encode,
immutable::{Band, Error, Text},
Channel, PacketLine,
};
use std::io;
impl<'a> Band<'a> {
/// 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) -> 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),
Band::Error(d) => encode::band_to_write(Channel::Error, d, out),
}
}
}
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) -> io::Result<usize> {
encode::text_to_write(self.0, out)
}
}
impl<'a> Error<'a> {
/// 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) -> 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) -> io::Result<usize> {
match self {
PacketLine::Data(d) => encode::data_to_write(d, out),
PacketLine::Flush => encode::flush_to_write(out),
PacketLine::Delimiter => encode::delim_to_write(out),
PacketLine::ResponseEnd => encode::response_end_to_write(out),
}
}
}
}
mod blocking_io;

2
git-packetline/tests/blocking/read/mod.rs

@ -1,6 +1,6 @@
mod sideband;
mod streaming_peek_reader {
mod streaming_peek_iter {
use bstr::ByteSlice;
use git_packetline::PacketLine;
use std::{io, path::PathBuf};

2
git-packetline/tests/blocking/read/sideband.rs

@ -1,4 +1,4 @@
use crate::blocking::read::streaming_peek_reader::fixture_bytes;
use crate::blocking::read::streaming_peek_iter::fixture_bytes;
use bstr::{BString, ByteSlice};
use git_odb::pack;
use git_packetline::PacketLine;

Loading…
Cancel
Save