@@ -84,19 +84,6 @@ pub enum IProtoType {
8484 Error = 1 << 15 ,
8585}
8686
87- pub fn encode_header (
88- stream : & mut impl Write ,
89- sync : SyncIndex ,
90- request_type : IProtoType ,
91- ) -> Result < ( ) , Error > {
92- rmp:: encode:: write_map_len ( stream, 2 ) ?;
93- rmp:: encode:: write_pfix ( stream, REQUEST_TYPE ) ?;
94- rmp:: encode:: write_pfix ( stream, request_type as u8 ) ?;
95- rmp:: encode:: write_pfix ( stream, SYNC ) ?;
96- rmp:: encode:: write_uint ( stream, sync. 0 ) ?;
97- Ok ( ( ) )
98- }
99-
10087pub fn chap_sha1_auth_data ( password : & str , salt : & [ u8 ] ) -> Vec < u8 > {
10188 // prepare 'chap-sha1' scramble:
10289 // salt = base64_decode(encoded_salt);
@@ -389,48 +376,72 @@ pub struct Header {
389376 pub schema_version : u64 ,
390377}
391378
392- pub struct Response < T > {
393- pub header : Header ,
394- pub payload : T ,
395- }
396-
397- pub fn decode_header ( stream : & mut ( impl Read + Seek ) ) -> Result < Header , Error > {
398- let mut sync: Option < u64 > = None ;
399- let mut iproto_type: Option < u32 > = None ;
400- let mut error_code: u32 = 0 ;
401- let mut schema_version: Option < u64 > = None ;
379+ impl Header {
380+ pub fn encode ( & self , stream : & mut impl Write ) -> Result < ( ) , Error > {
381+ rmp:: encode:: write_map_len ( stream, 2 ) ?;
382+ rmp:: encode:: write_pfix ( stream, REQUEST_TYPE ) ?;
383+ rmp:: encode:: write_pfix ( stream, self . iproto_type as u8 ) ?;
384+ rmp:: encode:: write_pfix ( stream, SYNC ) ?;
385+ rmp:: encode:: write_uint ( stream, self . sync . 0 ) ?;
386+ Ok ( ( ) )
387+ }
402388
403- let map_len = rmp:: decode:: read_map_len ( stream) ?;
404- for _ in 0 ..map_len {
405- let key = rmp:: decode:: read_pfix ( stream) ?;
406- match key {
407- REQUEST_TYPE => {
408- let r#type: u32 = rmp:: decode:: read_int ( stream) ?;
389+ pub fn encode_from_parts (
390+ stream : & mut impl Write ,
391+ sync : SyncIndex ,
392+ request_type : IProtoType ,
393+ ) -> Result < ( ) , Error > {
394+ rmp:: encode:: write_map_len ( stream, 2 ) ?;
395+ rmp:: encode:: write_pfix ( stream, REQUEST_TYPE ) ?;
396+ rmp:: encode:: write_pfix ( stream, request_type as u8 ) ?;
397+ rmp:: encode:: write_pfix ( stream, SYNC ) ?;
398+ rmp:: encode:: write_uint ( stream, sync. 0 ) ?;
399+ Ok ( ( ) )
400+ }
409401
410- const IPROTO_TYPE_ERROR : u32 = IProtoType :: Error as _ ;
411- if ( r#type & IPROTO_TYPE_ERROR ) != 0 {
412- iproto_type = Some ( IPROTO_TYPE_ERROR ) ;
413- error_code = r#type & !IPROTO_TYPE_ERROR ;
414- } else {
415- iproto_type = Some ( r#type) ;
402+ pub fn decode ( stream : & mut ( impl Read + Seek ) ) -> Result < Header , Error > {
403+ let mut sync: Option < u64 > = None ;
404+ let mut iproto_type: Option < u32 > = None ;
405+ let mut error_code: u32 = 0 ;
406+ let mut schema_version: Option < u64 > = None ;
407+
408+ let map_len = rmp:: decode:: read_map_len ( stream) ?;
409+ for _ in 0 ..map_len {
410+ let key = rmp:: decode:: read_pfix ( stream) ?;
411+ match key {
412+ REQUEST_TYPE => {
413+ let r#type: u32 = rmp:: decode:: read_int ( stream) ?;
414+
415+ const IPROTO_TYPE_ERROR : u32 = IProtoType :: Error as _ ;
416+ if ( r#type & IPROTO_TYPE_ERROR ) != 0 {
417+ iproto_type = Some ( IPROTO_TYPE_ERROR ) ;
418+ error_code = r#type & !IPROTO_TYPE_ERROR ;
419+ } else {
420+ iproto_type = Some ( r#type) ;
421+ }
416422 }
423+ SYNC => sync = Some ( rmp:: decode:: read_int ( stream) ?) ,
424+ SCHEMA_VERSION => schema_version = Some ( rmp:: decode:: read_int ( stream) ?) ,
425+ _ => msgpack:: skip_value ( stream) ?,
417426 }
418- SYNC => sync = Some ( rmp:: decode:: read_int ( stream) ?) ,
419- SCHEMA_VERSION => schema_version = Some ( rmp:: decode:: read_int ( stream) ?) ,
420- _ => msgpack:: skip_value ( stream) ?,
421427 }
422- }
423428
424- if sync. is_none ( ) || iproto_type. is_none ( ) || schema_version. is_none ( ) {
425- return Err ( io:: Error :: from ( io:: ErrorKind :: InvalidData ) . into ( ) ) ;
429+ if sync. is_none ( ) || iproto_type. is_none ( ) || schema_version. is_none ( ) {
430+ return Err ( io:: Error :: from ( io:: ErrorKind :: InvalidData ) . into ( ) ) ;
431+ }
432+
433+ Ok ( Header {
434+ sync : SyncIndex ( sync. unwrap ( ) ) ,
435+ iproto_type : iproto_type. unwrap ( ) ,
436+ error_code,
437+ schema_version : schema_version. unwrap ( ) ,
438+ } )
426439 }
440+ }
427441
428- Ok ( Header {
429- sync : SyncIndex ( sync. unwrap ( ) ) ,
430- iproto_type : iproto_type. unwrap ( ) ,
431- error_code,
432- schema_version : schema_version. unwrap ( ) ,
433- } )
442+ pub struct Response < T > {
443+ pub header : Header ,
444+ pub payload : T ,
434445}
435446
436447////////////////////////////////////////////////////////////////////////////////
0 commit comments