summaryrefslogtreecommitdiffhomepage
path: root/lib/prog/jao-rust.el
diff options
context:
space:
mode:
Diffstat (limited to 'lib/prog/jao-rust.el')
-rw-r--r--lib/prog/jao-rust.el113
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/prog/jao-rust.el b/lib/prog/jao-rust.el
new file mode 100644
index 0000000..436de00
--- /dev/null
+++ b/lib/prog/jao-rust.el
@@ -0,0 +1,113 @@
+;;; 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)))
+ (thread-last
+ rel
+ (replace-regexp-in-string "^src\\(/bin\\)?/" "")
+ (string-replace "-" "_")
+ (replace-regexp-in-string "/\\(mod\\|lib\\|main\\)$" "")
+ (string-replace "/" "::"))))
+
+(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)
+ (message "No tests module")))
+
+(defun jao-rust-test-current ()
+ (interactive)
+ (if-let* ((m (jao-rust-current-module-name))
+ (n (jao-rust-test-before-point)))
+ (let ((rustic-test-arguments (format "-- --exact %s::tests::%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)
+ (messages "No tests in this module")
+ (let* ((mod (jao-rust-current-module-name))
+ (tests (mapconcat (lambda (x) (format "%s::tests::%s" mod x)) tests " "))
+ (rustic-test-arguments (format "-- --exact %s" tests))
+ (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