From 6e28a0825bd8931815d7835ff4512b34678db32c Mon Sep 17 00:00:00 2001 From: Hilton Chain Date: Fri, 7 Aug 2026 07:08:38 +0800 Subject: [PATCH] utils: Add scm->kdl-string, scm->kdlv1-string, kdl-file and kdlv1-file. --- modules/rosenthal/utils/file.scm | 30 +++++++++++ modules/rosenthal/utils/kdl.scm | 86 ++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 modules/rosenthal/utils/kdl.scm diff --git a/modules/rosenthal/utils/file.scm b/modules/rosenthal/utils/file.scm index 2b8a424..19d789a 100644 --- a/modules/rosenthal/utils/file.scm +++ b/modules/rosenthal/utils/file.scm @@ -16,6 +16,8 @@ ini-file json-file + kdl-file + kdlv1-file toml-file yaml-file)) @@ -94,6 +96,34 @@ (cut scm->json '#$exp <> #:pretty #t))))) #:options '(#:substitutable? #f))) +(define (kdl-file name exp) + (computed-file name + (with-imported-modules '((rosenthal utils kdl)) + (if (gexp? exp) + #~(begin + (use-modules (srfi srfi-26) (rosenthal utils kdl)) + (call-with-output-file #$output + (cut display (scm->kdl-string #$exp) <>))) + #~(begin + (use-modules (srfi srfi-26) (rosenthal utils kdl)) + (call-with-output-file #$output + (cut display (scm->kdl-string '#$exp) <>))))) + #:options '(#:substitutable? #f))) + +(define (kdlv1-file name exp) + (computed-file name + (with-imported-modules '((rosenthal utils kdl)) + (if (gexp? exp) + #~(begin + (use-modules (srfi srfi-26) (rosenthal utils kdl)) + (call-with-output-file #$output + (cut display (scm->kdlv1-string #$exp) <>))) + #~(begin + (use-modules (srfi srfi-26) (rosenthal utils kdl)) + (call-with-output-file #$output + (cut display (scm->kdlv1-string '#$exp) <>))))) + #:options '(#:substitutable? #f))) + ;; https://github.com/hylophile/guile-toml ;; TODO: TOML writing support is incomplete. ;; See https://github.com/hylophile/guile-toml/blob/main/toml/builder.scm. diff --git a/modules/rosenthal/utils/kdl.scm b/modules/rosenthal/utils/kdl.scm new file mode 100644 index 0000000..c4f4313 --- /dev/null +++ b/modules/rosenthal/utils/kdl.scm @@ -0,0 +1,86 @@ +;;; SPDX-License-Identifier: GPL-3.0-or-later +;;; Copyright © 2026 Hilton Chain + +(define-module (rosenthal utils kdl) + #:use-module (ice-9 format) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) + #:use-module (srfi srfi-71) + #:export (scm->kdl-string + scm->kdlv1-string)) + +;; https://kdl.dev/spec/ + +(define %kdl-v1? + (make-parameter #f)) + +(define* (value->kdl value #:optional (kdlv1? (%kdl-v1?))) + (match value + ;; Null + ('null + (if kdlv1? "null" "#null")) + ;; Boolean + ((or #t 'true) + (if kdlv1? "true" "#true")) + ((or #f 'false) + (if kdlv1? "false" "#false")) + ;; Number + ((or +inf.0 'inf) + "#inf") + ((or -inf.0 '-inf) + "#-inf") + ((or +nan.0 'nan) + "#nan") + ((? number?) + (number->string value)) + ;; String + ((? symbol?) + (symbol->string value)) + ((? string?) + (format #f "~s" value)) + ;; Property. Added here for convenience. + (_ + (string-join + (map (lambda (x) + (format #f "~a=~a" + (value->kdl (first x)) + (value->kdl (second x)))) + (cdr value)))))) + +(define (child-node? x) + (and (pair? x) + (not (eq? (car x) '@)))) + +(define (node->kdl node level) + (let* ((name (car node)) + (children args (partition child-node? (cdr node))) + (indent (make-string (* level 8) #\space)) + (output + (string-join + (cons (string-append indent (value->kdl name)) + (map value->kdl args))))) + (if (null? children) + output + (format #f "~a {~%~{~a~%~}~a}" + output + (map (cut node->kdl <> (1+ level)) children) + indent)))) + +(define (document->kdl document) + (map (cut node->kdl <> 0) document)) + +(define (scm->kdl-string scm) + (cond + ((null? scm) + "") + ((not (pair? scm)) + (value->kdl scm)) + ((eq? (car scm) '*TOP*) + (scm->kdl-string (cdr scm))) + (else + (string-join (document->kdl scm) "\n")))) + +(define (scm->kdlv1-string scm) + (parameterize ((%kdl-v1? #t)) + (scm->kdl-string scm)))