A TOML parsing library for Zap, backed by tomlc17 through FFI.
| Path | Purpose |
|---|---|
tomlc17/ |
Vendored tomlc17 C library |
csrc/toml_shim.c |
Pointer-based C wrapper around tomlc17 |
build_lib.sh |
Compiles tomlc17 + shim into lib/libztoml.a |
src/ffi.zp |
Raw ext fun declarations for the zt_* symbols |
src/toml.zp |
High-level Zap API (Document, Value) |
src/main.zp |
Demo program |
The native static library must be built once before compiling Zap code:
./build_lib.sh # produces lib/libztoml.a
thor run # builds src/main.zp and runs the demo-Llib -lztoml links the native library.
Errors use Zap's failable functions. Parsing, lookups, and typed accessors
return T!toml.TomlError; propagate with ? or handle locally with
or <fallback> / or err { ... }.
import "std/io" { println, printInt };
import "toml";
fun load() Int!toml.TomlError {
var doc: toml.Document = toml.parseString("port = 8080")?;
var port: Int64 = doc.root().getInt("port")?;
printInt(port as Int);
doc.close(); // releases the whole parsed tree
return 0;
}
fun main() Int {
return load() or err {
println(err.message); // err is a toml.TomlError
1
};
}toml.TomlErroris an@error structwithkind: ErrorKindandmessage: String.ErrorKindisParse,NotFound,WrongType, orIndexOutOfRange.- Propagate with
?, supply a default withor <value>(e.g.v.asInt() or 30), or branch onerr.kindinsideor err { ... }. ?propagates a full expression; it does not chain mid-expression (v.get(k)?.asInt()?is not valid). Bind the intermediateValueto avarand unwrap each step, as the demo's helpers do.
A Document owns the entire parsed tree. Every Value borrows into that tree
and stays valid only until doc.close(). Always call doc.close() exactly
once per parsed Document (a parse failure frees the tree for you before
failing, so only close a Document you successfully obtained).