/chez-toml

A TOML library for chez scheme

Primary LanguageSchemeGNU General Public License v3.0GPL-3.0

Chez-Toml λ-0.1

Introduction

[中文版本]

TOML in chez-scheme; v1.0.0 compliant (TOML v1.0.0 repository)

  • Compliant with TOML v1.0.0.
  • Parser: parse TOML text stream into scheme association list.
  • Writer: parse toml-data in hashtable or alist form into TOML text stream.
  • Convert toml association list to chez builtin object (hash-table, vector and date)
  • Useful API for reference, set and display toml-data

Installation

(Chez Scheme (v9.5.8) is required)

chez-toml is a single-file library, ./src/toml.sls is the source code, ./src/toml.so is its compiled file. Each of them is enough to use chez-toml.

Basic steps

By following steps, you can easily run the test case.

git clone https://github.com/Yunoinsky/chez-toml # clone this repository
cd chez-toml
scheme --script ./test/test.ss

Global installation

By copying toml.sls or toml.so to the corresponding project, we can use chez-toml in other projects. You can also choose to install it globally. Here is one way to do this.

  1. Create lib directory: ~~/Library/chezscheme/~.
  2. Copy toml.so or toml.sls to this directory.
  3. append ~~/Library/chezscheme/~ to the environment variable CHEZSCHEMELIBDIRS.
  4. Then, we can use (import (toml)) to import this library directly.

Basic Usage

Take the following toml file as an example.

# ./test/school.toml

[student]
[student.LiHua]
name = "李华 Li Hua"
birthday = 1997-05-16
grade = 2

[[student.LiHua.courses]]

name = "Calculus"
teacher-id = "200701021"
score = 3

[[student.LiHua.courses]]

name = "Linear Algebra"
teacher-id = "201303011"
score = 4

Load and import library

(load "./src/toml.so") ;; this line can be commented 
                       ;; out if installed globally
(import (toml))

TOML Reader

(toml-load text-port)
Load toml-data from a text-port, return an assoc list.
(toml-display data)
Formatively display loaded toml-data.
(define fp (open-input-file "./test/school.toml"))
(define data (toml-load fp))
(toml-display data)

Convert to Chez Scheme built-in types

(to-builtin data)
Convert assoc list toml-data to built-in types.
(define data-ht (to-builtin data))
(toml-display data-ht)

Index and assign

(toml-ref data path)
Index toml-data by key path.
(toml-set! data path value)
Set tom-data by key path
(pretty-print
 (toml-ref data
           '(student LiHua birthday)))
(toml-set! data
           '(student LiHua grade)
           3)
(toml-display data)

TOML Writer

(toml-dump data text-port)
Output toml data to text port in TOML format.
(toml-dump data
           (current-output-port))