emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/tomelr cedb75df72 29/84: feat: Support basic TOML table


From: ELPA Syncer
Subject: [elpa] externals/tomelr cedb75df72 29/84: feat: Support basic TOML tables
Date: Tue, 3 May 2022 09:58:10 -0400 (EDT)

branch: externals/tomelr
commit cedb75df72f9aed0ad990b631f32d71f6ba1b79d
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>

    feat: Support basic TOML tables
---
 README.org                       | 14 +++++++---
 test/all-tests.el                |  1 +
 test/{all-tests.el => ttable.el} | 31 +++++++++++++++++-----
 tomelr.el                        | 57 ++++++++++++++++++++++++++++------------
 4 files changed, 76 insertions(+), 27 deletions(-)

diff --git a/README.org b/README.org
index ba51f2e6b2..2acd5d41be 100644
--- a/README.org
+++ b/README.org
@@ -29,8 +29,8 @@ specification defined below.
 - [X] Nil
 - [X] Arrays
 - [X] Array of Arrays
-- [ ] Tables
-  - [ ] Basic Tables
+- [-] Tables
+  - [X] Basic Tables
   - [ ] Nested Tables
 - [ ] Array of Tables
   - [ ] Basic Array of Tables
@@ -393,7 +393,8 @@ nested_mixed_array = [ [ 1, 2 ], [ "a", "b", "c" ] ]
 }
 #+end_example
 ** TOML Tables: Maps or Dictionaries or Hash Tables
-*** Basic TOML Tables
+*** DONE Basic TOML Tables
+CLOSED: [2022-04-29 Fri 13:41]
 **** S-expression
 #+begin_src emacs-lisp :eval no :noweb-ref tables
 '((table-1 . ((key1 . "some string")
@@ -402,11 +403,16 @@ nested_mixed_array = [ [ 1, 2 ], [ "a", "b", "c" ] ]
               (key2 . 456))))
 #+end_src
 **** TOML
+#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml
+(tomelr-encode
+  <<tables>>)
+#+end_src
+
+#+RESULTS:
 #+begin_src toml
 [table-1]
   key1 = "some string"
   key2 = 123
-
 [table-2]
   key1 = "another string"
   key2 = 456
