branch: elpa/parseedn
commit 6d80a596730095a58a54cd9e4a7b841bc04ba670
Author: Jack Rusher <[email protected]>
Commit: Jack Rusher <[email protected]>
Add support for alist, plist and an explicit error for unsupported dotted
pair notation.
---
parseedn.el | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
test/parseedn-test.el | 4 ++++
2 files changed, 48 insertions(+), 5 deletions(-)
diff --git a/parseedn.el b/parseedn.el
index e969d23d42..74b83e7083 100644
--- a/parseedn.el
+++ b/parseedn.el
@@ -141,8 +141,8 @@ TAG-READERS is an optional association list. For more
information, see
(insert " ")
(parseedn-print-seq next))))
-(defun parseedn-print-kvs (map &optional ks)
- "Insert hash table MAP as an EDN map into the current buffer."
+(defun parseedn-print-hash-or-alist (map &optional ks)
+ "Insert hash table MAP or elisp a-list as an EDN map into the current
buffer."
(let ((keys (or ks (a-keys map))))
(parseedn-print (car keys))
(insert " ")
@@ -152,6 +152,34 @@ TAG-READERS is an optional association list. For more
information, see
(insert ", ")
(parseedn-print-kvs map next)))))
+(defun parseedn-print-plist (plist)
+ "Insert an elisp property list as an EDN map into the current buffer."
+ (parseedn-print (car plist))
+ (insert " ")
+ (parseedn-print (cadr plist))
+ (let ((next (cddr plist)))
+ (when (not (seq-empty-p next))
+ (insert ", ")
+ (parseedn-print-plist next))))
+
+(defun parseedn-alist-p (list)
+ "Non-null if and only if LIST is an alist with simple keys."
+ (while (consp list)
+ (setq list (if (and (consp (car list))
+ (atom (caar list)))
+ (cdr list)
+ 'not-alist)))
+ (null list))
+
+(defun parseedn-plist-p (list)
+ "Non-null if and only if LIST is a plist with keyword keys."
+ (while (consp list)
+ (setq list (if (and (keywordp (car list))
+ (consp (cdr list)))
+ (cddr list)
+ 'not-plist)))
+ (null list))
+
(defun parseedn-print (datum)
"Insert DATUM as EDN into the current buffer.
DATUM can be any Emacs Lisp value."
@@ -175,19 +203,30 @@ DATUM can be any Emacs Lisp value."
((eq t datum)
(insert "true"))
- ((symbolp datum)
+ ((or (keywordp datum) (symbolp datum))
(insert (symbol-name datum)))
((vectorp datum) (insert "[") (parseedn-print-seq datum) (insert "]"))
+ ((or (hash-table-p datum) (parseedn-alist-p datum))
+ (insert "{")
+ (parseedn-print-hash-or-alist datum)
+ (insert "}"))
+
+ ((parseedn-plist-p datum)
+ (insert "{")
+ (parseedn-print-plist datum)
+ (insert "}"))
+
((consp datum)
(cond
+ ((not (listp (cdr datum))) ; dotted pair
+ (error "Don't know how to print: %s" datum))
((eq 'edn-set (car datum))
(insert "#{") (parseedn-print-seq (cadr datum)) (insert "}"))
(t (insert "(") (parseedn-print-seq datum) (insert ")"))))
- ((hash-table-p datum)
- (insert "{") (parseedn-print-kvs datum) (insert "}"))))
+ (t (error "Don't know how to print: %s" datum))))
(defun parseedn-print-str (datum)
"Return a string containing DATUM as EDN.
diff --git a/test/parseedn-test.el b/test/parseedn-test.el
index 9ddbdb4409..3c8a0f1221 100644
--- a/test/parseedn-test.el
+++ b/test/parseedn-test.el
@@ -38,6 +38,10 @@
(should (equal (parseedn-print-str 1.2) "1.2"))
(should (equal (parseedn-print-str [1 2 3]) "[1 2 3]"))
(should (equal (parseedn-print-str t) "true"))
+ (should (equal (parseedn-print-str '((a . 1) (b . 2))) "{a 1, b 2}"))
+ (should (equal (parseedn-print-str '((a . 1) (b . ((c . 3))))) "{:a 1, :b
{:c 3}}"))
+ (should (equal (parseedn-print-str '(:a 1 :b 2)) "{:a 1, :b 2}"))
+ (should (equal (parseedn-print-str '(:a 1 :b (:c 3))) "{:a 1, :b {:c 3}}"))
(should (listp (member (parseedn-print-str
(let ((ht (make-hash-table)))
(puthash :a 1 ht)