Re: [O] [0] ob-rust, support for rust code blocks

2017-12-19 Thread Nicolas Goaziou
Hello,

Andrés Saraos Luna  writes:

> As a toy project I've written a simple ob-rust, mainly in order to
> prepare some simple demos using rust.

Thank you.

There is another "ob-rust.el" at
. Would it make sense to join
efforts with its author?

> Since I'm not completely sure of the submission protocol for new
> features, the new file lives in contrib for now, although it doesn't
> depend on anything outside of `ob' really so maybe it could be added
> to core?

You need to sign FSF papers for that. Even if it doesn't land in master,
I suggest to start the process as it could take some time, depending on
your country. It would be nice to sort out the issue about 

> As far as testing is concerned, since I don't know how to use the
> database used by `org-test-at-id' I hardcoded the name of the testing
> file, I'd be happy to change that if necessary.

You don't need to use `org-test-at-id'. I even suggest to stay away from
it, as it makes debugging harder. Instead, you could make tests
self-sufficient, e.g.:

(should
 (equal 42
(org-test-with-temp-text "
#+name: test-simple
#+BEGIN_SRC rust :results silent
  fn main() {
  let answer = 42;
  println!("{}", answer);
  }
#+END_SRC"
  (org-babel-execute-src-block

Note that `should' is in the outer part of the test.

> Here's hoping this is useful for others, and since this is my first
> time writing anything functional in elisp I'm gladly open for any
> hints to improve the code.

I have a few suggestion. In particular docstrings should start with one
(or two) sentence(s) filling the first line. Additional sentences need
to start on subsequent lines.

Also, you need to add two spaces at the end of sentences.

> * contrib/lisp/ob-rust.el: Support for limited rust source code in org
>   babel.
>
> * testing/examples/ob-rust-test.org: Test all implemented features of
>   ob-rust in the form of named Org source blocks.
>
> * testing/lisp/test-ob-rust.el: Defines the tests.
>
> TINYCHANGE

This is not a TINYCHANGE :)

> +;; Org-Babel support for evaluating rust code.

Babel support for...

> +;; A currently very limited implementation:
> +;;  - arrays, vecs, lists or tables are not yet supported as header
> +;;  arguments
> +;;  - no error handling
> +;;  - only :results output is supported
> +;;  - cargo is completely ignored
> +
> +(require 'ob)
> +
> +(defvar org-babel-tangle-lang-exts)
> +(add-to-list 'org-babel-tangle-lang-exts '("rust" . "rs"))
> +
> +(defcustom org-babel-rust-command "rustc"
> +  "Name of the rust command."
> +  :group 'org-babel
> +  :type 'string)
> +
> +(defun org-babel-execute:rust (body params)

This function is missing a docstring.

> +  (let* ((full-body (org-babel-expand-body:rust body params))
> + (cmpflag (or (cdr (assq :cmpflag params)) ""))
> + (cmdline (or (cdr (assq :cmdline params)) ""))
> + (default-directory org-babel-temporary-directory)
> + (src-file (org-babel-temp-file "rust-src-" ".rs"))
> + (exe-file (org-babel-rust-exe-file src-file cmpflag))
> + (results))
> +(with-temp-file src-file
> +  (insert full-body)
> +  (when (require 'rust-mode nil t)
> +(rust-format-buffer)))
> +(org-babel-eval
> + (format "%s %s %s" org-babel-rust-command cmpflag src-file) "")
> +(setq results (org-babel-eval (format "%s %s" exe-file cmdline) ""))
> +(org-babel-reassemble-table
> + (org-babel-result-cond (cdr (assq :result-params params))
> +   (org-babel-read results)
> +   (let ((tmp-file (org-babel-temp-file "rs-")))
> + (with-temp-file tmp-file (insert results))
> + (org-babel-import-elisp-from-file tmp-file)))
> + (org-babel-pick-name
> +  (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
> + (org-babel-pick-name
> +  (cdr (assq :rowname-names params)) (cdr (assq :rownames params))
> +
> +(defun org-babel-expand-body:rust (body params)
> +  "Expand a block of rust code with org-babel according to its
> +header arguments."

The sentence must fit on a single line.

Also "Org Babel"

> +(defun org-babel-prep-session:rust (_session _params)
> +  "This function does nothing as C is a compiled language with no
> +support for sessions"
> +  (error "no support for sessions"))

C -> Rust

> +(defun org-babel-load-session:rust (_session _body _params)
> +  "This function does nothing as C is a compiled language with no
> +support for sessions"
> +  (error "no support for sessions"))

Ditto.
> +  (let* ((var (car var-pairs))
> + (val (cdr var-pairs))
> + (value-type (org-babel-rust-val-to-rust-type val))
> + (var-s (symbol-name var))
> + (var-regexp "\\(^mut_\\)?\\([[:alnum:]_]+\\)\\(: ?[[:alnum:]]+\\)?[ 
> \t]*$")
> + (mut
> +  (progn
> +(string-match var-regexp var-s)
> +(match-string 1 

[O] [0] ob-rust, support for rust code blocks

2017-12-18 Thread Andrés Saraos Luna
Hi all,

As a toy project I've written a simple ob-rust, mainly in order to prepare some 
simple demos using rust.

It works well for all the simple examples provided in the ob-rust-test.org 
file. Its shortcomings are
documented in the ob-rust.el file itself.

Since I'm not completely sure of the submission protocol for new features, the 
new file lives in
contrib for now, although it doesn't depend on anything outside of `ob' really 
so maybe it could be
added to core?

As far as testing is concerned, since I don't know how to use the database used 
by `org-test-at-id' I
hardcoded the name of the testing file, I'd be happy to change that if 
necessary.

Here's hoping this is useful for others, and since this is my first time 
writing anything functional
in elisp I'm gladly open for any hints to improve the code.

---
Andrés Saraos Luna>From 0a91c2b0d43e6e8f19d023208e78b07722a5dc33 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9s=20Saraos=20Luna?= 
Date: Mon, 18 Dec 2017 10:28:07 +0100
Subject: [PATCH] ob-rust.el, adds working version of rust evaluation in org
 source blocks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Andrés Saraos Luna 

* contrib/lisp/ob-rust.el: Support for limited rust source code in org
  babel.

* testing/examples/ob-rust-test.org: Test all implemented features of
  ob-rust in the form of named Org source blocks.

* testing/lisp/test-ob-rust.el: Defines the tests.

TINYCHANGE
---
 contrib/lisp/ob-rust.el   | 207 ++
 testing/examples/ob-rust-test.org |  82 +++
 testing/lisp/test-ob-rust.el  | 110 
 3 files changed, 399 insertions(+)
 create mode 100644 contrib/lisp/ob-rust.el
 create mode 100644 testing/examples/ob-rust-test.org
 create mode 100644 testing/lisp/test-ob-rust.el

diff --git a/contrib/lisp/ob-rust.el b/contrib/lisp/ob-rust.el
new file mode 100644
index 0..daa206228
--- /dev/null
+++ b/contrib/lisp/ob-rust.el
@@ -0,0 +1,207 @@
+;;; ob-rust.el --- Babel Functions for rust -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2017 Free Software Foundation, Inc.
+
+;; Author: Andrés Saraos Luna
+;; Keywords: literate programming, reproducible research
+;; Homepage: http://orgmode.org
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs 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.
+
+;; GNU Emacs 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 GNU Emacs.  If not, see .
+
+;;; Commentary:
+
+;; Org-Babel support for evaluating rust code.
+;;
+;; A currently very limited implementation:
+;;  - arrays, vecs, lists or tables are not yet supported as header
+;;  arguments
+;;  - no error handling
+;;  - only :results output is supported
+;;  - cargo is completely ignored
+
+(require 'ob)
+
+(defvar org-babel-tangle-lang-exts)
+(add-to-list 'org-babel-tangle-lang-exts '("rust" . "rs"))
+
+(defcustom org-babel-rust-command "rustc"
+  "Name of the rust command."
+  :group 'org-babel
+  :type 'string)
+
+(defun org-babel-execute:rust (body params)
+  (let* ((full-body (org-babel-expand-body:rust body params))
+ (cmpflag (or (cdr (assq :cmpflag params)) ""))
+ (cmdline (or (cdr (assq :cmdline params)) ""))
+ (default-directory org-babel-temporary-directory)
+ (src-file (org-babel-temp-file "rust-src-" ".rs"))
+ (exe-file (org-babel-rust-exe-file src-file cmpflag))
+ (results))
+(with-temp-file src-file
+  (insert full-body)
+  (when (require 'rust-mode nil t)
+(rust-format-buffer)))
+(org-babel-eval
+ (format "%s %s %s" org-babel-rust-command cmpflag src-file) "")
+(setq results (org-babel-eval (format "%s %s" exe-file cmdline) ""))
+(org-babel-reassemble-table
+ (org-babel-result-cond (cdr (assq :result-params params))
+   (org-babel-read results)
+   (let ((tmp-file (org-babel-temp-file "rs-")))
+ (with-temp-file tmp-file (insert results))
+ (org-babel-import-elisp-from-file tmp-file)))
+ (org-babel-pick-name
+  (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
+ (org-babel-pick-name
+  (cdr (assq :rowname-names params)) (cdr (assq :rownames params))
+
+(defun org-babel-expand-body:rust (body params)
+  "Expand a block of rust code with org-babel according to its
+header arguments."
+  (let* ((main-p (not (string= (cdr (assq :main params))