diff --git a/README.md b/README.md index ea6833d..1160efc 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,12 @@ This is a top-down LL parser that parses directly into Zig structs. * [x] Mapping to structs * [x] Mapping to enums * [x] Mapping to slices - * [ ] Mapping to arrays + * [x] Mapping to arrays * [x] Mapping to pointers * [x] Mapping to integer and floats with lower bit number than defined by TOML, i.e. `i16`, `f32`. * [x] Mapping to optional fields * [x] Mapping to HashMaps +* [ ] Serialization ## Example See [`example1.zig`](./examples/example1.zig) for the complete code that parses [`example.toml`](./examples/example1.toml) diff --git a/src/struct_mapping.zig b/src/struct_mapping.zig index 7f0e63f..ff8881c 100644 --- a/src/struct_mapping.zig +++ b/src/struct_mapping.zig @@ -154,6 +154,17 @@ fn setValue(ctx: *Context, comptime T: type, dest: *T, value: *const Value) !voi }, } }, + .array => |tinfo| { + switch (value.*) { + .array => |b| { + if (b.items.len != tinfo.len) return error.InvalidArrayLength; + for (dest, 0..b.items.len) |*x, ix| { + try setValue(ctx, tinfo.child, x, &b.items[ix]); + } + }, + else => return error.InvalidValueType, + } + }, .@"struct" => { switch (value.*) { .table => |tab| { diff --git a/src/tests.zig b/src/tests.zig index 38b39eb..9862f71 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -49,6 +49,7 @@ test "parse into struct" { b1: bool, cc: []i64, dd: []const []const u8, + d2: [3][]const u8, t1: Tt, t2: Tt, t3: Tt, @@ -106,6 +107,10 @@ test "parse into struct" { try testing.expect(std.mem.eql(u8, aa.dd[0], "aa")); try testing.expect(std.mem.eql(u8, aa.dd[1], "bb")); + try testing.expectEqualSlices(u8, aa.d2[0], "a1"); + try testing.expectEqualSlices(u8, aa.d2[1], "a2"); + try testing.expectEqualSlices(u8, aa.d2[2], "a3"); + try testing.expect(aa.t1.aa == 3); try testing.expect(aa.t1.bb == 4); try testing.expect(aa.t2.aa == 5); diff --git a/test/doc1.toml.txt b/test/doc1.toml.txt index 447e9cf..319d427 100644 --- a/test/doc1.toml.txt +++ b/test/doc1.toml.txt @@ -4,6 +4,7 @@ aa2 = 50 cc = [3, 15, 20,] dd = ["aa", "bb"] +d2 = ["a1", "a2", "a3"] b1 = true f1 = 125.55 f1a = 99.5