use crate::{low_level::PackFile, Error};
use bytes::{BufMut, BytesMut};
use std::fmt::Write;
const MAX_DATA_LEN: usize = 65516;
pub enum PktLine<'a> {
Data(&'a [u8]),
SidebandData(PackFile<'a>),
SidebandMsg(&'a [u8]),
Flush,
Delimiter,
ResponseEnd,
}
impl PktLine<'_> {
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self, buf), err))]
pub fn encode_to(&self, buf: &mut BytesMut) -> Result<(), Error> {
match self {
Self::Data(data) => {
for chunk in data.chunks(MAX_DATA_LEN) {
write!(buf, "{:04x}", chunk.len() + 4)?;
buf.extend_from_slice(chunk);
}
}
Self::SidebandData(packfile) => {
let mut data_buf = buf.split_off(buf.len());
packfile.encode_to(&mut data_buf)?;
if data_buf.len() + 5 <= MAX_DATA_LEN - 1 {
write!(buf, "{:04x}", data_buf.len() + 5)?;
buf.put_u8(1);
buf.unsplit(data_buf);
} else {
for chunk in data_buf.chunks(MAX_DATA_LEN - 1) {
write!(buf, "{:04x}", chunk.len() + 5)?;
buf.put_u8(1);
buf.extend_from_slice(chunk);
}
}
}
Self::SidebandMsg(msg) => {
for chunk in msg.chunks(MAX_DATA_LEN - 1) {
write!(buf, "{:04x}", chunk.len() + 5)?;
buf.put_u8(2);
buf.extend_from_slice(chunk);
}
}
Self::Flush => buf.extend_from_slice(b"0000"),
Self::Delimiter => buf.extend_from_slice(b"0001"),
Self::ResponseEnd => buf.extend_from_slice(b"0002"),
}
Ok(())
}
}
impl<'a> From<&'a str> for PktLine<'a> {
fn from(val: &'a str) -> Self {
PktLine::Data(val.as_bytes())
}
}
#[cfg(test)]
mod test {
use crate::packet_line::MAX_DATA_LEN;
use bytes::BytesMut;
#[test]
fn test_pkt_line() {
let mut buffer = BytesMut::new();
super::PktLine::from("agent=git/2.32.0\n")
.encode_to(&mut buffer)
.unwrap();
assert_eq!(buffer.as_ref(), b"0015agent=git/2.32.0\n");
}
#[test]
fn test_large_pkt_line() {
let mut buffer = BytesMut::new();
super::PktLine::from("a".repeat(70000).as_str())
.encode_to(&mut buffer)
.unwrap();
assert_eq!(
buffer.len(),
70008,
"should be two chunks each with a 4-byte len header"
);
assert_eq!(
std::str::from_utf8(&buffer[..4]).unwrap(),
format!("{:04x}", 4 + MAX_DATA_LEN)
);
assert!(
&buffer[4..4 + MAX_DATA_LEN]
.iter()
.all(|b| char::from(*b) == 'a'),
"data should be all 'a's"
);
assert_eq!(
std::str::from_utf8(&buffer[4 + MAX_DATA_LEN..][..4]).unwrap(),
format!("{:04x}", 4 + (70000 - MAX_DATA_LEN))
);
assert!(
&buffer[4 + MAX_DATA_LEN + 4..]
.iter()
.all(|b| char::from(*b) == 'a'),
"data should be all 'a's"
);
}
}