Enum git_packetline::PacketLineRef
source · pub enum PacketLineRef<'a> {
Data(&'a [u8]),
Flush,
Delimiter,
ResponseEnd,
}
Expand description
A borrowed packet line as it refers to a slice of data by reference.
Variants§
Data(&'a [u8])
A chunk of raw data.
Flush
A flush packet.
Delimiter
A delimiter packet.
ResponseEnd
The end of the response.
Implementations§
source§impl<'a> PacketLineRef<'a>
impl<'a> PacketLineRef<'a>
source§impl<'a> PacketLineRef<'a>
impl<'a> PacketLineRef<'a>
sourcepub fn as_slice(&self) -> Option<&'a [u8]>
pub fn as_slice(&self) -> Option<&'a [u8]>
Return this instance as slice if it’s Data
.
Examples found in repository?
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
pub fn as_bstr(&self) -> Option<&'a BStr> {
self.as_slice().map(Into::into)
}
/// Interpret this instance's [`as_slice()`][PacketLineRef::as_slice()] as [`ErrorRef`].
///
/// This works for any data received in an error [channel][crate::Channel].
///
/// Note that this creates an unchecked error using the slice verbatim, which is useful to [serialize it][ErrorRef::write_to()].
/// See [`check_error()`][PacketLineRef::check_error()] for a version that assures the error information is in the expected format.
pub fn as_error(&self) -> Option<ErrorRef<'a>> {
self.as_slice().map(ErrorRef)
}
/// Check this instance's [`as_slice()`][PacketLineRef::as_slice()] is a valid [`ErrorRef`] and return it.
///
/// This works for any data received in an error [channel][crate::Channel].
pub fn check_error(&self) -> Option<ErrorRef<'a>> {
self.as_slice().and_then(|data| {
if data.len() >= ERR_PREFIX.len() && &data[..ERR_PREFIX.len()] == ERR_PREFIX {
Some(ErrorRef(&data[ERR_PREFIX.len()..]))
} else {
None
}
})
}
/// Return this instance as text, with the trailing newline truncated if present.
pub fn as_text(&self) -> Option<TextRef<'a>> {
self.as_slice().map(Into::into)
}
/// Interpret the data in this [`slice`][PacketLineRef::as_slice()] as [`BandRef`] according to the given `kind` of channel.
///
/// Note that this is only relevant in a side-band channel.
/// See [`decode_band()`][PacketLineRef::decode_band()] in case `kind` is unknown.
pub fn as_band(&self, kind: Channel) -> Option<BandRef<'a>> {
self.as_slice().map(|d| match kind {
Channel::Data => BandRef::Data(d),
Channel::Progress => BandRef::Progress(d),
Channel::Error => BandRef::Error(d),
})
}
/// Decode the band of this [`slice`][PacketLineRef::as_slice()]
pub fn decode_band(&self) -> Result<BandRef<'a>, decode::band::Error> {
let d = self.as_slice().ok_or(decode::band::Error::NonDataLine)?;
Ok(match d[0] {
1 => BandRef::Data(&d[1..]),
2 => BandRef::Progress(&d[1..]),
3 => BandRef::Error(&d[1..]),
band => return Err(decode::band::Error::InvalidSideBand { band_id: band }),
})
}
More examples
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn read_line_inner_exhaustive<'a>(
reader: &mut T,
buf: &'a mut Vec<u8>,
delimiters: &[PacketLineRef<'static>],
fail_on_err_lines: bool,
buf_resize: bool,
) -> ExhaustiveOutcome<'a> {
(
false,
None,
Some(match Self::read_line_inner(reader, buf) {
Ok(Ok(line)) => {
if delimiters.contains(&line) {
let stopped_at = delimiters.iter().find(|l| **l == line).cloned();
buf.clear();
return (true, stopped_at, None);
} else if fail_on_err_lines {
if let Some(err) = line.check_error() {
let err = err.0.as_bstr().to_owned();
buf.clear();
return (
true,
None,
Some(Err(io::Error::new(
io::ErrorKind::Other,
crate::read::Error { message: err },
))),
);
}
}
let len = line
.as_slice()
.map(|s| s.len() + U16_HEX_BYTES)
.unwrap_or(U16_HEX_BYTES);
if buf_resize {
buf.resize(len, 0);
}
Ok(Ok(crate::decode(buf).expect("only valid data here")))
}
Ok(Err(err)) => {
buf.clear();
Ok(Err(err))
}
Err(err) => {
buf.clear();
Err(err)
}
}),
)
}
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
fn fill_buf(&mut self) -> io::Result<&[u8]> {
if self.pos >= self.cap {
let (ofs, cap) = loop {
let line = match self.parent.read_line() {
Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
None => break (0, 0),
};
match self.handle_progress.as_mut() {
Some(handle_progress) => {
let band = line
.decode_band()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
const ENCODED_BAND: usize = 1;
match band {
BandRef::Data(d) => {
if d.is_empty() {
continue;
}
break (U16_HEX_BYTES + ENCODED_BAND, d.len());
}
BandRef::Progress(d) => {
let text = TextRef::from(d).0;
handle_progress(false, text);
}
BandRef::Error(d) => {
let text = TextRef::from(d).0;
handle_progress(true, text);
}
};
}
None => {
break match line.as_slice() {
Some(d) => (U16_HEX_BYTES, d.len()),
None => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"encountered non-data line in a data-line only context",
))
}
}
}
}
};
self.cap = cap + ofs;
self.pos = ofs;
}
Ok(&self.parent.buf[self.pos..self.cap])
}
sourcepub fn as_error(&self) -> Option<ErrorRef<'a>>
pub fn as_error(&self) -> Option<ErrorRef<'a>>
Interpret this instance’s as_slice()
as ErrorRef
.
This works for any data received in an error channel.
Note that this creates an unchecked error using the slice verbatim, which is useful to serialize it.
See check_error()
for a version that assures the error information is in the expected format.
sourcepub fn check_error(&self) -> Option<ErrorRef<'a>>
pub fn check_error(&self) -> Option<ErrorRef<'a>>
Check this instance’s as_slice()
is a valid ErrorRef
and return it.
This works for any data received in an error channel.
Examples found in repository?
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn read_line_inner_exhaustive<'a>(
reader: &mut T,
buf: &'a mut Vec<u8>,
delimiters: &[PacketLineRef<'static>],
fail_on_err_lines: bool,
buf_resize: bool,
) -> ExhaustiveOutcome<'a> {
(
false,
None,
Some(match Self::read_line_inner(reader, buf) {
Ok(Ok(line)) => {
if delimiters.contains(&line) {
let stopped_at = delimiters.iter().find(|l| **l == line).cloned();
buf.clear();
return (true, stopped_at, None);
} else if fail_on_err_lines {
if let Some(err) = line.check_error() {
let err = err.0.as_bstr().to_owned();
buf.clear();
return (
true,
None,
Some(Err(io::Error::new(
io::ErrorKind::Other,
crate::read::Error { message: err },
))),
);
}
}
let len = line
.as_slice()
.map(|s| s.len() + U16_HEX_BYTES)
.unwrap_or(U16_HEX_BYTES);
if buf_resize {
buf.resize(len, 0);
}
Ok(Ok(crate::decode(buf).expect("only valid data here")))
}
Ok(Err(err)) => {
buf.clear();
Ok(Err(err))
}
Err(err) => {
buf.clear();
Err(err)
}
}),
)
}
sourcepub fn as_text(&self) -> Option<TextRef<'a>>
pub fn as_text(&self) -> Option<TextRef<'a>>
Return this instance as text, with the trailing newline truncated if present.
sourcepub fn as_band(&self, kind: Channel) -> Option<BandRef<'a>>
pub fn as_band(&self, kind: Channel) -> Option<BandRef<'a>>
Interpret the data in this slice
as BandRef
according to the given kind
of channel.
Note that this is only relevant in a side-band channel.
See decode_band()
in case kind
is unknown.
sourcepub fn decode_band(&self) -> Result<BandRef<'a>, Error>
pub fn decode_band(&self) -> Result<BandRef<'a>, Error>
Decode the band of this slice
Examples found in repository?
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
fn fill_buf(&mut self) -> io::Result<&[u8]> {
if self.pos >= self.cap {
let (ofs, cap) = loop {
let line = match self.parent.read_line() {
Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
None => break (0, 0),
};
match self.handle_progress.as_mut() {
Some(handle_progress) => {
let band = line
.decode_band()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
const ENCODED_BAND: usize = 1;
match band {
BandRef::Data(d) => {
if d.is_empty() {
continue;
}
break (U16_HEX_BYTES + ENCODED_BAND, d.len());
}
BandRef::Progress(d) => {
let text = TextRef::from(d).0;
handle_progress(false, text);
}
BandRef::Error(d) => {
let text = TextRef::from(d).0;
handle_progress(true, text);
}
};
}
None => {
break match line.as_slice() {
Some(d) => (U16_HEX_BYTES, d.len()),
None => {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"encountered non-data line in a data-line only context",
))
}
}
}
}
};
self.cap = cap + ofs;
self.pos = ofs;
}
Ok(&self.parent.buf[self.pos..self.cap])
}
Trait Implementations§
source§impl<'a> Clone for PacketLineRef<'a>
impl<'a> Clone for PacketLineRef<'a>
source§fn clone(&self) -> PacketLineRef<'a>
fn clone(&self) -> PacketLineRef<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for PacketLineRef<'a>
impl<'a> Debug for PacketLineRef<'a>
source§impl<'de: 'a, 'a> Deserialize<'de> for PacketLineRef<'a>
impl<'de: 'a, 'a> Deserialize<'de> for PacketLineRef<'a>
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<'a> Hash for PacketLineRef<'a>
impl<'a> Hash for PacketLineRef<'a>
source§impl<'a> Ord for PacketLineRef<'a>
impl<'a> Ord for PacketLineRef<'a>
source§fn cmp(&self, other: &PacketLineRef<'a>) -> Ordering
fn cmp(&self, other: &PacketLineRef<'a>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<'a> PartialEq<PacketLineRef<'a>> for PacketLineRef<'a>
impl<'a> PartialEq<PacketLineRef<'a>> for PacketLineRef<'a>
source§fn eq(&self, other: &PacketLineRef<'a>) -> bool
fn eq(&self, other: &PacketLineRef<'a>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialOrd<PacketLineRef<'a>> for PacketLineRef<'a>
impl<'a> PartialOrd<PacketLineRef<'a>> for PacketLineRef<'a>
source§fn partial_cmp(&self, other: &PacketLineRef<'a>) -> Option<Ordering>
fn partial_cmp(&self, other: &PacketLineRef<'a>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more