Skip to content

Commit

Permalink
Merge pull request #18 from optimism-java/ssz
Browse files Browse the repository at this point in the history
feat: add support for nested fixed size struct in ssz encode and decode
  • Loading branch information
GrapeBaBa authored Nov 4, 2024
2 parents e7fbf03 + 214b237 commit d912992
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/ssz/ssz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,16 @@ pub fn decodeSSZ(comptime T: type, serialized: []const u8) SSZDecodeErrors!T {
inline for (struct_info.fields) |field| {
switch (@typeInfo(field.type)) {
.bool, .int, .array => continue,
.@"struct" => {
if (isStaticType(field.type)) {
continue;
} else {
num_fields += 1;
}
},
else => num_fields += 1,
}
}

var indices: [num_fields]u32 = undefined;
var result: T = undefined;

Expand All @@ -138,6 +144,12 @@ pub fn decodeSSZ(comptime T: type, serialized: []const u8) SSZDecodeErrors!T {
@field(result, field.name) = try decodeSSZ(field.type, serialized[index .. index + @sizeOf(field.type)]);
index += @sizeOf(field.type);
},
.@"struct" => {
if (isStaticType(field.type)) {
@field(result, field.name) = try decodeSSZ(field.type, serialized[index .. index + @sizeOf(field.type)]);
index += @sizeOf(field.type);
}
},
else => {
indices[field_index] = try decodeSSZ(u32, serialized[index .. index + 4]);
index += 4;
Expand All @@ -150,6 +162,11 @@ pub fn decodeSSZ(comptime T: type, serialized: []const u8) SSZDecodeErrors!T {
inline for (struct_info.fields) |field| {
switch (@typeInfo(field.type)) {
.bool, .int, .array => continue,
.@"struct" => {
if (isStaticType(field.type)) {
continue;
}
},
else => {
const final = if (final_index == indices.len - 1) serialized.len else indices[final_index + 1];
@field(result, field.name) = try decodeSSZ(field.type, serialized[indices[final_index]..final]);
Expand Down Expand Up @@ -308,6 +325,11 @@ fn encodeItem(value: anytype, list: *std.ArrayList(u8)) Allocator.Error!void {
inline for (struct_info.fields) |field| {
switch (@typeInfo(field.type)) {
.int, .bool, .array => start += @sizeOf(field.type),
.@"struct" => {
if (isStaticType(field.type)) {
start += @sizeOf(field.type);
}
},
else => start += 4,
}
}
Expand All @@ -316,6 +338,11 @@ fn encodeItem(value: anytype, list: *std.ArrayList(u8)) Allocator.Error!void {
inline for (struct_info.fields) |field| {
switch (@typeInfo(field.type)) {
.int, .bool, .array => try encodeItem(@field(value, field.name), list),
.@"struct" => {
if (isStaticType(field.type)) {
try encodeItem(@field(value, field.name), list);
}
},
else => {
try encodeItem(@as(u32, @truncate(accumulate)), list);
accumulate += sizeOfValue(@field(value, field.name));
Expand Down Expand Up @@ -351,6 +378,19 @@ fn sizeOfValue(value: anytype) usize {
else
1 + sizeOfValue(value.?),
.null => return @intCast(0),
.@"struct" => |struct_info| {
comptime var start: usize = 0;
inline for (struct_info.fields) |field| {
switch (@typeInfo(field.type)) {
.int, .bool, .array => start += @sizeOf(field.type),
.@"struct" => {
start += sizeOfValue(field.type);
},
else => {},
}
}
return start;
},
else => @compileError("Unsupported type " ++ @typeName(@TypeOf(value))),
}
// It should never reach this
Expand All @@ -368,6 +408,7 @@ pub inline fn isStaticType(comptime T: type) bool {
if (!isStaticType(field.type)) {
return false;
}
return true;
},
.pointer => switch (info.pointer.size) {
.Many, .Slice, .C => return false,
Expand Down
4 changes: 4 additions & 0 deletions src/yaml/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ const Parser = struct {
self.token_it.seekBy(-1);
return self.list_bracketed();
},
.flow_map_start => {
self.token_it.seekTo(pos + 1);
return try self.json_format_map();
},
else => return null,
}
}
Expand Down

0 comments on commit d912992

Please sign in to comment.