diff --git a/test/all-tests.el b/test/all-tests.el
index f89407594b..a28528c2c0 100644
--- a/test/all-tests.el
+++ b/test/all-tests.el
@@ -24,3 +24,4 @@
 (require 'tscalar)
 (require 'tnil)
 (require 'tarray)
+(require 'ttable)
diff --git a/test/all-tests.el b/test/ttable.el
similarity index 55%
copy from test/all-tests.el
copy to test/ttable.el
index f89407594b..fdcafa87f4 100644
--- a/test/all-tests.el
+++ b/test/ttable.el
@@ -1,4 +1,4 @@
-;;; all-tests.el --- Tests for tomelr.el                   -*- 
lexical-binding: t; -*-
+;; -*- lexical-binding: t; -*-
 
 ;; Authors: Kaushal Modi <kaushal.modi@gmail.com>
 
@@ -17,10 +17,29 @@
 ;; You should have received a copy of the GNU General Public License
 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-;;; Code:
+;;; Commentary:
 
-(setq load-prefer-newer t)
+;; Test conversion to TOML tables.
 
-(require 'tscalar)
-(require 'tnil)
-(require 'tarray)
+;;; Code:
+(require 'tomelr)
+
+;;;; Simple tables
+(ert-deftest test-table ()
+  (let ((inp '(((table-1 . ((key1 . "some string")
+                            (key2 . 123))))
+               ((table-2 . ((key1 . "another string")
+                            (key2 . 456))))))
+        (ref '("[table-1]
+  key1 = \"some string\"
+  key2 = 123"
+               "[table-2]
+  key1 = \"another string\"
+  key2 = 456"))
+        out)
+    (dolist (el inp)
+      (push (tomelr-encode el) out))
+    (should (equal ref (nreverse out)))))
+
+
+(provide 'ttable)
diff --git a/tomelr.el b/tomelr.el
index f4ed83e757..ce1ebd4e9d 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -196,40 +196,63 @@ Return the same STRING passed as input."
   "Return a TOML representation of STRING."
   (tomelr--with-output-to-string (tomelr--print-string string)))
 
-(defun tomelr--print-stringlike (object)
+(defun tomelr--print-stringlike (object &optional type)
   "Insert OBJECT encoded as a TOML string at point.
+
+TYPE is set to `table' if OBJECT is a TOML Table key.
+
 Return nil if OBJECT cannot be encoded as a TOML string."
   (cond ((stringp object)
-         ;; (message "[tomelr--print-stringlike DBG] string")
+         ;; (message "[tomelr--print-stringlike DBG] %S is string" object)
          (tomelr--print-string object))
         ((keywordp object)
-         ;; (message "[tomelr--print-stringlike DBG] keyword")
+         ;; (message "[tomelr--print-stringlike DBG] %S is keyword" object)
          (tomelr--print-string (symbol-name object) 1))
         ((symbolp object)
-         ;; (message "[tomelr--print-stringlike DBG] symbol")
-         (princ (symbol-name object))
-         ;; (tomelr--print-string (symbol-name object))
-         )))
+         ;; (message "[tomelr--print-stringlike DBG] %S is symbol" object)
+         (cond
+          ((equal type 'table)
+           (princ (format "[%s]" (symbol-name object))))
+          (t
+           (princ (symbol-name object)))))))
+
+(defun tomelr--print-key (key &optional type)
+  "Insert a TOML key representation of KEY at point.
+
+TYPE is set to `table' if KEY is a TOML Table key.
 
-(defun tomelr--print-key (object)
-  "Insert a TOML key representation of OBJECT at point.
 Signal `tomelr-key-format' if it cannot be encoded as a string."
-  (or (tomelr--print-stringlike object)
-      (signal 'tomelr-key-format (list object))))
+  (or (tomelr--print-stringlike key type)
+      (signal 'tomelr-key-format (list key))))
 
 ;;;; Objects
 (defun tomelr--print-pair (key val)
   "Insert TOML representation of KEY-VAL pair at point."
-  ;; (message "[tomelr--print-pair DBG] key = %S, val = %S" key val)
-  (when val                     ;Don't print the key if val is nil
-    (tomelr--print-indentation) ;Newline before each key in a key-value pair
-    (tomelr--print-key key)
-    (insert tomelr--print-keyval-separator)
-    (tomelr--print val)))
+  (let ((type (cond
+               ;; TODO: Need to find a robust way of detecting TOML tables.
+               ((and (mapp val)
+                     (consp val)       ;      val = ((KEY . VAL)) <- cons
+                     (consp (car val)) ;(car val) = (KEY . VAL)   <- also cons
+                     )
+                'table)
+               (t
+                nil))))
+    ;; (message "[tomelr--print-pair DBG] key = %S, val = %S, type = %S"
+    ;;          key val type)
+    ;; (message "[tomelr--print-pair DBG] val type = %S" (type-of val))
+    (when val                     ;Don't print the key if val is nil
+      (tomelr--print-indentation) ;Newline before each key in a key-value pair
+      (tomelr--print-key key type)
+      ;; Skip putting the separator if `type' has a non-nil value like
+      ;; `table'.
+      (unless type
+        (insert tomelr--print-keyval-separator))
+      (tomelr--print val))))
 
 (defun tomelr--print-map (map)
   "Insert TOML object representation of MAP at point.
 This works for any MAP satisfying `mapp'."
+  ;; (message "[tomelr--print-map DBG] map = %S" map)
   (unless (map-empty-p map)
     (tomelr--with-indentation
       (map-do #'tomelr--print-pair map))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]