Zig TOML (v1.0.0) parser
Find a file
2025-12-31 21:19:46 +02:00
.github/workflows Update setip-zig action in CI 2025-08-31 11:18:54 +03:00
examples Fix empty optional string on ReleaseSafe 2025-07-27 14:48:03 +02:00
src Fix build with latest zig 2025-12-31 21:19:46 +02:00
test Implement union parsing 2025-11-23 16:16:38 +01:00
.gitignore Merge branch 'main' into hotfix/optionals-releasefast 2024-10-06 16:54:00 +02:00
build.zig Fix build for zig-0.15 2025-07-13 22:50:40 +02:00
build.zig.zon Initial zig-0.15 migration 2025-08-23 10:28:36 +03:00
LICENSE Add license 2023-01-09 21:51:27 +01:00
README.md Add docs 2025-06-22 11:11:40 +02:00

zig-toml

Zig Docs

Zig TOML v1.0.0 parser.

This is a top-down LL parser that parses directly into Zig structs.

Features

  • TOML Syntax
    • Integers, hexadecimal, octal, and binary numbers
    • Floats
    • Booleans
    • Comments
    • Arrays
    • Tables
    • Array of Tables
    • Inline Table
    • Single-line strings
    • String escapes (also unicode)
    • Multi-line strings
    • Multi-line string leading space trimming
    • Trailing backslash in multi-line strings
    • Date, time, date-time, time offset
  • Struct mapping
    • Mapping to structs
    • Mapping to enums
    • Mapping to slices
    • Mapping to arrays
    • Mapping to pointers
    • Mapping to integer and floats with lower bit number than defined by TOML, i.e. i16, f32.
    • Mapping to optional fields
    • Mapping to HashMaps
  • Serialization
    • Basic types like integers, floating points, strings, booleans etc.
    • Arrays
    • Top level tables
    • Sub tables
    • Pointers
    • Date, time, DateTime, time offset
    • Enums
    • Unions

Using with the Zig package manager

Add zig-toml to your build.zig.zon

# For zig-master
zig fetch --save git+https://github.com/sam701/zig-toml

# For zig 0.13
zig fetch --save git+https://github.com/sam701/zig-toml#last-zig-0.13

Example

See example1.zig for the complete code that parses example.toml

Run it with zig build examples

// ....

const Address = struct {
    port: i64,
    host: []const u8,
};

const Config = struct {
    master: bool,
    expires_at: toml.DateTime,
    description: []const u8,

    local: *Address,
    peers: []const Address,
};

pub fn main() anyerror!void {
    var parser = toml.Parser(Config).init(allocator);
    defer parser.deinit();

    var result = try parser.parseFile("./examples/example1.toml");
    defer result.deinit();

    const config = result.value;
    std.debug.print("{s}\nlocal address: {s}:{}\n", .{ config.description, config.local.host, config.local.port });
    std.debug.print("peer0: {s}:{}\n", .{ config.peers[0].host, config.peers[0].port });
}

Error Handling

TODO

License

MIT