@@ -66,20 +66,34 @@ impl TryFrom<Frame> for Command {
66
66
type Error = Error ;
67
67
68
68
fn try_from ( frame : Frame ) -> Result < Self , Self :: Error > {
69
- let mut parser = CommandParser :: try_from ( frame) ?;
69
+ // Clients send commands to the Redis server as RESP arrays.
70
+ let frames = match frame {
71
+ Frame :: Array ( array) => array,
72
+ frame => {
73
+ return Err ( Box :: new ( CommandParserError :: InvalidFrame {
74
+ expected : "array" . to_string ( ) ,
75
+ actual : frame,
76
+ } ) )
77
+ }
78
+ } ;
79
+
80
+ let parser = & mut CommandParser {
81
+ parts : frames. into_iter ( ) ,
82
+ } ;
83
+
70
84
let command_name = parser. parse_command_name ( ) ?;
71
85
72
86
match & command_name[ ..] {
73
- "get" => Get :: try_from ( & mut parser) . map ( Command :: Get ) ,
74
- "set" => Set :: try_from ( & mut parser) . map ( Command :: Set ) ,
75
- "exists" => Exists :: try_from ( & mut parser) . map ( Command :: Exists ) ,
76
- "dbsize" => DBSize :: try_from ( & mut parser) . map ( Command :: DBsize ) ,
77
- "info" => Info :: try_from ( & mut parser) . map ( Command :: Info ) ,
78
- "client" => Client :: try_from ( & mut parser) . map ( Command :: Client ) ,
79
- "module" => Module :: try_from ( & mut parser) . map ( Command :: Module ) ,
80
- "command" => Foo :: try_from ( & mut parser) . map ( Command :: Command ) ,
81
- "config" => Config :: try_from ( & mut parser) . map ( Command :: Config ) ,
82
- "type" => Type :: try_from ( & mut parser) . map ( Command :: Type ) ,
87
+ "get" => Get :: try_from ( parser) . map ( Command :: Get ) ,
88
+ "set" => Set :: try_from ( parser) . map ( Command :: Set ) ,
89
+ "exists" => Exists :: try_from ( parser) . map ( Command :: Exists ) ,
90
+ "dbsize" => DBSize :: try_from ( parser) . map ( Command :: DBsize ) ,
91
+ "info" => Info :: try_from ( parser) . map ( Command :: Info ) ,
92
+ "client" => Client :: try_from ( parser) . map ( Command :: Client ) ,
93
+ "module" => Module :: try_from ( parser) . map ( Command :: Module ) ,
94
+ "command" => Foo :: try_from ( parser) . map ( Command :: Command ) ,
95
+ "config" => Config :: try_from ( parser) . map ( Command :: Config ) ,
96
+ "type" => Type :: try_from ( parser) . map ( Command :: Type ) ,
83
97
name => Err ( format ! ( "protocol error; unknown command {:?}" , name) . into ( ) ) ,
84
98
}
85
99
}
@@ -147,7 +161,7 @@ impl CommandParser {
147
161
}
148
162
}
149
163
150
- #[ derive( Debug , ThisError ) ]
164
+ #[ derive( Debug , ThisError , PartialEq ) ]
151
165
pub ( crate ) enum CommandParserError {
152
166
#[ error( "protocol error; invalid frame, expected {expected}, got {actual}" ) ]
153
167
InvalidFrame { expected : String , actual : Frame } ,
@@ -157,27 +171,6 @@ pub(crate) enum CommandParserError {
157
171
EndOfStream ,
158
172
}
159
173
160
- impl TryFrom < Frame > for CommandParser {
161
- type Error = CommandParserError ;
162
-
163
- fn try_from ( frame : Frame ) -> Result < Self , Self :: Error > {
164
- // Clients send commands to the Redis server as RESP arrays.
165
- let frames = match frame {
166
- Frame :: Array ( array) => array,
167
- frame => {
168
- return Err ( CommandParserError :: InvalidFrame {
169
- expected : "array" . to_string ( ) ,
170
- actual : frame,
171
- } )
172
- }
173
- } ;
174
-
175
- Ok ( Self {
176
- parts : frames. into_iter ( ) ,
177
- } )
178
- }
179
- }
180
-
181
174
#[ cfg( test) ]
182
175
mod tests {
183
176
use super :: * ;
0 commit comments