guile-toml/README.org

2.9 KiB
Raw Permalink Blame History

guile-toml

TOML for Guile; v1.0.0 compliant.

Installation

For now: add this folder to your 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 the replacement character <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"))

example.toml:

# This is a TOML document

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"