Skip to content

Commit a1e1dcb

Browse files
committed
Simplify
1 parent e2bc862 commit a1e1dcb

File tree

1 file changed

+66
-107
lines changed

1 file changed

+66
-107
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 66 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,6 @@ fn inferred_examples(package_root: &Path) -> Vec<(String, PathBuf)> {
134134
infer_from_directory(&package_root.join("examples"))
135135
}
136136

137-
impl TomlTarget {
138-
fn validate_crate_type(&self) -> CargoResult<()> {
139-
// Per the Macros 1.1 RFC:
140-
//
141-
// > Initially if a crate is compiled with the proc-macro crate type
142-
// > (and possibly others) it will forbid exporting any items in the
143-
// > crate other than those functions tagged #[proc_macro_derive] and
144-
// > those functions must also be placed at the crate root.
145-
//
146-
// A plugin requires exporting plugin_registrar so a crate cannot be
147-
// both at once.
148-
if self.plugin == Some(true) && self.proc_macro() == Some(true) {
149-
Err("lib.plugin and lib.proc-macro cannot both be true".into())
150-
} else {
151-
Ok(())
152-
}
153-
}
154-
}
155-
156137
fn clean_lib(toml_lib: Option<&TomlLibTarget>,
157138
package_root: &Path,
158139
package_name: &str,
@@ -166,17 +147,10 @@ fn clean_lib(toml_lib: Option<&TomlLibTarget>,
166147
bail!("library target names cannot contain hyphens: {}", name)
167148
}
168149
}
169-
170-
lib.validate_crate_type()?;
171-
Some(
172-
TomlTarget {
173-
name: lib.name.clone().or(Some(package_name.to_owned())),
174-
path: lib.path.clone().or_else(
175-
|| inferred.as_ref().map(|p| PathValue(p.clone()))
176-
),
177-
..lib.clone()
178-
}
179-
)
150+
Some(TomlTarget {
151+
name: lib.name.clone().or(Some(package_name.to_owned())),
152+
..lib.clone()
153+
})
180154
}
181155
None => inferred.as_ref().map(|lib| {
182156
TomlTarget {
@@ -218,16 +192,21 @@ fn clean_lib(toml_lib: Option<&TomlLibTarget>,
218192
}
219193
};
220194

221-
let crate_types = match lib.crate_types() {
222-
Some(kinds) => kinds.iter().map(|s| LibKind::from_str(s)).collect(),
223-
None => {
224-
let lib_kind = match (lib.plugin, lib.proc_macro()) {
225-
(Some(true), _) => LibKind::Dylib,
226-
(_, Some(true)) => LibKind::ProcMacro,
227-
_ => LibKind::Lib
228-
};
229-
vec![lib_kind]
230-
}
195+
// Per the Macros 1.1 RFC:
196+
//
197+
// > Initially if a crate is compiled with the proc-macro crate type
198+
// > (and possibly others) it will forbid exporting any items in the
199+
// > crate other than those functions tagged #[proc_macro_derive] and
200+
// > those functions must also be placed at the crate root.
201+
//
202+
// A plugin requires exporting plugin_registrar so a crate cannot be
203+
// both at once.
204+
let crate_types = match (lib.crate_types(), lib.plugin, lib.proc_macro()) {
205+
(_, Some(true), Some(true)) => bail!("lib.plugin and lib.proc-macro cannot both be true"),
206+
(Some(kinds), _, _) => kinds.iter().map(|s| LibKind::from_str(s)).collect(),
207+
(None, Some(true), _) => vec![LibKind::Dylib],
208+
(None, _, Some(true)) => vec![LibKind::ProcMacro],
209+
(None, _, _) => vec![LibKind::Lib],
231210
};
232211

233212
let mut target = Target::lib_target(&lib.name(), crate_types, path);
@@ -317,40 +296,20 @@ fn clean_bins(toml_bins: Option<&Vec<TomlBinTarget>>,
317296
fn clean_examples(toml_examples: Option<&Vec<TomlExampleTarget>>,
318297
package_root: &Path)
319298
-> CargoResult<Vec<Target>> {
320-
let inferred = inferred_examples(package_root);
321-
let examples = match toml_examples {
322-
Some(examples) => examples.clone(),
323-
None => inferred.iter().map(|&(ref name, ref path)| {
324-
TomlTarget {
325-
name: Some(name.clone()),
326-
path: Some(PathValue(path.clone())),
327-
..TomlTarget::new()
328-
}
329-
}).collect()
330-
};
331-
332-
for target in examples.iter() {
333-
validate_has_name(target, "example", "example")?;
334-
}
335-
336-
validate_unique_names(&examples, "example")?;
299+
let targets = clean_targets("example", "example",
300+
toml_examples, inferred_examples(package_root),
301+
package_root)?;
337302

338303
let mut result = Vec::new();
339-
for ex in examples.iter() {
340-
let path = target_path(ex, &inferred, "example", package_root)?;
341-
342-
let crate_types = match ex.crate_types() {
304+
for (path, toml) in targets {
305+
let crate_types = match toml.crate_types() {
343306
Some(kinds) => kinds.iter().map(|s| LibKind::from_str(s)).collect(),
344307
None => Vec::new()
345308
};
346309

347-
let mut target = Target::example_target(
348-
&ex.name(),
349-
crate_types,
350-
path,
351-
ex.required_features.clone()
352-
);
353-
configure(ex, &mut target);
310+
let mut target = Target::example_target(&toml.name(), crate_types, path,
311+
toml.required_features.clone());
312+
configure(&toml, &mut target);
354313
result.push(target);
355314
}
356315

@@ -359,41 +318,44 @@ fn clean_examples(toml_examples: Option<&Vec<TomlExampleTarget>>,
359318

360319
fn clean_tests(toml_tests: Option<&Vec<TomlTestTarget>>,
361320
package_root: &Path) -> CargoResult<Vec<Target>> {
362-
let inferred = inferred_tests(package_root);
363-
let tests = match toml_tests {
364-
Some(tests) => tests.clone(),
365-
None => inferred.iter().map(|&(ref name, ref path)| {
366-
TomlTarget {
367-
name: Some(name.clone()),
368-
path: Some(PathValue(path.clone())),
369-
..TomlTarget::new()
370-
}
371-
}).collect()
372-
};
321+
let targets = clean_targets("test", "test",
322+
toml_tests, inferred_tests(package_root),
323+
package_root)?;
373324

374-
for target in tests.iter() {
375-
validate_has_name(target, "test", "test")?;
325+
let mut result = Vec::new();
326+
for (path, toml) in targets {
327+
let mut target = Target::test_target(&toml.name(), path,
328+
toml.required_features.clone());
329+
configure(&toml, &mut target);
330+
result.push(target);
376331
}
332+
Ok(result)
333+
}
377334

378-
validate_unique_names(&tests, "test")?;
335+
fn clean_benches(toml_benches: Option<&Vec<TomlBenchTarget>>,
336+
package_root: &Path) -> CargoResult<Vec<Target>> {
337+
let targets = clean_targets("benchmark", "bench",
338+
toml_benches, inferred_benches(package_root),
339+
package_root)?;
379340

380341
let mut result = Vec::new();
381-
for test in tests.iter() {
382-
let path = target_path(test, &inferred, "test", package_root)?;
383-
384-
let mut target = Target::test_target(&test.name(), path,
385-
test.required_features.clone());
386-
configure(test, &mut target);
342+
for (path, toml) in targets {
343+
let mut target = Target::bench_target(&toml.name(), path,
344+
toml.required_features.clone());
345+
configure(&toml, &mut target);
387346
result.push(target);
388347
}
348+
389349
Ok(result)
390350
}
391351

392-
fn clean_benches(toml_benches: Option<&Vec<TomlBenchTarget>>,
393-
package_root: &Path) -> CargoResult<Vec<Target>> {
394-
let inferred = inferred_benches(package_root);
395-
let benches = match toml_benches {
396-
Some(benches) => benches.clone(),
352+
fn clean_targets(target_kind_human: &str, target_kind: &str,
353+
toml_targets: Option<&Vec<TomlTarget>>,
354+
inferred: Vec<(String, PathBuf)>,
355+
package_root: &Path)
356+
-> CargoResult<Vec<(PathBuf, TomlTarget)>> {
357+
let toml_targets = match toml_targets {
358+
Some(targets) => targets.clone(),
397359
None => inferred.iter().map(|&(ref name, ref path)| {
398360
TomlTarget {
399361
name: Some(name.clone()),
@@ -403,20 +365,15 @@ fn clean_benches(toml_benches: Option<&Vec<TomlBenchTarget>>,
403365
}).collect()
404366
};
405367

406-
for target in benches.iter() {
407-
validate_has_name(target, "benchmark", "bench")?;
368+
for target in toml_targets.iter() {
369+
validate_has_name(target, target_kind_human, target_kind)?;
408370
}
409371

410-
validate_unique_names(&benches, "bench")?;
411-
372+
validate_unique_names(&toml_targets, target_kind)?;
412373
let mut result = Vec::new();
413-
for bench in benches.iter() {
414-
let path = target_path(bench, &inferred, "bench", package_root)?;
415-
416-
let mut target = Target::bench_target(&bench.name(), path,
417-
bench.required_features.clone());
418-
configure(bench, &mut target);
419-
result.push(target);
374+
for target in toml_targets {
375+
let path = target_path(&target, &inferred, target_kind, package_root)?;
376+
result.push((path, target));
420377
}
421378
Ok(result)
422379
}
@@ -461,12 +418,14 @@ fn target_path(target: &TomlTarget,
461418
}
462419
}
463420

464-
fn validate_has_name(target: &TomlTarget, target_name: &str, target_kind: &str) -> CargoResult<()> {
421+
fn validate_has_name(target: &TomlTarget,
422+
target_kind_human: &str,
423+
target_kind: &str) -> CargoResult<()> {
465424
match target.name {
466425
Some(ref name) => if name.trim().is_empty() {
467-
bail!("{} target names cannot be empty", target_name)
426+
bail!("{} target names cannot be empty", target_kind_human)
468427
},
469-
None => bail!("{} target {}.name is required", target_name, target_kind)
428+
None => bail!("{} target {}.name is required", target_kind_human, target_kind)
470429
}
471430

472431
Ok(())

0 commit comments

Comments
 (0)