@@ -721,44 +721,52 @@ pub const HeaderType = union(enum) {
721721 return self .countData () + 1 ;
722722 }
723723
724- pub const PayloadSizeInfo = struct {
724+ pub const PayloadSizeInfo = union {
725+ /// The size is known.
725726 known : u8 ,
726- is_variable : bool ,
727+ /// The value has variable size.
728+ variable : void ,
727729
728- fn init (size : u8 , varsized : bool ) PayloadSizeInfo {
729- return .{ .known = size , .is_variable = varsized };
730- }
731-
732- fn chooseReadSize (self : PayloadSizeInfo , varDefault : usize ) usize {
733- if (self .known > 0 ) {
734- return self .known ;
735- } else if (self .is_variable ) {
736- return varDefault ;
737- } else {
738- return 0 ;
739- }
730+ fn sized (size : u8 ) PayloadSizeInfo {
731+ return .{ .known = size };
740732 }
741733 };
742734
743- pub fn payloadSize (self : HeaderType ) PayloadSizeInfo {
735+ /// Return the payload size info.
736+ ///
737+ /// Size for (fix)array, (fix)map, bin, str, ext are always variable.
738+ /// Bin, str, ext have not determined byte size at this stage.
739+ /// Their byte size is available in the `Header`.
740+ pub fn countPayload (self : HeaderType ) PayloadSizeInfo {
744741 return switch (self ) {
745- .nil , .bool , .fixint = > PayloadSizeInfo .init ( 0 , false ),
746- .fixstr = > | n | PayloadSizeInfo .init ( n , false ),
747- .uint , .int = > | n | PayloadSizeInfo .init (switch (n ) {
742+ .nil , .bool , .fixint = > PayloadSizeInfo .sized ( 0 ),
743+ .fixstr = > | n | PayloadSizeInfo .sized ( n ),
744+ .uint , .int = > | n | PayloadSizeInfo .sized (switch (n ) {
748745 0 = > 1 ,
749746 1 = > 2 ,
750747 2 = > 4 ,
751748 3 = > 8 ,
752- }, false ),
753- .float = > | n | PayloadSizeInfo .init (switch (n ) {
749+ }),
750+ .float = > | n | PayloadSizeInfo .sized (switch (n ) {
754751 0 = > 4 ,
755752 1 = > 8 ,
756- }, false ),
757- .bin , .str , .ext = > PayloadSizeInfo .init (0 , true ),
758- .fixext = > | n | PayloadSizeInfo .init (n , false ),
759- .fixarray , .fixmap , .array , .map = > PayloadSizeInfo .init (0 , true ),
753+ }),
754+ .fixext = > | n | PayloadSizeInfo .sized (n ),
755+ .fixarray , .fixmap , .array , .map , .bin , .str , .ext = > .variable ,
760756 };
761757 }
758+
759+ /// Return the size for fetching the rest of the value.
760+ /// The header type itself (1 byte) is not included.
761+ ///
762+ /// If the header type has unknown byte size, the result
763+ /// is the size of the header data.
764+ pub fn countForFetch (self : HeaderType ) usize {
765+ return self .countData () + (switch (self .countPayload ()) {
766+ .variable = > 0 ,
767+ .known = > | sz | sz ,
768+ });
769+ }
762770};
763771
764772pub const Header = struct {
0 commit comments