2727//! by Sayan Nandan and this is the first client implementation of the protocol
2828//!
2929
30+ use crate :: types:: Array ;
3031use crate :: types:: FlatElement ;
3132use crate :: RespCode ;
3233use std:: hint:: unreachable_unchecked;
@@ -65,24 +66,19 @@ pub(super) struct Parser<'a> {
6566/// This enum represents the data types supported by the Skyhash Protocol
6667pub enum Element {
6768 /// Arrays can be nested! Their `<tsymbol>` is `&`
68- Array ( Vec < Element > ) ,
69+ Array ( Array ) ,
6970 /// An unicode string value; `<tsymbol>` is `+`
7071 Str ( String ) ,
7172 /// A binary string (`?`)
7273 Binstr ( Vec < u8 > ) ,
7374 /// An unsigned integer value; `<tsymbol>` is `:`
7475 UnsignedInt ( u64 ) ,
75- /// A non-recursive array; tsymbol: `_`
76- FlatArray ( Vec < FlatElement > ) ,
7776 /// A response code
7877 RespCode ( RespCode ) ,
79- /// An array of unicode strings
80- StrArray ( Vec < Option < String > > ) ,
81- /// An array of binary strings
82- BinArray ( Vec < Option < Vec < u8 > > > ) ,
8378}
8479
8580#[ derive( Debug , PartialEq ) ]
81+ #[ non_exhaustive]
8682/// # Parser Errors
8783///
8884/// Several errors can arise during parsing and this enum accounts for them
@@ -375,16 +371,16 @@ impl<'a> Parser<'a> {
375371 b'?' => Element :: Binstr ( self . parse_next_binstr ( ) ?) ,
376372 b'+' => Element :: Str ( self . parse_next_string ( ) ?) ,
377373 b':' => Element :: UnsignedInt ( self . parse_next_u64 ( ) ?) ,
378- b'&' => Element :: Array ( self . parse_next_array ( ) ?) ,
374+ b'&' => Element :: Array ( Array :: Recursive ( self . parse_next_array ( ) ?) ) ,
379375 b'!' => Element :: RespCode ( self . parse_next_respcode ( ) ?) ,
380376 b'@' => {
381377 // hmmm, a typed array; let's check the tsymbol
382378 if let Some ( array_type) = self . buffer . get ( self . cursor ) {
383379 // got tsymbol, let's skip it
384380 self . incr_cursor ( ) ;
385381 match array_type {
386- b'+' => Element :: StrArray ( self . parse_next_typed_array_str ( ) ?) ,
387- b'?' => Element :: BinArray ( self . parse_next_typed_array_bin ( ) ?) ,
382+ b'+' => Element :: Array ( Array :: Str ( self . parse_next_typed_array_str ( ) ?) ) ,
383+ b'?' => Element :: Array ( Array :: Bin ( self . parse_next_typed_array_bin ( ) ?) ) ,
388384 _ => return Err ( ParseError :: UnknownDatatype ) ,
389385 }
390386 } else {
@@ -393,7 +389,7 @@ impl<'a> Parser<'a> {
393389 return Err ( ParseError :: NotEnough ) ;
394390 }
395391 }
396- b'_' => Element :: FlatArray ( self . parse_next_flat_array ( ) ?) ,
392+ b'_' => Element :: Array ( Array :: Flat ( self . parse_next_flat_array ( ) ?) ) ,
397393 _ => return Err ( ParseError :: UnknownDatatype ) ,
398394 } ;
399395 Ok ( ret)
@@ -537,11 +533,11 @@ fn test_typed_str_array() {
537533 assert_eq ! ( forward, typed_array_packet. len( ) ) ;
538534 assert_eq ! (
539535 parsed,
540- RawResponse :: SimpleQuery ( Element :: StrArray ( vec![
536+ RawResponse :: SimpleQuery ( Element :: Array ( Array :: Str ( vec![
541537 Some ( "the" . to_owned( ) ) ,
542538 Some ( "cat" . to_owned( ) ) ,
543539 Some ( "meowed" . to_owned( ) )
544- ] ) )
540+ ] ) ) )
545541 ) ;
546542}
547543
@@ -552,11 +548,11 @@ fn test_typed_bin_array() {
552548 assert_eq ! ( forward, typed_array_packet. len( ) ) ;
553549 assert_eq ! (
554550 parsed,
555- RawResponse :: SimpleQuery ( Element :: BinArray ( vec![
551+ RawResponse :: SimpleQuery ( Element :: Array ( Array :: Bin ( vec![
556552 Some ( Vec :: from( "the" ) ) ,
557553 Some ( Vec :: from( "cat" ) ) ,
558554 Some ( Vec :: from( "meowed" ) )
559- ] ) )
555+ ] ) ) )
560556 ) ;
561557}
562558
@@ -567,11 +563,11 @@ fn test_typed_bin_array_null() {
567563 assert_eq ! ( forward, typed_array_packet. len( ) ) ;
568564 assert_eq ! (
569565 parsed,
570- RawResponse :: SimpleQuery ( Element :: BinArray ( vec![
566+ RawResponse :: SimpleQuery ( Element :: Array ( Array :: Bin ( vec![
571567 Some ( Vec :: from( "the" ) ) ,
572568 Some ( Vec :: from( "cat" ) ) ,
573569 None
574- ] ) )
570+ ] ) ) )
575571 ) ;
576572}
577573
@@ -582,10 +578,10 @@ fn test_typed_str_array_null() {
582578 assert_eq ! ( forward, typed_array_packet. len( ) ) ;
583579 assert_eq ! (
584580 parsed,
585- RawResponse :: SimpleQuery ( Element :: StrArray ( vec![
581+ RawResponse :: SimpleQuery ( Element :: Array ( Array :: Str ( vec![
586582 Some ( "the" . to_owned( ) ) ,
587583 Some ( "cat" . to_owned( ) ) ,
588584 None
589- ] ) )
585+ ] ) ) )
590586 ) ;
591587}
0 commit comments