gix_packetline_blocking/
lib.rs

1// DO NOT EDIT - this is a copy of gix-packetline/src/lib.rs. Run `just copy-packetline` to update it.
2
3//! Read and write the git packet line wire format without copying it.
4//!
5//! For reading the packet line format use the [`StreamingPeekableIter`], and for writing the [`Writer`].
6//! ## Feature Flags
7#![cfg_attr(
8    all(doc, all(doc, feature = "document-features")),
9    doc = ::document_features::document_features!()
10)]
11#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
12#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
13
14const U16_HEX_BYTES: usize = 4;
15const MAX_DATA_LEN: usize = 65516;
16const MAX_LINE_LEN: usize = MAX_DATA_LEN + U16_HEX_BYTES;
17const FLUSH_LINE: &[u8] = b"0000";
18const DELIMITER_LINE: &[u8] = b"0001";
19const RESPONSE_END_LINE: &[u8] = b"0002";
20const ERR_PREFIX: &[u8] = b"ERR ";
21
22/// One of three side-band types allowing to multiplex information over a single connection.
23#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub enum Channel {
26    /// The usable data itself in any format.
27    Data = 1,
28    /// Progress information in a user-readable format.
29    Progress = 2,
30    /// Error information in a user readable format. Receiving it usually terminates the connection.
31    Error = 3,
32}
33
34mod line;
35///
36pub mod read;
37
38///
39#[cfg(any(feature = "async-io", feature = "blocking-io"))]
40mod write;
41#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
42pub use write::async_io::Writer;
43#[cfg(feature = "blocking-io")]
44pub use write::blocking_io::Writer;
45
46/// A borrowed packet line as it refers to a slice of data by reference.
47#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49pub enum PacketLineRef<'a> {
50    /// A chunk of raw data.
51    Data(&'a [u8]),
52    /// A flush packet.
53    Flush,
54    /// A delimiter packet.
55    Delimiter,
56    /// The end of the response.
57    ResponseEnd,
58}
59
60/// A packet line representing an Error in a side-band channel.
61#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
62#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
63pub struct ErrorRef<'a>(pub &'a [u8]);
64
65/// A packet line representing text, which may include a trailing newline.
66#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
67#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
68pub struct TextRef<'a>(pub &'a [u8]);
69
70/// A band in a side-band channel.
71#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
72#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
73pub enum BandRef<'a> {
74    /// A band carrying data.
75    Data(&'a [u8]),
76    /// A band carrying user readable progress information.
77    Progress(&'a [u8]),
78    /// A band carrying user readable errors.
79    Error(&'a [u8]),
80}
81
82/// Read pack lines one after another, without consuming more than needed from the underlying
83/// [`Read`][std::io::Read]. [`Flush`][PacketLineRef::Flush] lines cause the reader to stop producing lines forever,
84/// leaving [`Read`][std::io::Read] at the start of whatever comes next.
85///
86/// This implementation tries hard not to allocate at all which leads to quite some added complexity and plenty of extra memory copies.
87pub struct StreamingPeekableIter<T> {
88    read: T,
89    peek_buf: Vec<u8>,
90    #[cfg(any(feature = "blocking-io", feature = "async-io"))]
91    buf: Vec<u8>,
92    fail_on_err_lines: bool,
93    delimiters: &'static [PacketLineRef<'static>],
94    is_done: bool,
95    stopped_at: Option<PacketLineRef<'static>>,
96    #[cfg_attr(all(not(feature = "async-io"), not(feature = "blocking-io")), allow(dead_code))]
97    trace: bool,
98}
99
100/// Utilities to help decoding packet lines
101pub mod decode;
102#[doc(inline)]
103pub use decode::all_at_once as decode;
104/// Utilities to encode different kinds of packet lines
105pub mod encode;
106
107#[cfg(all(feature = "async-io", feature = "blocking-io"))]
108compile_error!("Cannot set both 'blocking-io' and 'async-io' features as they are mutually exclusive");