1
1
use crate :: Frame ;
2
2
3
3
use bytes:: Bytes ;
4
- use std:: { error , fmt, io , str, vec} ;
4
+ use std:: { fmt, str, vec} ;
5
5
6
6
/// Utility for parsing a command
7
7
#[ derive( Debug ) ]
@@ -12,14 +12,14 @@ pub(crate) struct Parse {
12
12
#[ derive( Debug ) ]
13
13
pub ( crate ) enum ParseError {
14
14
EndOfStream ,
15
- Invalid ,
15
+ Other ( crate :: Error ) ,
16
16
}
17
17
18
18
impl Parse {
19
19
pub ( crate ) fn new ( frame : Frame ) -> Result < Parse , ParseError > {
20
20
let array = match frame {
21
21
Frame :: Array ( array) => array,
22
- _ => return Err ( ParseError :: Invalid ) ,
22
+ frame => return Err ( format ! ( "protocol error; expected array, got {:?}" , frame ) . into ( ) ) ,
23
23
} ;
24
24
25
25
Ok ( Parse {
@@ -39,27 +39,29 @@ impl Parse {
39
39
Frame :: Simple ( s) => Ok ( s) ,
40
40
Frame :: Bulk ( data) => str:: from_utf8 ( & data[ ..] )
41
41
. map ( |s| s. to_string ( ) )
42
- . map_err ( |_| ParseError :: Invalid ) ,
43
- _ => Err ( ParseError :: Invalid ) ,
42
+ . map_err ( |_| "protocol error; invalid string" . into ( ) ) ,
43
+ frame => Err ( format ! ( "protocol error; expected simple frame or bulk frame, got {:?}" , frame ) . into ( ) ) ,
44
44
}
45
45
}
46
46
47
47
pub ( crate ) fn next_bytes ( & mut self ) -> Result < Bytes , ParseError > {
48
48
match self . next ( ) ? {
49
49
Frame :: Simple ( s) => Ok ( Bytes :: from ( s. into_bytes ( ) ) ) ,
50
50
Frame :: Bulk ( data) => Ok ( data) ,
51
- _ => Err ( ParseError :: Invalid ) ,
51
+ frame => Err ( format ! ( "protocol error; expected simple frame or bulk frame, got {:?}" , frame ) . into ( ) ) ,
52
52
}
53
53
}
54
54
55
55
pub ( crate ) fn next_int ( & mut self ) -> Result < u64 , ParseError > {
56
56
use atoi:: atoi;
57
57
58
+ const MSG : & str = "protocol error; invalid number" ;
59
+
58
60
match self . next ( ) ? {
59
61
Frame :: Integer ( v) => Ok ( v) ,
60
- Frame :: Simple ( data) => atoi :: < u64 > ( data. as_bytes ( ) ) . ok_or ( ParseError :: Invalid ) ,
61
- Frame :: Bulk ( data) => atoi :: < u64 > ( & data) . ok_or ( ParseError :: Invalid ) ,
62
- _ => Err ( ParseError :: Invalid ) ,
62
+ Frame :: Simple ( data) => atoi :: < u64 > ( data. as_bytes ( ) ) . ok_or_else ( || MSG . into ( ) ) ,
63
+ Frame :: Bulk ( data) => atoi :: < u64 > ( & data) . ok_or_else ( || MSG . into ( ) ) ,
64
+ frame => Err ( format ! ( "protocol error; expected int frame but got {:?}" , frame ) . into ( ) ) ,
63
65
}
64
66
}
65
67
@@ -68,29 +70,33 @@ impl Parse {
68
70
if self . parts . next ( ) . is_none ( ) {
69
71
Ok ( ( ) )
70
72
} else {
71
- Err ( ParseError :: Invalid )
73
+ Err ( "protocol error; expected end of frame, but there was more" . into ( ) )
72
74
}
73
75
}
74
76
}
75
77
76
- impl From < ParseError > for io:: Error {
77
- fn from ( src : ParseError ) -> io:: Error {
78
- io:: Error :: new ( io:: ErrorKind :: Other , format ! ( "{}" , src) )
78
+ impl From < String > for ParseError {
79
+ fn from ( src : String ) -> ParseError {
80
+ ParseError :: Other ( src. into ( ) )
81
+ }
82
+ }
83
+
84
+ impl From < & str > for ParseError {
85
+ fn from ( src : & str ) -> ParseError {
86
+ src. to_string ( ) . into ( )
79
87
}
80
88
}
81
89
82
90
impl fmt:: Display for ParseError {
83
91
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
84
- let msg = match self {
85
- ParseError :: EndOfStream => "end of stream" . to_string ( ) ,
86
- ParseError :: Invalid => "invalid" . to_string ( ) ,
87
- } ;
88
- write ! ( f, "{}" , & msg)
92
+ match self {
93
+ ParseError :: EndOfStream => {
94
+ "protocol error; unexpected end of stream" . fmt ( f)
95
+ }
96
+ ParseError :: Other ( err) => err. fmt ( f) ,
97
+ }
89
98
}
90
99
}
91
100
92
101
impl std:: error:: Error for ParseError {
93
- fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
94
- None
95
- }
96
102
}
0 commit comments