summaryrefslogtreecommitdiffhomepage
path: root/lib/prog/jao-rust.el
blob: b2549ab8f0f9c46abfc5a5ec2dd278379e29f9ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
;;; jao-rust.el --- Utilities for rust programming   -*- lexical-binding: t; -*-

;; Copyright (C) 2026  Jose Ruiz

;; Author: Jose Ruiz <mail@jao.io>
;; Keywords: languages

;; This program 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.

;; This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(defun jao-rust--root ()
  (locate-dominating-file default-directory "Cargo.toml"))

(defun jao-rust-open-cargo ()
  (interactive)
  (let ((f (expand-file-name "Cargo.toml" (jao-rust--root))))
    (when (or (file-exists-p f)
              (y-or-n-p "No Cargo.toml found. Create? "))
        (find-file f))))

(defun jao-rust-current-module-name ()
  (let* ((root (expand-file-name (jao-rust--root)))
         (current (file-name-sans-extension (buffer-file-name)))
         (rel (string-replace root "" current)))
    (and (string-prefix-p "src/" rel)
         (thread-last
           rel
           (replace-regexp-in-string "^src\\(/bin\\)?/" "")
           (string-replace "-" "_")
           (replace-regexp-in-string "\\(^\\|/\\)\\(mod\\|lib\\|main\\)$" "")
           (string-replace "/" "::")))))

(defun jao-rust--tests-prefix ()
  (let ((m (jao-rust-current-module-name)))
    (cond ((not (stringp m)) "")
          ((string-empty-p m) "tests::")
          (t (concat m "::tests::")))))

(defconst jao-rust--test-header-rx
  (concat "#\\[" (regexp-opt '("test" "quickcheck" "tokio::test") t)))

(defconst jao-rust--test-name-rx "^ *\\(pub \\)?\\( async \\)?fn \\([^(]+\\)(")

(defun jao-rust-test-at-point ()
  (when (looking-at jao-rust--test-name-rx) (match-string-no-properties 3)))

(defun jao-rust-test-before-point ()
  (save-excursion
    (when (re-search-backward jao-rust--test-header-rx nil t)
      (forward-line)
      (jao-rust-test-at-point))))


(defconst jao-rust--tests-mod-rx "^\\(pub\\)? *mod tests {")

(defun jao-rust-goto-tests ()
  (interactive)
  (or (re-search-forward jao-rust--tests-mod-rx nil t)
      (re-search-backward jao-rust--tests-mod-rx nil t)
      (progn
        (goto-char (point-min))
        (re-search-forward jao-rust--test-header-rx nil t))
      (when (y-or-n-p "No tests module. Add it?")
        (goto-char (point-max))
        (insert "\n#[cfg(test)]\nmod tests {\n\n}")
        (forward-line -1))))

(defun jao-rust-test-current ()
  (interactive)
  (if-let* ((m (jao-rust--tests-prefix))
            (n (jao-rust-test-before-point)))
      (let ((rustic-test-arguments (format "-- --exact %s%s" m n))
            (rustic-cargo-use-last-stored-arguments t))
        (rustic-cargo-test))
    (message "No test before point")))

(defun jao-rust-buffer-tests ()
  (save-excursion
    (goto-char (point-min))
    (let ((res '()))
      (while (re-search-forward jao-rust--test-header-rx nil t)
        (forward-line)
        (when-let* ((name (jao-rust-test-at-point))) (push name res)))
      res)))

(defun jao-rust-test-module ()
  (interactive)
  (let ((tests (jao-rust-buffer-tests)))
    (if (seq-empty-p tests)
        (when (y-or-n-p "No tests in this module. Run doc tests?")
          (jao-rust-test-doctests))
      (let* ((prefix (jao-rust--tests-prefix))
             (tests (mapconcat (lambda (x) (format "%s%s" prefix x)) tests " "))
             (rustic-test-arguments (format "-- --exact %s" tests))
             (rustic-cargo-use-last-stored-arguments t))
        (rustic-cargo-test)))))

(defun jao-rust-test-doctests (&optional all)
  (interactive "P")
  (let* ((m (if all "" (jao-rust-current-module-name)))
         (rustic-cargo-test-runner 'cargo)
         (rustic-test-arguments (format "--doc %s" m))
         (rustic-cargo-use-last-stored-arguments t))
    (rustic-cargo-test)))

(defun jao-rust-retest ()
  (interactive)
  (let* ((rustic-test-arguments "-R latest")
         (rustic-cargo-use-last-stored-arguments t))
    (rustic-cargo-test)))

(defun jao-rust-test-workspace ()
  (interactive)
  (let ((rustic-test-arguments rustic-default-test-arguments))
    (rustic-cargo-test)))


(defun jao-rust-build-doc (deps)
  (interactive "P")
  (rustic-run-cargo-command `(,(rustic-cargo-bin) "doc" ,@(unless deps '("--no-deps")))))

(provide 'jao-rust)
;;; jao-rust.el ends here