Skip to content

Commit 6538525

Browse files
committed
Add support for 'required-features'.
Discover 'required-features' in Cargo manifest and initialize value of 'rust-cargo-features'.
1 parent 1ffbbfe commit 6538525

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

flycheck-rust.el

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ FILE-NAME is the path of the file that is matched against the
119119
`src_path' value in the list of `targets' returned by `cargo
120120
read-manifest'.
121121
122-
Return a cons cell (KIND . NAME) where KIND is the target
123-
kind (lib, bin, test, example or bench), and NAME the target
124-
name (usually, the crate name). If FILE-NAME exactly matches a
125-
target `src-path', this target is returned. Otherwise, return
122+
Return a alist ((KIND . k) (NAME . n) (REQUIRED-FEATURES . rf))
123+
where KIND is the target kind (lib, bin, test, example or bench),
124+
NAME the target name (usually, the crate name), and REQUIRED-FEATURES is the
125+
optional list of features required to build selected target. If FILE-NAME
126+
exactly matches a target `src-path', this target is returned. Otherwise, return
126127
the closest matching target, or nil if no targets could be found.
127128
128129
See http://doc.crates.io/manifest.html#the-project-layout for a
@@ -160,7 +161,10 @@ description of the conventional Cargo project layout."
160161
(--find (not (equal target it))))))
161162
(when target
162163
(let-alist target
163-
(cons (flycheck-rust-normalize-target-kind .kind) .name))))))
164+
(seq-filter (lambda (kv) (cdr kv))
165+
(list (cons 'kind (flycheck-rust-normalize-target-kind .kind))
166+
(cons 'name .name)
167+
(cons 'required-features .required-features))))))))
164168

165169

166170
(defun flycheck-rust-normalize-target-kind (kinds)
@@ -194,9 +198,11 @@ Flycheck according to the Cargo project layout."
194198
;; https://github.com/flycheck/flycheck-rust/issues/40#issuecomment-253760883).
195199
(with-demoted-errors "Error in flycheck-rust-setup: %S"
196200
(-when-let* ((file-name (buffer-file-name))
197-
((kind . name) (flycheck-rust-find-cargo-target file-name)))
198-
(setq-local flycheck-rust-crate-type kind)
199-
(setq-local flycheck-rust-binary-name name))))
201+
(target (flycheck-rust-find-cargo-target file-name)))
202+
(let-alist target
203+
(setq-local flycheck-rust-features .required-features)
204+
(setq-local flycheck-rust-crate-type .kind)
205+
(setq-local flycheck-rust-binary-name .name)))))
200206

201207
(provide 'flycheck-rust)
202208

tests/test-rust-setup.el

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,91 +44,101 @@
4444
(defun build-script-crate-file (file-name)
4545
(expand-file-name file-name "tests/build-script-test"))
4646

47+
(defun cdrassoc (sym alist) (cdr (assoc sym alist)))
48+
4749
(describe
4850
"`flycheck-rust-find-cargo-target' associates"
4951

5052
(it "'src/lib.rs' to the library target"
5153
(expect
52-
(car (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
54+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
5355
:to-equal "lib"))
5456

5557
(it "'src/a.rs' to the library target"
5658
(expect
57-
(car (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
59+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
5860
:to-equal "lib"))
5961

6062
(it "'src/main.rs' to the main binary target"
6163
(expect
6264
(flycheck-rust-find-cargo-target (crate-file "src/main.rs"))
63-
:to-equal '("bin" . "test-crate")))
65+
:to-equal '((kind . "bin") (name . "test-crate"))))
6466

6567
(it "'src/bin/a.rs' to the 'a' binary target"
6668
(expect
6769
(flycheck-rust-find-cargo-target (crate-file "src/bin/a.rs"))
68-
:to-equal '("bin" . "a")))
70+
:to-equal '((kind . "bin") (name . "a"))))
6971

7072
(it "'src/bin/b.rs' to the 'b' binary target"
7173
(expect
7274
(flycheck-rust-find-cargo-target (crate-file "src/bin/b.rs"))
73-
:to-equal '("bin" . "b")))
75+
:to-equal '((kind . "bin") (name . "b"))))
7476

7577
(it "'src/bin/support/mod.rs' to any binary target"
7678
(expect
7779
(flycheck-rust-find-cargo-target (crate-file "src/bin/support/mod.rs"))
78-
:to-equal-one-of '("bin". "a") '("bin". "b")))
80+
:to-equal-one-of
81+
'((kind . "bin") (name . "a"))
82+
'((kind . "bin") (name . "b"))))
7983

8084
(it "'tests/a.rs' to the 'a' test target"
8185
(expect
8286
(flycheck-rust-find-cargo-target (crate-file "tests/a.rs"))
83-
:to-equal '("test" . "a")))
87+
:to-equal '((kind . "test") (name . "a"))))
8488

8589
(it "'tests/support/mod.rs' to any test target"
8690
(expect
8791
(flycheck-rust-find-cargo-target (crate-file "tests/support/mod.rs"))
88-
:to-equal-one-of '("test". "a") '("test". "b")))
92+
:to-equal-one-of
93+
'((kind . "test") (name . "a"))
94+
'((kind . "test") (name . "b"))))
8995

9096
(it "'examples/a.rs' to the 'a' example target"
9197
(expect
9298
(flycheck-rust-find-cargo-target (crate-file "examples/a.rs"))
93-
:to-equal '("example" . "a")))
99+
:to-equal '((kind . "example") (name . "a"))))
94100

95101
(it "'examples/b.rs' to the 'b' example target"
96102
(expect
97103
(flycheck-rust-find-cargo-target (crate-file "examples/b.rs"))
98-
:to-equal '("example" . "b")))
104+
:to-equal '((kind . "example") (name . "b"))))
99105

100106
(it "'examples/support/mod.rs' to any example target"
101107
(expect
102108
(flycheck-rust-find-cargo-target (crate-file "examples/support/mod.rs"))
103-
:to-equal-one-of '("example" . "a") '("example" . "b")))
109+
:to-equal-one-of
110+
'((kind . "example") (name . "a"))
111+
'((kind . "example") (name . "b"))))
104112

105113
(it "'benches/a.rs' to the 'a' bench target"
106114
(expect
107115
(flycheck-rust-find-cargo-target (crate-file "benches/a.rs"))
108-
:to-equal '("bench" . "a")))
116+
:to-equal '((kind . "bench") (name . "a"))))
109117

110118
(it "'benches/b.rs' to the 'b' bench target"
111119
(expect
112120
(flycheck-rust-find-cargo-target (crate-file "benches/b.rs"))
113-
:to-equal '("bench" . "b")))
121+
:to-equal '((kind . "bench") (name . "b"))))
114122

115123
(it "'benches/support/mod.rs' to any bench target"
116124
(expect
117125
(flycheck-rust-find-cargo-target (crate-file "benches/support/mod.rs"))
118-
:to-equal-one-of '("bench" . "a") '("bench" . "b")))
126+
:to-equal-one-of
127+
'((kind . "bench") (name . "a"))
128+
'((kind . "bench") (name . "b"))))
119129

120130
(it "'src/lib.rs' to the library target (custom-lib-target)"
121131
(expect
122132
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
123-
:to-equal "lib"))
133+
:to-equal '(kind . "lib")))
124134

125135
(it "'build.rs' to any target in the same workspace member (parent)"
126136
(expect
127137
(flycheck-rust-find-cargo-target (build-script-crate-file "build.rs"))
128-
:to-equal (cons "bin" "build-script-test")))
138+
:to-equal '((kind . "bin") (name . "build-script-test"))))
129139

130140
(it "'build.rs' to any target in the same workspace member (child)"
131141
(expect
132142
(flycheck-rust-find-cargo-target (build-script-crate-file "lib-test/build.rs"))
133-
:to-equal (cons "lib" "lib-test")))
143+
:to-equal '((kind . "lib") (name . "lib-test"))))
134144
)

0 commit comments

Comments
 (0)