Skip to content

Commit 2f3c5ff

Browse files
authored
Add basic var support to the org-src blocks. (#62)
* Add basic var support to the org-src blocks. This will just wrap the variables into (if needed) nested &[&str] constants. It will also not care about the types of the passed variables, except if they are lists - all other typeinformation doesn't matter - everything will be a &str in the end. * Add tests to check the three support cases of vars.
1 parent 34abafb commit 2f3c5ff

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

rustic-babel.el

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,51 @@ executed with the parameter `:include'."
361361
(module (expand-file-name (format "%s.rs" b) src-dir)))
362362
(write-region contents nil module nil 0))))
363363

364+
(defun rustic-babel-variable-to-type (var)
365+
"Return a valid const type for the passed variable.
366+
367+
Only supports three cases:
368+
369+
1. Simple value: A='a' -> &str
370+
2. Simple list: A=('a' 'b') -> &[&str]
371+
3. Nested list (org-table): A=(('a' 'b')) -> &[&[&str]]"
372+
(if (listp var)
373+
(if (listp (car var)) "&[&[&str]]" "&[&str]")
374+
"&str"))
375+
376+
(defun rustic-babel-variable-to-rust (var)
377+
"Return a valid rust assignment of an org VAR.
378+
379+
This will convert a simple variable to a &str and list to nested
380+
list of strings. Tables will be converted to &[&[&str]] but need
381+
to be homogenous."
382+
(if (listp var)
383+
(if (listp (car var))
384+
(concat "&[" (string-join (mapcar #'rustic-babel-variable-to-rust var) ",") "]")
385+
(concat "&["
386+
(string-join
387+
(mapcar (lambda (v) (format "\"%s\"" v)) var)
388+
", ")
389+
"]"))
390+
(concat "\"" var "\"")))
391+
392+
(defun rustic-babel-variable-assignments:rust (vars)
393+
"Convert the passed org-src block VARS into a matching const type.
394+
395+
There are only 3 cases:
396+
397+
1. Simple value: A='a' -> &str
398+
2. Simple list: A=('a' 'b') -> &[&str]
399+
2. Nested list (org-table): A=(('a' 'b')) -> &[&[&str]]"
400+
(string-join
401+
(mapcar
402+
(lambda (pair)
403+
(let ((key (car pair))
404+
(value (cdr pair)))
405+
(format "const %s: %s = %s;\n" key (rustic-babel-variable-to-type value) (rustic-babel-variable-to-rust value))))
406+
(org-babel--get-vars vars))
407+
"\n"))
408+
364409
(defun org-babel-execute:rustic (body params)
365410
"Execute a block of Rust code with org-babel.
366411
@@ -376,6 +421,7 @@ kill the running process."
376421
(dir (setq rustic-babel-dir (expand-file-name project)))
377422
(main-p (cdr (assq :main params)))
378423
(main (expand-file-name "main.rs" (concat dir "/src")))
424+
(vars (cdr (assq :var params)))
379425
(wrap-main (cond ((string= main-p "yes") t)
380426
((string= main-p "no") nil)
381427
(t rustic-babel-auto-wrap-main)))
@@ -399,7 +445,8 @@ kill the running process."
399445
(concat "#![allow(non_snake_case, unused)]\n"
400446
(if use-blocks (rustic-babel-insert-mod use-blocks) "")
401447
(if include-blocks (rustic-babel-include-blocks include-blocks) "")
402-
(if wrap-main (rustic-babel-ensure-main-wrap body) body))
448+
(if wrap-main (rustic-babel-ensure-main-wrap body) body)
449+
(if (not (eq vars nil)) (rustic-babel-variable-assignments:rust params) ""))
403450
nil main nil 0)
404451
(rustic-babel-eval dir toolchain main-p)
405452
(setq rustic-babel-src-location

test/rustic-babel-test.el

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,28 @@
271271
(rustic-test-babel-execute-block buf)
272272
(should (eq (rustic-test-babel-check-results buf) nil)))))
273273

274+
(ert-deftest rustic-test-babel-block-with-simple-var ()
275+
(let* ((string "println!(\"{}\", A)")
276+
(params ":main yes :var A=\"A\"")
277+
(buf (rustic-test-get-babel-block string params)))
278+
(with-current-buffer buf
279+
(rustic-test-babel-execute-block buf)
280+
(should (string-equal (rustic-test-babel-check-results buf) "A\n")))))
281+
282+
(ert-deftest rustic-test-babel-block-with-list-var ()
283+
(let* ((string "println!(\"{:?}\", A)")
284+
(params ":main yes :var A='(\"A\" \"B\")")
285+
(buf (rustic-test-get-babel-block string params)))
286+
(with-current-buffer buf
287+
(rustic-test-babel-execute-block buf)
288+
(should (string-equal (rustic-test-babel-check-results buf) "[\"A\", \"B\"]\n")))))
289+
290+
(ert-deftest rustic-test-babel-block-with-nested-list-var ()
291+
(let* ((string "println!(\"{:?}\", A)")
292+
(params ":main yes :var A='((\"A\" \"B\") (\"C\" \"D\"))")
293+
(buf (rustic-test-get-babel-block string params)))
294+
(with-current-buffer buf
295+
(rustic-test-babel-execute-block buf)
296+
(should (string-equal (rustic-test-babel-check-results buf) "[[\"A\", \"B\"], [\"C\", \"D\"]]\n")))))
297+
274298
(provide 'rustic-babel-test)

0 commit comments

Comments
 (0)