branch: externals/tomelr commit 96c890a68b9a587283bc7522c3893370cc522ca6 Author: Kaushal Modi <kaushal.m...@gmail.com> Commit: Kaushal Modi <kaushal.m...@gmail.com>
feat: Convert Lisp lists to TOML arrays --- README.org | 34 ++++++++++++++++------------------ test/all-tests.el | 1 + test/{all-tests.el => tarray.el} | 28 +++++++++++++++++++++++----- tomelr.el | 17 ++++------------- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/README.org b/README.org index c21b8a72e9..948d37fdaa 100644 --- a/README.org +++ b/README.org @@ -18,7 +18,7 @@ the Emacs core library [[https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/j It will then be gradually refactored so that it meets the specification defined below. -* Library Completion Status [2/7] +* Library Completion Status [3/7] - [X] Scalar - [X] Boolean - [X] Integer @@ -27,7 +27,7 @@ specification defined below. - [X] Date - [X] Date + Time with Offset - [X] Nil -- [ ] Arrays +- [X] Arrays - [ ] Array of Arrays - [ ] Tables - [ ] Array of Tables @@ -280,29 +280,33 @@ key5 = true : } ** TOML Arrays: Lists https://toml.io/en/v1.0.0#array -*** Lists +*** DONE Plain Arrays +CLOSED: [2022-04-29 Fri 00:25] **** S-expression -#+begin_src emacs-lisp :eval no :noweb-ref lists +#+begin_src emacs-lisp :eval no :noweb-ref arrays '((integers . (1 2 3)) (integers2 . [1 2 3]) ;Same as above (colors . ("red" "yellow" "green")) - (string_array . ("all" "strings" "are the same" "type")) + ;; Mixed-type arrays are allowed (numbers . (0.1 0.2 0.5 1 2 5))) #+end_src **** TOML +#+begin_src emacs-lisp :noweb yes :exports results :wrap src toml +(tomelr-encode + <<arrays>>) +#+end_src + +#+RESULTS: #+begin_src toml integers = [ 1, 2, 3 ] integers2 = [ 1, 2, 3 ] colors = [ "red", "yellow", "green" ] -string_array = [ "all", 'strings', """are the same""", '''type''' ] - -# Mixed-type arrays are allowed numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ] #+end_src **** JSON Reference #+begin_src emacs-lisp :noweb yes :exports results (json-encode-pretty - <<lists>>) + <<arrays>>) #+end_src #+RESULTS: @@ -323,12 +327,6 @@ numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ] "yellow", "green" ], - "string_array": [ - "all", - "strings", - "are the same", - "type" - ], "numbers": [ 0.1, 0.2, @@ -339,9 +337,9 @@ numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ] ] } #+end_example -*** Lists of lists +*** Array of Arrays **** S-expression -#+begin_src emacs-lisp :eval no :noweb-ref lists-of-lists +#+begin_src emacs-lisp :eval no :noweb-ref array-of-arrays '((nested_arrays_of_ints . [(1 2) (3 4 5)]) (nested_mixed_array . [(1 2) ("a" "b" "c")]) (contributors . ("Foo Bar <f...@example.com>" @@ -363,7 +361,7 @@ contributors = [ **** JSON Reference #+begin_src emacs-lisp :noweb yes :exports results (json-encode-pretty - <<lists-of-lists>>) + <<array-of-arrays>>) #+end_src #+RESULTS: diff --git a/test/all-tests.el b/test/all-tests.el index 5b1013864c..f89407594b 100644 --- a/test/all-tests.el +++ b/test/all-tests.el @@ -23,3 +23,4 @@ (require 'tscalar) (require 'tnil) +(require 'tarray) diff --git a/test/all-tests.el b/test/tarray.el similarity index 51% copy from test/all-tests.el copy to test/tarray.el index 5b1013864c..1c92e55c8c 100644 --- a/test/all-tests.el +++ b/test/tarray.el @@ -1,4 +1,4 @@ -;;; all-tests.el --- Tests for tomelr.el -*- lexical-binding: t; -*- +;; -*- lexical-binding: t; -*- ;; Authors: Kaushal Modi <kaushal.m...@gmail.com> @@ -17,9 +17,27 @@ ;; 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 arrays. -(require 'tscalar) -(require 'tnil) +;;; Code: +(require 'tomelr) + +;;;; Key with array value +(ert-deftest test-array () + (let ((inp '(((integers . (1 2 3))) + ((integers2 . [1 2 3])) ;Same as above + ((colors . ("red" "yellow" "green"))) + ((numbers . (0.1 0.2 0.5 1 2 5))))) ;Mixed-type arrays are allowed + (ref '("integers = [ 1, 2, 3 ]" + "integers2 = [ 1, 2, 3 ]" + "colors = [ \"red\", \"yellow\", \"green\" ]" + "numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]")) + out) + (dolist (el inp) + (push (tomelr-encode el) out)) + (should (equal ref (nreverse out))))) + + +(provide 'tarray) diff --git a/tomelr.el b/tomelr.el index cd7ff6b901..8952f920a9 100644 --- a/tomelr.el +++ b/tomelr.el @@ -37,11 +37,6 @@ "String used for a single indentation level during encoding. This value is repeated for each further nested element.") -(defvar tomelr-encoding-lisp-style-closings nil - "If non-nil, delimiters ] and } will be formatted Lisp-style. -This means they will be placed on the same line as the last -element of the respective array or object, without indentation.") - (defvar tomelr-encoding-object-sort-predicate nil "Sorting predicate for TOML object keys during encoding. If nil, no sorting is performed. Else, TOML object keys are @@ -281,21 +276,17 @@ non-nil. Sorting can optionally be DESTRUCTIVE for speed." ;;;; Arrays (defun tomelr--print-array (array) "Like `tomelr-encode-array', but insert the TOML at point." - (insert ?\[) + (insert "[ ") (unless (length= array 0) (tomelr--with-indentation - (tomelr--print-indentation) (let ((first t)) (mapc (lambda (elt) (if first (setq first nil) - (insert ",") - (tomelr--print-indentation)) + (insert ", ")) (tomelr--print elt)) - array))) - (or tomelr-encoding-lisp-style-closings - (tomelr--print-indentation))) - (insert ?\])) + array)))) + (insert " ]")) (defun tomelr-encode-array (array) "Return a TOML representation of ARRAY.