Skip to content

Commit d912992

Browse files
authored
Merge pull request #18 from optimism-java/ssz
feat: add support for nested fixed size struct in ssz encode and decode
2 parents e7fbf03 + 214b237 commit d912992

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/ssz/ssz.zig

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,16 @@ pub fn decodeSSZ(comptime T: type, serialized: []const u8) SSZDecodeErrors!T {
123123
inline for (struct_info.fields) |field| {
124124
switch (@typeInfo(field.type)) {
125125
.bool, .int, .array => continue,
126+
.@"struct" => {
127+
if (isStaticType(field.type)) {
128+
continue;
129+
} else {
130+
num_fields += 1;
131+
}
132+
},
126133
else => num_fields += 1,
127134
}
128135
}
129-
130136
var indices: [num_fields]u32 = undefined;
131137
var result: T = undefined;
132138

@@ -138,6 +144,12 @@ pub fn decodeSSZ(comptime T: type, serialized: []const u8) SSZDecodeErrors!T {
138144
@field(result, field.name) = try decodeSSZ(field.type, serialized[index .. index + @sizeOf(field.type)]);
139145
index += @sizeOf(field.type);
140146
},
147+
.@"struct" => {
148+
if (isStaticType(field.type)) {
149+
@field(result, field.name) = try decodeSSZ(field.type, serialized[index .. index + @sizeOf(field.type)]);
150+
index += @sizeOf(field.type);
151+
}
152+
},
141153
else => {
142154
indices[field_index] = try decodeSSZ(u32, serialized[index .. index + 4]);
143155
index += 4;
@@ -150,6 +162,11 @@ pub fn decodeSSZ(comptime T: type, serialized: []const u8) SSZDecodeErrors!T {
150162
inline for (struct_info.fields) |field| {
151163
switch (@typeInfo(field.type)) {
152164
.bool, .int, .array => continue,
165+
.@"struct" => {
166+
if (isStaticType(field.type)) {
167+
continue;
168+
}
169+
},
153170
else => {
154171
const final = if (final_index == indices.len - 1) serialized.len else indices[final_index + 1];
155172
@field(result, field.name) = try decodeSSZ(field.type, serialized[indices[final_index]..final]);
@@ -308,6 +325,11 @@ fn encodeItem(value: anytype, list: *std.ArrayList(u8)) Allocator.Error!void {
308325
inline for (struct_info.fields) |field| {
309326
switch (@typeInfo(field.type)) {
310327
.int, .bool, .array => start += @sizeOf(field.type),
328+
.@"struct" => {
329+
if (isStaticType(field.type)) {
330+
start += @sizeOf(field.type);
331+
}
332+
},
311333
else => start += 4,
312334
}
313335
}
@@ -316,6 +338,11 @@ fn encodeItem(value: anytype, list: *std.ArrayList(u8)) Allocator.Error!void {
316338
inline for (struct_info.fields) |field| {
317339
switch (@typeInfo(field.type)) {
318340
.int, .bool, .array => try encodeItem(@field(value, field.name), list),
341+
.@"struct" => {
342+
if (isStaticType(field.type)) {
343+
try encodeItem(@field(value, field.name), list);
344+
}
345+
},
319346
else => {
320347
try encodeItem(@as(u32, @truncate(accumulate)), list);
321348
accumulate += sizeOfValue(@field(value, field.name));
@@ -351,6 +378,19 @@ fn sizeOfValue(value: anytype) usize {
351378
else
352379
1 + sizeOfValue(value.?),
353380
.null => return @intCast(0),
381+
.@"struct" => |struct_info| {
382+
comptime var start: usize = 0;
383+
inline for (struct_info.fields) |field| {
384+
switch (@typeInfo(field.type)) {
385+
.int, .bool, .array => start += @sizeOf(field.type),
386+
.@"struct" => {
387+
start += sizeOfValue(field.type);
388+
},
389+
else => {},
390+
}
391+
}
392+
return start;
393+
},
354394
else => @compileError("Unsupported type " ++ @typeName(@TypeOf(value))),
355395
}
356396
// It should never reach this
@@ -368,6 +408,7 @@ pub inline fn isStaticType(comptime T: type) bool {
368408
if (!isStaticType(field.type)) {
369409
return false;
370410
}
411+
return true;
371412
},
372413
.pointer => switch (info.pointer.size) {
373414
.Many, .Slice, .C => return false,

src/yaml/parse.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ const Parser = struct {
369369
self.token_it.seekBy(-1);
370370
return self.list_bracketed();
371371
},
372+
.flow_map_start => {
373+
self.token_it.seekTo(pos + 1);
374+
return try self.json_format_map();
375+
},
372376
else => return null,
373377
}
374378
}

0 commit comments

Comments
 (0)