@@ -40,8 +40,35 @@ impl<'a> Borrowed<'a> {
40
40
pub fn to_error ( & self ) -> Error {
41
41
Error ( self . as_slice ( ) )
42
42
}
43
+ pub fn to_band ( & self , kind : Channel ) -> Band {
44
+ let d = match self {
45
+ Borrowed :: Data ( d) => d,
46
+ _ => panic ! ( "cannot side-channel non-data lines" ) ,
47
+ } ;
48
+
49
+ match kind {
50
+ Channel :: Data => Band :: Data ( d) ,
51
+ Channel :: Progress => Band :: Progress ( d) ,
52
+ Channel :: Error => Band :: Error ( d) ,
53
+ }
54
+ }
55
+ /// Decode the band of the line, or panic if it is not actually a side-band line
56
+ pub fn decode_band ( & self ) -> Band {
57
+ let d = match self {
58
+ Borrowed :: Data ( d) => d,
59
+ _ => panic ! ( "cannot decode side-channel information from non-data lines" ) ,
60
+ } ;
61
+ match d[ 0 ] {
62
+ 1 => Band :: Data ( & d[ 1 ..] ) ,
63
+ 2 => Band :: Progress ( & d[ 1 ..] ) ,
64
+ 3 => Band :: Error ( & d[ 1 ..] ) ,
65
+ _ => panic ! ( "attempt to decode a non-side channel line" ) ,
66
+ }
67
+ }
43
68
}
44
69
70
+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
71
+ #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
45
72
pub struct Error < ' a > ( & ' a [ u8 ] ) ;
46
73
47
74
impl < ' a > Error < ' a > {
@@ -50,5 +77,31 @@ impl<'a> Error<'a> {
50
77
}
51
78
}
52
79
80
+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
81
+ #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
82
+ pub enum Band < ' a > {
83
+ Data ( & ' a [ u8 ] ) ,
84
+ Progress ( & ' a [ u8 ] ) ,
85
+ Error ( & ' a [ u8 ] ) ,
86
+ }
87
+
88
+ impl < ' a > Band < ' a > {
89
+ pub fn to_write ( & self , out : impl io:: Write ) -> Result < usize , encode:: Error > {
90
+ match self {
91
+ Band :: Data ( d) => encode:: band_to_write ( Channel :: Data , d, out) ,
92
+ Band :: Progress ( d) => encode:: band_to_write ( Channel :: Progress , d, out) ,
93
+ Band :: Error ( d) => encode:: band_to_write ( Channel :: Error , d, out) ,
94
+ }
95
+ }
96
+ }
97
+
98
+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
99
+ #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
100
+ pub enum Channel {
101
+ Data = 1 ,
102
+ Progress = 2 ,
103
+ Error = 3 ,
104
+ }
105
+
53
106
pub mod decode;
54
107
pub mod encode;
0 commit comments