3 use crate::{decode, BandRef, Channel, ErrorRef, PacketLineRef, TextRef, ERR_PREFIX}
;
5 impl<'a
> PacketLineRef
<'a
> {
6 /// Return this instance as slice if it's [`Data`][PacketLineRef::Data].
7 pub fn as_slice(&self) -> Option
<&'a
[u8]> {
9 PacketLineRef
::Data(d
) => Some(d
),
10 PacketLineRef
::Flush
| PacketLineRef
::Delimiter
| PacketLineRef
::ResponseEnd
=> None
,
13 /// Return this instance's [`as_slice()`][PacketLineRef::as_slice()] as [`BStr`].
14 pub fn as_bstr(&self) -> Option
<&'a BStr
> {
15 self.as_slice().map(Into
::into
)
17 /// Interpret this instance's [`as_slice()`][PacketLineRef::as_slice()] as [`ErrorRef`].
19 /// This works for any data received in an error [channel][crate::Channel].
21 /// Note that this creates an unchecked error using the slice verbatim, which is useful to [serialize it][ErrorRef::write_to()].
22 /// See [`check_error()`][PacketLineRef::check_error()] for a version that assures the error information is in the expected format.
23 pub fn as_error(&self) -> Option
<ErrorRef
<'a
>> {
24 self.as_slice().map(ErrorRef
)
26 /// Check this instance's [`as_slice()`][PacketLineRef::as_slice()] is a valid [`ErrorRef`] and return it.
28 /// This works for any data received in an error [channel][crate::Channel].
29 pub fn check_error(&self) -> Option
<ErrorRef
<'a
>> {
30 self.as_slice().and_then(|data
| {
31 if data
.len() >= ERR_PREFIX
.len() && &data
[..ERR_PREFIX
.len()] == ERR_PREFIX
{
32 Some(ErrorRef(&data
[ERR_PREFIX
.len()..]))
38 /// Return this instance as text, with the trailing newline truncated if present.
39 pub fn as_text(&self) -> Option
<TextRef
<'a
>> {
40 self.as_slice().map(Into
::into
)
43 /// Interpret the data in this [`slice`][PacketLineRef::as_slice()] as [`BandRef`] according to the given `kind` of channel.
45 /// Note that this is only relevant in a side-band channel.
46 /// See [`decode_band()`][PacketLineRef::decode_band()] in case `kind` is unknown.
47 pub fn as_band(&self, kind
: Channel
) -> Option
<BandRef
<'a
>> {
48 self.as_slice().map(|d
| match kind
{
49 Channel
::Data
=> BandRef
::Data(d
),
50 Channel
::Progress
=> BandRef
::Progress(d
),
51 Channel
::Error
=> BandRef
::Error(d
),
55 /// Decode the band of this [`slice`][PacketLineRef::as_slice()]
56 pub fn decode_band(&self) -> Result
<BandRef
<'a
>, decode
::band
::Error
> {
57 let d
= self.as_slice().ok_or(decode
::band
::Error
::NonDataLine
)?
;
59 1 => BandRef
::Data(&d
[1..]),
60 2 => BandRef
::Progress(&d
[1..]),
61 3 => BandRef
::Error(&d
[1..]),
62 band
=> return Err(decode
::band
::Error
::InvalidSideBand { band_id: band }
),
67 impl<'a
> From
<&'a
[u8]> for TextRef
<'a
> {
68 fn from(d
: &'a
[u8]) -> Self {
69 let d
= if d
[d
.len() - 1] == b'
\n' { &d[..d.len() - 1] }
else { d }
;
74 impl<'a
> TextRef
<'a
> {
75 /// Return this instance's data.
76 pub fn as_slice(&self) -> &'a
[u8] {
79 /// Return this instance's data as [`BStr`].
80 pub fn as_bstr(&self) -> &'a BStr
{
85 #[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
87 #[cfg(feature = "blocking-io")]