Data formats

TOML

TOML to values and back: Cargo.toml, pyproject.toml and settings files.

Version 1.0.0 · 2 functions · 1 crate

#toml#config

Functions

What use toml.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use toml_parse, toml_stringify from toml (Quillo 0.6).

toml_parse(text: str) → anyor an error

TOML text as a map. Dates and times come as their text.

toml_stringify(value: any) → stror an error

A map as TOML text. TOML has no null: a field that is null is left out.

Get it

$ quillo install toml               # toml.qwer into this folder$ quillo install toml --example     # and its example, toml.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/toml.qwer$ curl -O https://quillolang.org/plugins/files/toml.qwe

Then, in your program, all of it or only what you call:

main.qwe
use toml.qweruse toml_parse, toml_stringify from toml      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
toml = "0.8"

Example

toml.qwe
// Reads a Cargo.toml-like file, and writes settings back.////   quillo run toml.qwe      then open http://localhost:8080/packageuse "toml.qwer"route GET /package {    let t = toml_parse(`[package]name = "shop"version = "1.4.0"released = 2026-03-01[dependencies]serde = { version = "1", features = ["derive"] }`)?    { name: t.package.name, version: t.package.version, released: t.package.released, deps: keys(t.dependencies) }}route GET /settings = toml_stringify({ title: "Shop", owner: { name: "Ada", email: null }, ports: [8080, 8081] })?
$ quillo run toml.qwe$ curl 'localhost:8080/package'$ curl 'localhost:8080/settings'

Source

toml.qwer
//! TOML to values and back: Cargo.toml, pyproject.toml, app settings.//! version: 1.0.0//!//! [dependencies]//! toml = "0.8"use serde_json::{Map, Value};fn json_of(v: toml::Value) -> Value {    match v {        toml::Value::String(s) => Value::String(s),        toml::Value::Integer(i) => Value::from(i),        toml::Value::Float(f) => Value::from(f),        toml::Value::Boolean(b) => Value::Bool(b),        // A date or time is its text, as TOML writes it: `1979-05-27T07:32:00Z`.        toml::Value::Datetime(d) => Value::String(d.to_string()),        toml::Value::Array(a) => Value::Array(a.into_iter().map(json_of).collect()),        toml::Value::Table(t) => Value::Object(t.into_iter().map(|(k, v)| (k, json_of(v))).collect::<Map<_, _>>()),    }}/// TOML text as a map. Dates and times come as their text.pub fn toml_parse(text: &str) -> Result<Value, String> {    text.parse::<toml::Table>().map(|t| json_of(toml::Value::Table(t))).map_err(|e| e.to_string())}/// A map as TOML text. TOML has no null: a field that is `null` is left out.pub fn toml_stringify(value: serde_json::Value) -> Result<String, String> {    fn strip(v: Value) -> Value {        match v {            Value::Object(m) => Value::Object(m.into_iter().filter(|(_, v)| !v.is_null()).map(|(k, v)| (k, strip(v))).collect()),            Value::Array(a) => Value::Array(a.into_iter().map(strip).collect()),            v => v,        }    }    if !value.is_object() {        return Err("TOML's top level is a map".into());    }    toml::to_string(&strip(value)).map_err(|e| e.to_string())}
Thread
87Useful? Like it, or add to its thread below.

Thread 0 posts

On TOML: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.