Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sam701/zig-toml
Zig TOML (v1.0.0) parser
https://github.com/sam701/zig-toml
parser toml zig
Last synced: 30 days ago
JSON representation
Zig TOML (v1.0.0) parser
- Host: GitHub
- URL: https://github.com/sam701/zig-toml
- Owner: sam701
- License: mit
- Created: 2022-12-20T21:06:03.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-05T06:37:54.000Z (3 months ago)
- Last Synced: 2024-09-06T11:38:12.769Z (3 months ago)
- Topics: parser, toml, zig
- Language: Zig
- Homepage:
- Size: 67.4 KB
- Stars: 28
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - sam701/zig-toml
- awesome-zig - zig-tomlποΈAn LL TOML parser that parses into Zig structs
README
# zig-toml
Zig [TOML v1.0.0](https://toml.io/en/v1.0.0) parser.
This is a top-down LL parser that parses directly into Zig structs.
## Features
* TOML Syntax
* [x] Integers, hexadecimal, octal, and binary numbers
* [x] Floats
* [x] Booleans
* [x] Comments
* [x] Arrays
* [x] Tables
* [x] Array of Tables
* [x] Inline Table
* [x] Single-line strings
* [x] String escapes (also unicode)
* [x] Multi-line strings
* [x] Multi-line string leading space trimming
* [x] Trailing backslash in multi-line strings
* [x] Date, time, date-time, time offset
* Struct mapping
* [x] Mapping to structs
* [x] Mapping to enums
* [x] Mapping to slices
* [ ] 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## Example
See [`example1.zig`](./examples/example1.zig) for the complete code that parses [`example.toml`](./examples/example1.toml)Run it with `zig build examples`
```zig
// ....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