Skip to content

Commit

Permalink
Handle custom library crate-type
Browse files Browse the repository at this point in the history
Closes GH-54.
  • Loading branch information
fmdkdd committed Apr 4, 2017
1 parent a5491da commit 7e5fc78
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
26 changes: 24 additions & 2 deletions flycheck-rust.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@

;;; Code:

(eval-when-compile
(require 'pcase)
(require 'let-alist))

(require 'dash)
(require 'flycheck)
(require 'seq)
(require 'json)
(require 'let-alist)

(defun flycheck-rust-find-manifest (file-name)
"Get the Cargo.toml manifest for FILE-NAME.
Expand Down Expand Up @@ -146,7 +149,26 @@ description of the conventional Cargo project layout."
(file-name-directory manifest)))))
;; If all else fails, just pick the first target
(car targets))))
(let-alist target (cons (car .kind) .name)))))
(let-alist target (cons (flycheck-rust-normalize-target-kind .kind) .name)))))

(defun flycheck-rust-normalize-target-kind (kinds)
"Return the normalized target name from KIND.
KIND is a list of target name as returned by `cargo metadata',
which do not necessarily correspond to to target names that can
be passed as argument to `cargo rustc'.
The normalization returns a valid cargo target based on KINDS."
;; Assumption: we only care about the first kind name. Multiple kinds only
;; seem to happen for library crate types, and those all maps to the same
;; `lib' target.
(pcase (car kinds)
(`"dylib" "lib")
(`"rlib" "lib")
(`"staticlib" "lib")
(`"cdylib" "lib")
(`"proc-macro" "lib")
(_ (car kinds))))

;;;###autoload
(defun flycheck-rust-setup ()
Expand Down
7 changes: 7 additions & 0 deletions tests/custom-lib-target/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "custom-lib-target"
version = "0.1.0"

[lib]
name = "foo"
crate-type = ["dylib", "cdylib"]
6 changes: 6 additions & 0 deletions tests/custom-lib-target/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}
8 changes: 8 additions & 0 deletions tests/test-rust-setup.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
(defun crate-file (file-name)
(expand-file-name file-name "tests/test-crate"))

(defun lib-crate-file (file-name)
(expand-file-name file-name "tests/custom-lib-target"))

(describe
"`flycheck-rust-find-cargo-target' associates"

Expand Down Expand Up @@ -107,4 +110,9 @@
(expect
(flycheck-rust-find-cargo-target (crate-file "benches/support/mod.rs"))
:to-equal-one-of '("bench" . "a") '("bench" . "b")))

(it "'src/lib.rs' to the library target"
(expect
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
:to-equal "lib"))
)

0 comments on commit 7e5fc78

Please sign in to comment.