@@ -3,10 +3,12 @@ use std::ops::Deref;
33use bytes:: { Buf , BytesMut } ;
44
55use super :: {
6- calc_total_length, extract_fixed_data , parse_length, RespDecode , RespEncode , RespError ,
7- RespFrame , BUF_CAP , CRLF_LEN ,
6+ calc_total_length, parse_length, RespDecode , RespEncode , RespError , RespFrame , BUF_CAP ,
7+ CRLF_LEN ,
88} ;
99
10+ const NULL_ARRAY : & [ u8 ] = b"*-1\r \n " ;
11+
1012#[ derive( Debug , Clone , PartialEq ) ]
1113pub struct RespArray ( pub ( crate ) Vec < RespFrame > ) ;
1214
@@ -22,6 +24,9 @@ impl RespArray {
2224// - array: "*<number-of-elements>\r\n<element-1>...<element-n>"
2325impl RespEncode for RespArray {
2426 fn encode ( self ) -> Vec < u8 > {
27+ if self . is_empty ( ) {
28+ return NULL_ARRAY . to_vec ( ) ;
29+ }
2530 let mut buf = Vec :: with_capacity ( BUF_CAP ) ;
2631 buf. extend_from_slice ( & format ! ( "*{}\r \n " , self . len( ) ) . into_bytes ( ) ) ;
2732 for frame in self . 0 {
@@ -35,6 +40,11 @@ impl RespEncode for RespArray {
3540impl RespDecode for RespArray {
3641 const PREFIX : & ' static str = "*" ;
3742 fn decode ( buf : & mut BytesMut ) -> Result < Self , RespError > {
43+ if buf. starts_with ( NULL_ARRAY ) {
44+ buf. advance ( NULL_ARRAY . len ( ) ) ;
45+ return Ok ( RespArray :: new ( vec ! [ ] ) ) ;
46+ }
47+
3848 let ( end, len) = parse_length ( buf, Self :: PREFIX ) ?;
3949 let total_len = calc_total_length ( buf, end, len, Self :: PREFIX ) ?;
4050
@@ -58,24 +68,25 @@ impl RespDecode for RespArray {
5868 }
5969}
6070
71+ // NOTE: refactor -> delete RespNullArray, add NULL_ARRAY
6172// - null array: "*-1\r\n"
62- impl RespEncode for RespNullArray {
63- fn encode ( self ) -> Vec < u8 > {
64- b"*-1\r \n " . to_vec ( )
65- }
66- }
67-
68- impl RespDecode for RespNullArray {
69- const PREFIX : & ' static str = "*" ;
70- fn decode ( buf : & mut BytesMut ) -> Result < Self , RespError > {
71- extract_fixed_data ( buf, "*-1\r \n " , "NullArray" ) ?;
72- Ok ( RespNullArray )
73- }
74-
75- fn expect_length ( _buf : & [ u8 ] ) -> Result < usize , RespError > {
76- Ok ( 4 )
77- }
78- }
73+ // impl RespEncode for RespNullArray {
74+ // fn encode(self) -> Vec<u8> {
75+ // b"*-1\r\n".to_vec()
76+ // }
77+ // }
78+
79+ // impl RespDecode for RespNullArray {
80+ // const PREFIX: &'static str = "*";
81+ // fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
82+ // extract_fixed_data(buf, "*-1\r\n", "NullArray")?;
83+ // Ok(RespNullArray)
84+ // }
85+
86+ // fn expect_length(_buf: &[u8]) -> Result<usize, RespError> {
87+ // Ok(4)
88+ // }
89+ // }
7990
8091impl Deref for RespArray {
8192 type Target = Vec < RespFrame > ;
@@ -107,7 +118,8 @@ mod tests {
107118
108119 #[ test]
109120 fn test_null_array_encode ( ) {
110- let frame: RespFrame = RespNullArray . into ( ) ;
121+ // let frame: RespFrame = RespNullArray.into();
122+ let frame: RespFrame = RespArray :: new ( vec ! [ ] ) . into ( ) ;
111123 assert_eq ! ( frame. encode( ) , b"*-1\r \n " ) ;
112124 }
113125
@@ -116,8 +128,9 @@ mod tests {
116128 let mut buf = BytesMut :: new ( ) ;
117129 buf. extend_from_slice ( b"*-1\r \n " ) ;
118130
119- let frame = RespNullArray :: decode ( & mut buf) ?;
120- assert_eq ! ( frame, RespNullArray ) ;
131+ // let frame = RespNullArray::decode(&mut buf)?;
132+ let frame = RespArray :: decode ( & mut buf) ?;
133+ assert_eq ! ( frame, RespArray :: new( vec![ ] ) ) ;
121134
122135 Ok ( ( ) )
123136 }
0 commit comments