From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: [0] ob-rust, support for rust code blocks Date: Wed, 20 Dec 2017 00:16:15 +0100 Message-ID: <877etijolc.fsf@nicolasgoaziou.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:34827) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRR7U-0003pb-2Q for emacs-orgmode@gnu.org; Tue, 19 Dec 2017 18:16:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eRR7S-000080-MD for emacs-orgmode@gnu.org; Tue, 19 Dec 2017 18:16:20 -0500 Received: from relay3-d.mail.gandi.net ([2001:4b98:c:538::195]:44441) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eRR7S-000064-DA for emacs-orgmode@gnu.org; Tue, 19 Dec 2017 18:16:18 -0500 In-Reply-To: (=?utf-8?Q?=22Andr=C3=A9s?= Saraos Luna"'s message of "Mon, 18 Dec 2017 14:44:20 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: =?utf-8?Q?Andr=C3=A9s?= Saraos Luna Cc: emacs-orgmode@gnu.org Hello, Andr=C3=A9s 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=20 > 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 =3D 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 var-s))) > + (var-name > + (progn > + (string-match var-regexp var-s) > + (match-string 2 var-s))) > + (var-type > + (or > + (progn > + (string-match var-regexp var-s) > + (match-string 3 var-s)) You shouldn't match multiple times. Match once and get match strings. Regards, --=20 Nicolas Goaziou