aboutsummaryrefslogtreecommitdiff
path: root/ccwl
diff options
context:
space:
mode:
authorArun Isaac2023-11-16 15:34:27 +0000
committerArun Isaac2023-11-16 15:39:33 +0000
commit11f2532db434ede7bb725e62548fef7e6f6e1343 (patch)
tree9e160a121e9309d93c6b401c18aea8fdf70c4b6c /ccwl
parent900c76a29726778a34ba0cbeb832cddd618783f4 (diff)
downloadccwl-11f2532db434ede7bb725e62548fef7e6f6e1343.tar.gz
ccwl-11f2532db434ede7bb725e62548fef7e6f6e1343.tar.lz
ccwl-11f2532db434ede7bb725e62548fef7e6f6e1343.zip
yaml: Display arrays with a single atomic element on the same line.
* ccwl/yaml.scm (atom?): New function. (display-dictionary-entry, scm->yaml): Display arrays with a single atomic element on the same line. * tests/yaml.scm ("strings with hyphen characters should be escaped", "strings with asterisk characters should be escaped"): Update tests with new behaviour. ("single element vectors must be serialized on the same line"): New test.
Diffstat (limited to 'ccwl')
-rw-r--r--ccwl/yaml.scm22
1 files changed, 19 insertions, 3 deletions
diff --git a/ccwl/yaml.scm b/ccwl/yaml.scm
index 8a2b7a5..07ed913 100644
--- a/ccwl/yaml.scm
+++ b/ccwl/yaml.scm
@@ -34,6 +34,14 @@
#:export (scm->yaml
scm->yaml-string))
+(define (atom? x)
+ "Return @code{#t} if @var{x} is a primitive element that can be
+serialized to YAML. Else, return @code{#f}."
+ (or (symbol? x)
+ (boolean? x)
+ (number? x)
+ (string? x)))
+
(define (display-atom atom port)
"Display ATOM in PORT."
(cond
@@ -68,9 +76,11 @@
(display-atom key port)
(display ":" port)
(match value
- ;; If value is an empty array or dictionary, display it on the
- ;; same line.
- ((or #() ())
+ ;; Display on the same line if value is
+ ;; - an empty array
+ ;; - an empty dictionary
+ ;; - an array with an atom as its only element
+ ((or #() () #((? atom? _)))
(display " " port)
(scm->yaml value port level))
;; Else, display it on the next line.
@@ -83,6 +93,12 @@
"Convert SCM, an S-expression tree, to YAML and display to
PORT. LEVEL is an internal recursion variable."
(match scm
+ ;; Display arrays with a single atomic element on the same line.
+ (#((? atom? single-element))
+ (display "[" port)
+ (display-atom single-element port)
+ (display "]" port)
+ (newline port))
(#(head tail ...)
(display-array-element head port level)
(for-each (lambda (element)