emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Nicolas Goaziou
Subject: Re: [O] [0] ob-rust, support for rust code blocks
Date: Wed, 20 Dec 2017 00:16:15 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

Hello,

Andrés Saraos Luna <address@hidden> 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
<https://github.com/micanzhang/ob-rust>. 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 "
    <point>#+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 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,

-- 
Nicolas Goaziou



reply via email to

[Prev in Thread] Current Thread [Next in Thread]