TOML module for Guile
Find a file
2023-01-03 23:35:33 +01:00
test docs: add README for how to run tests 2022-12-30 18:54:24 +01:00
toml feat(encoder): datetime 2022-12-30 18:27:42 +01:00
.gitignore chore: add gitignore 2023-01-03 23:35:33 +01:00
LICENSE chore: add license 2022-12-30 18:35:02 +01:00
README.org docs: fix version 2022-12-30 19:02:25 +01:00
toml.scm fix: export scm->toml 2022-12-30 18:11:03 +01:00

guile-toml

TOML for Guile; v1.0.0 compliant.

Installation

For now: add this folder to GUILE_LOAD_PATH

Usage

When parsing and building TOML documents, guile-toml follows guile-json as close as possible. TOML types correspond to Guile types according to the following table:

TOML Guile
string string [0]
key-value pair alist
array vector
integer/float real
true #t
false #f
datetime SRFI-19 date [1]
nan +nan.0
±inf ±inf.0

To start using guile-toml: (use-modules (toml))

[0]: TOML's default behaviour for invalid UTF-8 is to fail, whereas Guile's default behavior is to replace invalid UTF-8 with <20>. If you prefer TOML's behavior, use (set-port-conversion-strategy! (current-input-port) 'error).

[1]: TOML's time-local is parsed same as a datetime-local on the date of 1970-01-01.

Reading TOML documents

  • (toml->scm str) Reads a TOML document from the given string. Guile-toml's parsing is implemented with (ice-9 peg), which unfortunately does not support ports. See below for how to use this with a TOML file.

Building TOML documents

  • (scm->toml native #:optional port) Creates a TOML document from the given native Guile value. The TOML document is written into the given port, or to the current output port if none is given.

Examples

  • Basic toml->scm usage:
> (toml->scm "[a]\n b.c = \"hi world\"")
(("a" ("b" ("c" . "hi world"))))
  • Read a TOML file and parse it (example from toml.io):
> (use-modules (toml) (ice-9 textual-ports))
> (toml->scm (call-with-input-file "example.toml" get-string-all))
(("servers"
  ("beta" ("role" . "backend") ("ip" . "10.0.0.2"))
  ("alpha"
   ("role" . "frontend")
   ("ip" . "10.0.0.1")))
 ("database"
  ("temp_targets" ("case" . 72.0) ("cpu" . 79.5))
  ("data" . #(#("delta" "phi") #(3.14)))
  ("ports" . #(8000 8001 8002))
  ("enabled" . #t))
 ("owner"
  ("dob"
   .
   #<date nanosecond: 0 second: 0 minute: 32 hour: 7 day: 27 month: 5 year: 1979 zone-offset: -28800>)
  ("name" . "Tom Preston-Werner"))
 ("title" . "TOML Example"))