forked from greghendershott/toml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoml-test.rkt
executable file
·40 lines (37 loc) · 1.33 KB
/
toml-test.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env racket
#lang racket/base
(require json
racket/date
racket/format
racket/match
racket/port
racket/list
"main.rkt")
(module+ main
(displayln
(jsexpr->string
(type-jsexpr
(parse-toml
(port->string
(current-input-port)))))))
(define (type-jsexpr v)
(match v
[#t (hasheq 'type "bool" 'value "true")]
[#f (hasheq 'type "bool" 'value "false")]
[(? string? v) (hasheq 'type "string" 'value v)]
[(? exact-integer? v) (hasheq 'type "integer" 'value (~a v))]
[(? real? v) (hasheq 'type "float" 'value (~a v))]
[(? date? d) (hasheq 'type "datetime"
'value
(parameterize ([date-display-format 'iso-8601])
(string-append (date->string d #t) "Z")))]
[(? list? xs) (cond [(and (not (empty? xs)) ;an array of tables?
(hash? (first xs)))
(for/list ([x xs])
(type-jsexpr x))]
[else
(hasheq 'type "array" ;a table value
'value (for/list ([x xs])
(type-jsexpr x)))])]
[(? hash? ht) (for/hasheq ([(k v) (in-hash ht)])
(values k (type-jsexpr v)))]))