Skip to content

Commit 2799e63

Browse files
committed
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.
1 parent 34abafb commit 2799e63

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-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

0 commit comments

Comments
 (0)