Skip to content

Commit

Permalink
feat: Convert Lisp lists to TOML arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 29, 2022
1 parent 69217d4 commit 96c890a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
34 changes: 16 additions & 18 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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 <foo@example.com>"
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions test/all-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@

(require 'tscalar)
(require 'tnil)
(require 'tarray)
43 changes: 43 additions & 0 deletions test/tarray.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
;; -*- lexical-binding: t; -*-

;; Authors: Kaushal Modi <kaushal.modi@gmail.com>

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Test conversion to TOML arrays.

;;; 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)
17 changes: 4 additions & 13 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 96c890a

Please sign in to comment.