diff options
author | Arun Isaac | 2023-11-16 15:34:27 +0000 |
---|---|---|
committer | Arun Isaac | 2023-11-16 15:39:33 +0000 |
commit | 11f2532db434ede7bb725e62548fef7e6f6e1343 (patch) | |
tree | 9e160a121e9309d93c6b401c18aea8fdf70c4b6c | |
parent | 900c76a29726778a34ba0cbeb832cddd618783f4 (diff) | |
download | ccwl-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.
-rw-r--r-- | ccwl/yaml.scm | 22 | ||||
-rw-r--r-- | tests/yaml.scm | 11 |
2 files changed, 28 insertions, 5 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) diff --git a/tests/yaml.scm b/tests/yaml.scm index 545b49c..d3d01c2 100644 --- a/tests/yaml.scm +++ b/tests/yaml.scm @@ -31,12 +31,19 @@ bar: {} (test-equal "strings with hyphen characters should be escaped" "- \"-1\" +- \"-2\" " - (scm->yaml-string #("-1"))) + (scm->yaml-string #("-1" "-2"))) (test-equal "strings with asterisk characters should be escaped" "- \"*foo\" +- \"*bar\" " - (scm->yaml-string #("*foo"))) + (scm->yaml-string #("*foo" "*bar"))) + +(test-equal "single element vectors must be serialized on the same line" + "[foo] +" + (scm->yaml-string #("foo"))) (test-end "yaml") |