Skip to content

Repository files navigation

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

// ....constAddress=struct {
port: i64,
host: []constu8,
};
constConfig=struct {
master: bool,
expires_at: toml.DateTime,
description: []constu8,
local: *Address,
peers: []constAddress,
};
pubfnmain() anyerror!void {
varparser=toml.Parser(Config).init(allocator);
deferparser.deinit();
varresult=tryparser.parseFile("./examples/example1.toml");
deferresult.deinit();
constconfig=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

About

Zig TOML (v1.0.0) parser

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages