@@ -77,6 +77,8 @@ pub enum Element {
7777 UnsignedInt ( u64 ) ,
7878 /// A response code
7979 RespCode ( RespCode ) ,
80+ /// A floating point value
81+ Float ( f64 ) ,
8082}
8183
8284impl Element {
@@ -370,6 +372,24 @@ impl<'a> Parser<'a> {
370372 Err ( ParseError :: UnexpectedByte )
371373 }
372374 }
375+ fn parse_next_float ( & mut self ) -> ParseResult < f64 > {
376+ let our_float_chunk = self . __get_next_element ( ) ?;
377+ match String :: from_utf8_lossy ( our_float_chunk) . parse ( ) {
378+ Ok ( f) => {
379+ if self . will_cursor_give_linefeed ( ) ? {
380+ self . incr_cursor ( ) ;
381+ Ok ( f)
382+ } else {
383+ println ! ( "LF error" ) ;
384+ Err ( ParseError :: UnexpectedByte )
385+ }
386+ }
387+ Err ( e) => {
388+ println ! ( "Error: {}" , e) ;
389+ Err ( ParseError :: UnexpectedByte )
390+ }
391+ }
392+ }
373393 /// The cursor should be **at the tsymbol**
374394 fn parse_next_element ( & mut self ) -> ParseResult < Element > {
375395 if let Some ( tsymbol) = self . buffer . get ( self . cursor ) {
@@ -382,6 +402,7 @@ impl<'a> Parser<'a> {
382402 b':' => Element :: UnsignedInt ( self . parse_next_u64 ( ) ?) ,
383403 b'&' => Element :: Array ( Array :: Recursive ( self . parse_next_array ( ) ?) ) ,
384404 b'!' => Element :: RespCode ( self . parse_next_respcode ( ) ?) ,
405+ b'%' => Element :: Float ( self . parse_next_float ( ) ?) ,
385406 b'@' => {
386407 // hmmm, a typed array; let's check the tsymbol
387408 if let Some ( array_type) = self . buffer . get ( self . cursor ) {
@@ -594,3 +615,11 @@ fn test_typed_str_array_null() {
594615 ] ) ) )
595616 ) ;
596617}
618+
619+ #[ test]
620+ fn test_parse_float ( ) {
621+ let packet = b"*1\n %3\n 1.1\n " ;
622+ let ( parsed, forward) = Parser :: new ( packet) . parse ( ) . unwrap ( ) ;
623+ assert_eq ! ( forward, packet. len( ) ) ;
624+ assert_eq ! ( parsed, RawResponse :: SimpleQuery ( Element :: Float ( 1.1 ) ) )
625+ }
0 commit comments