Skip to main content

gix_packetline/blocking_io/
encode.rs

1use std::io;
2
3use crate::{
4    encode::{u16_to_hex, Error},
5    BandRef, Channel, ErrorRef, PacketLineRef, TextRef, DELIMITER_LINE, ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN,
6    RESPONSE_END_LINE,
7};
8
9/// Write a response-end message to `out`.
10pub fn response_end_to_write(mut out: impl io::Write) -> io::Result<usize> {
11    out.write_all(RESPONSE_END_LINE).map(|_| 4)
12}
13
14/// Write a delim message to `out`.
15pub fn delim_to_write(mut out: impl io::Write) -> io::Result<usize> {
16    out.write_all(DELIMITER_LINE).map(|_| 4)
17}
18
19/// Write a flush message to `out`.
20pub fn flush_to_write(mut out: impl io::Write) -> io::Result<usize> {
21    out.write_all(FLUSH_LINE).map(|_| 4)
22}
23
24/// Write an error `message` to `out`.
25pub fn error_to_write(message: &[u8], out: impl io::Write) -> io::Result<usize> {
26    prefixed_data_to_write(ERR_PREFIX, message, out)
27}
28
29/// Serialize `error` to `out`.
30///
31/// This includes a marker to allow decoding it outside a sideband channel, returning the amount of bytes written.
32pub fn write_error(error: &ErrorRef<'_>, out: impl io::Write) -> io::Result<usize> {
33    error_to_write(error.0, out)
34}
35
36/// Write `data` of `kind` to `out` using sideband encoding.
37pub fn band_to_write(kind: Channel, data: &[u8], out: impl io::Write) -> io::Result<usize> {
38    prefixed_data_to_write(&[kind as u8], data, out)
39}
40
41/// Serialize `band` to `out`, returning the amount of bytes written.
42///
43/// The data written to `out` can be decoded with [`PacketLineRef::decode_band()`].
44pub fn write_band(band: &BandRef<'_>, out: impl io::Write) -> io::Result<usize> {
45    match band {
46        BandRef::Data(d) => band_to_write(Channel::Data, d, out),
47        BandRef::Progress(d) => band_to_write(Channel::Progress, d, out),
48        BandRef::Error(d) => band_to_write(Channel::Error, d, out),
49    }
50}
51
52/// Write a `data` message to `out`.
53pub fn data_to_write(data: &[u8], out: impl io::Write) -> io::Result<usize> {
54    prefixed_data_to_write(&[], data, out)
55}
56
57/// Serialize `line` to `out` in git `packetline` format, returning the amount of bytes written to `out`.
58pub fn write_packet_line(line: &PacketLineRef<'_>, out: impl io::Write) -> io::Result<usize> {
59    match line {
60        PacketLineRef::Data(d) => data_to_write(d, out),
61        PacketLineRef::Flush => flush_to_write(out),
62        PacketLineRef::Delimiter => delim_to_write(out),
63        PacketLineRef::ResponseEnd => response_end_to_write(out),
64    }
65}
66
67/// Write a `text` message to `out`, which is assured to end in a newline.
68pub fn text_to_write(text: &[u8], out: impl io::Write) -> io::Result<usize> {
69    prefixed_and_suffixed_data_to_write(&[], text, b"\n", out)
70}
71
72/// Serialize `text` to `out`, appending a newline if there is none, returning the amount of bytes written.
73pub fn write_text(text: &TextRef<'_>, out: impl io::Write) -> io::Result<usize> {
74    text_to_write(text.0, out)
75}
76
77fn prefixed_data_to_write(prefix: &[u8], data: &[u8], out: impl io::Write) -> io::Result<usize> {
78    prefixed_and_suffixed_data_to_write(prefix, data, &[], out)
79}
80
81fn prefixed_and_suffixed_data_to_write(
82    prefix: &[u8],
83    data: &[u8],
84    suffix: &[u8],
85    mut out: impl io::Write,
86) -> io::Result<usize> {
87    let data_len = prefix.len() + data.len() + suffix.len();
88    if data_len > MAX_DATA_LEN {
89        return Err(io::Error::other(Error::DataLengthLimitExceeded {
90            length_in_bytes: data_len,
91        }));
92    }
93    if data.is_empty() {
94        return Err(io::Error::other(Error::DataIsEmpty));
95    }
96
97    let data_len = data_len + 4;
98    let buf = u16_to_hex(data_len as u16);
99
100    out.write_all(&buf)?;
101    if !prefix.is_empty() {
102        out.write_all(prefix)?;
103    }
104    out.write_all(data)?;
105    if !suffix.is_empty() {
106        out.write_all(suffix)?;
107    }
108    Ok(data_len)
109}