Skip to content

Commit 6f253bc

Browse files
committed
HeaderType: add countForFetch
* rename payloadSize to countPayload
1 parent e1425da commit 6f253bc

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

src/Unpack.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//! - `Unpack.peek` begins the unpack process for a value.
99
//! - The function returns a `HeaderType`.
10-
//! - Use `HeaderType.countData` to get the required data size
10+
//! - Use `HeaderType.count` or `HeaderType.countForFetch` to get the required data size
1111
//! for the header.
1212
//! - Ensured the `Unpack.rest` have enough data, use `Unpack.next` move to
1313
//! the next value and extract the header.
@@ -35,7 +35,7 @@
3535
//! const unpack: Unpack = Unpack.init(data);
3636
//!
3737
//! if (unpack.peek()) |peek| {
38-
//! const requiredSize = peek.count();
38+
//! const requiredSize = peek.countForFetch();
3939
//! if (requiredSize > unpack.rest.len) {
4040
//! const ndata = readMore(data);
4141
//! unpack.setAppend(data.len, ndata);

src/root.zig

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

764772
pub const Header = struct {

0 commit comments

Comments
 (0)