Skip to content

Commit

Permalink
[git-packetline] the first green test
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed May 14, 2021
1 parent 0220bca commit 916c862
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion git-packetline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doctest = false
[features]
default = []
serde1 = ["serde", "bstr/serde1"]
async-io = ["futures-io"]
async-io = ["futures-io", "pin-project-lite"]
blocking-io = []

[[test]]
Expand All @@ -41,6 +41,7 @@ quick-error = "2.0.0"
hex = "0.4.2"
bstr = { version = "0.2.13", default-features = false, features = ["std"] }
futures-io = { version = "0.3.15", optional = true }
pin-project-lite = { version = "0.2.6", optional = true }

[dev-dependencies]
futures-lite = "1.11.3"
Expand Down
46 changes: 33 additions & 13 deletions git-packetline/src/write.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/// An implementor of [`Write`][io::Write] which passes all input to an inner `Write` in packet line data encoding,
/// one line per `write(…)` call or as many lines as it takes if the data doesn't fit into the maximum allowed line length.
pub struct Writer<T> {
/// the `Write` implementation to which to propagate packet lines
pub inner: T,
binary: bool,
}

/// Non-IO methods
impl<T> Writer<T> {
/// If called, each call to [`write()`][io::Write::write()] will write bytes as is.
Expand All @@ -28,16 +20,26 @@ impl<T> Writer<T> {
self
}
}
// #[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
mod async_io {
use crate::{Writer, MAX_DATA_LEN, U16_HEX_BYTES};
use crate::{MAX_DATA_LEN, U16_HEX_BYTES};
use futures_io::AsyncWrite;
use std::{
io,
pin::Pin,
task::{Context, Poll},
};

pin_project_lite::pin_project! {
/// An implementor of [`Write`][io::Write] which passes all input to an inner `Write` in packet line data encoding,
/// one line per `write(…)` call or as many lines as it takes if the data doesn't fit into the maximum allowed line length.
pub struct Writer<T> {
#[pin]
inner: T,
pub(crate) binary: bool,
}
}

impl<T: AsyncWrite> Writer<T> {
/// Create a new instance from the given `write`
pub fn new(write: T) -> Self {
Expand All @@ -49,24 +51,40 @@ mod async_io {
}
impl<T: AsyncWrite> AsyncWrite for Writer<T> {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
todo!()
if buf.is_empty() {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::Other,
"empty packet lines are not permitted as '0004' is invalid",
)));
}
todo!("other writing")
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
todo!()
self.project().inner.poll_flush(cx)
}

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
todo!()
}
}
}
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
pub use async_io::Writer;

#[cfg(feature = "blocking-io")]
mod blocking_io {
use crate::{Writer, MAX_DATA_LEN, U16_HEX_BYTES};
use crate::{MAX_DATA_LEN, U16_HEX_BYTES};
use std::io;

/// An implementor of [`Write`][io::Write] which passes all input to an inner `Write` in packet line data encoding,
/// one line per `write(…)` call or as many lines as it takes if the data doesn't fit into the maximum allowed line length.
pub struct Writer<T> {
/// the `Write` implementation to which to propagate packet lines
pub inner: T,
pub(crate) binary: bool,
}

impl<T: io::Write> Writer<T> {
/// Create a new instance from the given `write`
pub fn new(write: T) -> Self {
Expand Down Expand Up @@ -115,3 +133,5 @@ mod blocking_io {
}
}
}
#[cfg(feature = "blocking-io")]
pub use blocking_io::Writer;

0 comments on commit 916c862

Please sign in to comment.