Skip to content

Commit

Permalink
[git-packetline] encode module now available as async edition
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed May 15, 2021
1 parent 374f129 commit 119fcc3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 35 deletions.
25 changes: 24 additions & 1 deletion git-packetline/src/encode/async_io.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::u16_to_hex;
use crate::{encode::Error, MAX_DATA_LEN};
use crate::{encode::Error, DELIMITER_LINE, ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN, RESPONSE_END_LINE};
use futures_io::AsyncWrite;
use futures_lite::AsyncWriteExt;
use std::{
Expand Down Expand Up @@ -177,3 +177,26 @@ pub async fn text_to_write(text: &[u8], out: impl AsyncWrite + Unpin) -> io::Res
pub async fn data_to_write(data: &[u8], out: impl AsyncWrite + Unpin) -> io::Result<usize> {
prefixed_data_to_write(&[], data, out).await
}

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

/// Write a response-end message to `out`.
pub async fn response_end_to_write(mut out: impl AsyncWrite + Unpin) -> io::Result<usize> {
out.write_all(RESPONSE_END_LINE).await?;
Ok(4)
}

/// Write a delim message to `out`.
pub async fn delim_to_write(mut out: impl AsyncWrite + Unpin) -> io::Result<usize> {
out.write_all(DELIMITER_LINE).await?;
Ok(4)
}

/// Write a flush message to `out`.
pub async fn flush_to_write(mut out: impl AsyncWrite + Unpin) -> io::Result<usize> {
out.write_all(FLUSH_LINE).await?;
Ok(4)
}
33 changes: 0 additions & 33 deletions git-packetline/tests/blocking/encode.rs

This file was deleted.

1 change: 0 additions & 1 deletion git-packetline/tests/blocking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod decode;
mod encode;
mod read;
38 changes: 38 additions & 0 deletions git-packetline/tests/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,41 @@ mod text_to_write {
Ok(())
}
}

mod error {
use bstr::ByteSlice;
use git_packetline::encode::error_to_write;

#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
async fn write_line() -> crate::Result {
let mut out = Vec::new();
let res = error_to_write(b"hello error", &mut out).await?;
assert_eq!(res, 19);
assert_eq!(out.as_bstr(), b"0013ERR hello error".as_bstr());
Ok(())
}
}

mod flush_delim_response_end {
use bstr::ByteSlice;
use git_packetline::encode::{delim_to_write, flush_to_write, response_end_to_write};

#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
async fn success_flush_delim_response_end() -> crate::Result {
let mut out = Vec::new();
let res = flush_to_write(&mut out).await?;
assert_eq!(res, 4);
assert_eq!(out.as_bstr(), b"0000".as_bstr());

out.clear();
let res = delim_to_write(&mut out).await?;
assert_eq!(res, 4);
assert_eq!(out.as_bstr(), b"0001".as_bstr());

out.clear();
let res = response_end_to_write(&mut out).await?;
assert_eq!(res, 4);
assert_eq!(out.as_bstr(), b"0002".as_bstr());
Ok(())
}
}

0 comments on commit 119fcc3

Please sign in to comment.