guile-toml/README.org

104 lines
2.9 KiB
Org Mode
Raw Permalink Normal View History

2022-12-30 16:53:13 +00:00
* guile-toml
2022-12-30 17:11:15 +00:00
2022-12-30 18:02:25 +00:00
TOML for Guile; v1.0.0 compliant.
2022-12-30 16:53:13 +00:00
** Installation
2022-12-30 17:11:15 +00:00
2023-01-03 23:01:37 +00:00
For now: add this folder to your ~GUILE_LOAD_PATH~.
2022-12-30 16:53:13 +00:00
2022-12-30 16:53:13 +00:00
** Usage
2022-12-30 16:53:13 +00:00
2022-12-30 17:11:15 +00:00
When parsing and building TOML documents, guile-toml follows [[https://github.com/aconchillo/guile-json][guile-json]] as close as possible. TOML types correspond to Guile types according to the following table:
2022-12-30 16:53:13 +00:00
2023-01-03 23:01:37 +00:00
| TOML | Guile |
|----------------+---------------------|
| string | string [0] |
2023-01-03 23:01:37 +00:00
| key-value pair | alist |
| array | vector |
| integer/float | real |
| true | #t |
| false | #f |
| datetime | SRFI-19 date [1] |
2023-01-03 23:01:37 +00:00
| nan | +nan.0 |
| ±inf | ±inf.0 |
To start using guile-toml:
#+begin_src scheme
(use-modules (toml))
#+end_src
2022-12-30 16:53:13 +00:00
[0]: TOML's default behaviour for invalid UTF-8 is to fail, whereas [[https://www.gnu.org/software/guile/manual/html_node/Encoding.html][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)~.
2022-12-30 16:53:13 +00:00
[1]: TOML's ~time-local~ is parsed same as a ~datetime-local~ on the date of ~1970-01-01~.
2022-12-30 16:53:13 +00:00
*** Reading TOML documents
2022-12-30 16:53:13 +00:00
2022-12-30 16:53:13 +00:00
- ~(toml->scm str)~
2022-12-30 16:53:13 +00:00
2022-12-30 17:54:07 +00:00
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.
2022-12-30 16:53:13 +00:00
2022-12-30 16:53:13 +00:00
*** Building TOML documents
- ~(scm->toml native #:optional port)~
2022-12-30 16:53:13 +00:00
2022-12-30 17:11:15 +00:00
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.
2022-12-30 16:53:13 +00:00
** Examples
2022-12-30 16:53:13 +00:00
- Basic ~toml->scm~ usage:
2022-12-30 16:53:13 +00:00
#+begin_src scheme
> (toml->scm "[a]\n b.c = \"hi world\"")
2022-12-30 17:11:15 +00:00
2022-12-30 16:53:13 +00:00
(("a" ("b" ("c" . "hi world"))))
#+end_src
2022-12-30 16:53:13 +00:00
- Read a TOML file and parse it (example from [[https://toml.io][toml.io]]):
2022-12-30 17:15:14 +00:00
#+begin_src scheme
2022-12-30 16:53:13 +00:00
> (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"))
#+end_src
2023-01-03 22:56:10 +00:00
~example.toml~:
2023-01-03 22:56:10 +00:00
#+begin_src 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"
#+end_src