Skip to content

Commit

Permalink
Implement array mapping
Browse files Browse the repository at this point in the history
Close #15
  • Loading branch information
sam701 committed Jan 17, 2025
1 parent 88e6196 commit 561a076
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/struct_mapping.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
5 changes: 5 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/doc1.toml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 561a076

Please sign in to comment.