@@ -5,7 +5,9 @@ use bstr::BString;
5
5
use quick_error:: quick_error;
6
6
7
7
quick_error ! {
8
+ /// The error used in the [`decode`][crate::decode] module
8
9
#[ derive( Debug ) ]
10
+ #[ allow( missing_docs) ]
9
11
pub enum Error {
10
12
HexDecode ( err: String ) {
11
13
display( "Failed to decode the first four hex bytes indicating the line length: {}" , err)
@@ -28,23 +30,32 @@ quick_error! {
28
30
}
29
31
}
30
32
33
+ /// A utility return type to support incremental parsing of packet lines.
31
34
#[ derive( Debug , Clone ) ]
32
35
pub enum Stream < ' a > {
36
+ /// Indicate a single packet line was parsed completely
33
37
Complete {
38
+ /// The parsed packet line
34
39
line : PacketLine < ' a > ,
40
+ /// The amount of bytes consumed from input
35
41
bytes_consumed : usize ,
36
42
} ,
43
+ /// A packet line could not yet be parsed to to missing bytes
37
44
Incomplete {
38
45
/// The amount of additional bytes needed for the parsing to complete
39
46
bytes_needed : usize ,
40
47
} ,
41
48
}
42
49
50
+ /// The result of [`hex_prefix()`] indicating either a special packet line or the amount of wanted bytes
43
51
pub enum PacketLineOrWantedSize < ' a > {
52
+ /// The special kind of packet line decoded from the hex prefix. It never contains actual data.
44
53
Line ( PacketLine < ' a > ) ,
54
+ /// The amount of bytes indicated by the hex prefix of the packet line.
45
55
Wanted ( u16 ) ,
46
56
}
47
57
58
+ /// Decode the `four_bytes` packet line prefix provided in hexadecimal form and check it for validity.
48
59
pub fn hex_prefix ( four_bytes : & [ u8 ] ) -> Result < PacketLineOrWantedSize < ' _ > , Error > {
49
60
debug_assert_eq ! ( four_bytes. len( ) , 4 , "need four hex bytes" ) ;
50
61
for ( line_bytes, line_type) in & [
@@ -60,6 +71,7 @@ pub fn hex_prefix(four_bytes: &[u8]) -> Result<PacketLineOrWantedSize<'_>, Error
60
71
let mut buf = [ 0u8 ; U16_HEX_BYTES / 2 ] ;
61
72
hex:: decode_to_slice ( four_bytes, & mut buf) . map_err ( |err| Error :: HexDecode ( err. to_string ( ) ) ) ?;
62
73
let wanted_bytes = u16:: from_be_bytes ( buf) ;
74
+
63
75
if wanted_bytes == 3 {
64
76
return Err ( Error :: InvalidLineLength ) ;
65
77
}
@@ -73,6 +85,7 @@ pub fn hex_prefix(four_bytes: &[u8]) -> Result<PacketLineOrWantedSize<'_>, Error
73
85
Ok ( PacketLineOrWantedSize :: Wanted ( wanted_bytes - U16_HEX_BYTES as u16 ) )
74
86
}
75
87
88
+ /// Obtain a `PacketLine` from `data` after assuring `data` is small enough to fit.
76
89
pub fn to_data_line ( data : & [ u8 ] ) -> Result < PacketLine < ' _ > , Error > {
77
90
if data. len ( ) > MAX_LINE_LEN {
78
91
return Err ( Error :: DataLengthLimitExceeded ( data. len ( ) ) ) ;
0 commit comments