1
1
use bstr:: BStr ;
2
2
3
- use crate :: { decode, Channel , PacketLineRef , ERR_PREFIX } ;
3
+ use crate :: { decode, BandRef , Channel , ErrorRef , PacketLineRef , TextRef , ERR_PREFIX } ;
4
4
5
5
impl < ' a > PacketLineRef < ' a > {
6
6
/// Return this instance as slice if it's [`Data`][PacketLine::Data].
@@ -20,68 +20,58 @@ impl<'a> PacketLineRef<'a> {
20
20
///
21
21
/// Note that this creates an unchecked error using the slice verbatim, which is useful to [serialize it][Error::write_to()].
22
22
/// See [`check_error()`][PacketLine::check_error()] for a version that assures the error information is in the expected format.
23
- pub fn as_error ( & self ) -> Option < Error < ' _ > > {
24
- self . as_slice ( ) . map ( Error )
23
+ pub fn as_error ( & self ) -> Option < ErrorRef < ' _ > > {
24
+ self . as_slice ( ) . map ( ErrorRef )
25
25
}
26
26
/// Check this instance's [`as_slice()`][PacketLine::as_slice()] is a valid [`Error`] and return it.
27
27
///
28
28
/// This works for any data received in an error [channel][crate::Channel].
29
- pub fn check_error ( & self ) -> Option < Error < ' _ > > {
29
+ pub fn check_error ( & self ) -> Option < ErrorRef < ' _ > > {
30
30
self . as_slice ( ) . and_then ( |data| {
31
31
if data. len ( ) >= ERR_PREFIX . len ( ) && & data[ ..ERR_PREFIX . len ( ) ] == ERR_PREFIX {
32
- Some ( Error ( & data[ ERR_PREFIX . len ( ) ..] ) )
32
+ Some ( ErrorRef ( & data[ ERR_PREFIX . len ( ) ..] ) )
33
33
} else {
34
34
None
35
35
}
36
36
} )
37
37
}
38
38
/// Return this instance as text, with the trailing newline truncated if present.
39
- pub fn as_text ( & self ) -> Option < Text < ' _ > > {
39
+ pub fn as_text ( & self ) -> Option < TextRef < ' _ > > {
40
40
self . as_slice ( ) . map ( Into :: into)
41
41
}
42
42
43
43
/// Interpret the data in this [`slice`][PacketLine::as_slice()] as [`Band`] according to the given `kind` of channel.
44
44
///
45
45
/// Note that this is only relevant in a side-band channel.
46
46
/// See [`decode_band()`][PacketLine::decode_band()] in case `kind` is unknown.
47
- pub fn as_band ( & self , kind : Channel ) -> Option < Band < ' _ > > {
47
+ pub fn as_band ( & self , kind : Channel ) -> Option < BandRef < ' _ > > {
48
48
self . as_slice ( ) . map ( |d| match kind {
49
- Channel :: Data => Band :: Data ( d) ,
50
- Channel :: Progress => Band :: Progress ( d) ,
51
- Channel :: Error => Band :: Error ( d) ,
49
+ Channel :: Data => BandRef :: Data ( d) ,
50
+ Channel :: Progress => BandRef :: Progress ( d) ,
51
+ Channel :: Error => BandRef :: Error ( d) ,
52
52
} )
53
53
}
54
54
55
55
/// Decode the band of this [`slice`][PacketLine::as_slice()], or panic if it is not actually a side-band line.
56
- pub fn decode_band ( & self ) -> Result < Band < ' _ > , decode:: band:: Error > {
56
+ pub fn decode_band ( & self ) -> Result < BandRef < ' _ > , decode:: band:: Error > {
57
57
let d = self . as_slice ( ) . ok_or ( decode:: band:: Error :: NonDataLine ) ?;
58
58
Ok ( match d[ 0 ] {
59
- 1 => Band :: Data ( & d[ 1 ..] ) ,
60
- 2 => Band :: Progress ( & d[ 1 ..] ) ,
61
- 3 => Band :: Error ( & d[ 1 ..] ) ,
59
+ 1 => BandRef :: Data ( & d[ 1 ..] ) ,
60
+ 2 => BandRef :: Progress ( & d[ 1 ..] ) ,
61
+ 3 => BandRef :: Error ( & d[ 1 ..] ) ,
62
62
band => return Err ( decode:: band:: Error :: InvalidSideBand ( band) ) ,
63
63
} )
64
64
}
65
65
}
66
66
67
- /// A packet line representing an Error in a side-band channel.
68
- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
69
- #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
70
- pub struct Error < ' a > ( pub & ' a [ u8 ] ) ;
71
-
72
- /// A packet line representing text, which may include a trailing newline.
73
- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
74
- #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
75
- pub struct Text < ' a > ( pub & ' a [ u8 ] ) ;
76
-
77
- impl < ' a > From < & ' a [ u8 ] > for Text < ' a > {
67
+ impl < ' a > From < & ' a [ u8 ] > for TextRef < ' a > {
78
68
fn from ( d : & ' a [ u8 ] ) -> Self {
79
69
let d = if d[ d. len ( ) - 1 ] == b'\n' { & d[ ..d. len ( ) - 1 ] } else { d } ;
80
- Text ( d)
70
+ TextRef ( d)
81
71
}
82
72
}
83
73
84
- impl < ' a > Text < ' a > {
74
+ impl < ' a > TextRef < ' a > {
85
75
/// Return this instance's data.
86
76
pub fn as_slice ( & self ) -> & [ u8 ] {
87
77
self . 0
@@ -92,18 +82,6 @@ impl<'a> Text<'a> {
92
82
}
93
83
}
94
84
95
- /// A band in a side-band channel.
96
- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
97
- #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
98
- pub enum Band < ' a > {
99
- /// A band carrying data.
100
- Data ( & ' a [ u8 ] ) ,
101
- /// A band carrying user readable progress information.
102
- Progress ( & ' a [ u8 ] ) ,
103
- /// A band carrying user readable errors.
104
- Error ( & ' a [ u8 ] ) ,
105
- }
106
-
107
85
#[ cfg( all( not( feature = "blocking-io" ) , feature = "async-io" ) ) ]
108
86
mod async_io;
109
87
#[ cfg( feature = "blocking-io" ) ]
0 commit comments