1//! Cargo flag for selecting the relevant crate.
2
3use std::path;
4
5#[derive(Default, Clone, Debug, PartialEq, Eq)]
6#[cfg_attr(feature = "clap", derive(clap::Args))]
7#[non_exhaustive]
8pub struct Manifest {
9 #[cfg_attr(feature = "clap", arg(long, name = "PATH"))]
10 /// Path to Cargo.toml
11 pub manifest_path: Option<path::PathBuf>,
12}
13
14#[cfg(feature = "cargo_metadata")]
15impl Manifest {
16 /// Create a `cargo_metadata::MetadataCommand`
17 ///
18 /// Note: Requires the features `cargo_metadata`.
19 pub fn metadata(&self) -> cargo_metadata::MetadataCommand {
20 let mut c = cargo_metadata::MetadataCommand::new();
21 if let Some(ref manifest_path) = self.manifest_path {
22 c.manifest_path(manifest_path);
23 }
24 c
25 }
26}
27
28#[cfg(test)]
29mod test {
30 use super::*;
31
32 #[test]
33 #[cfg(feature = "clap")]
34 fn verify_app() {
35 #[derive(Debug, clap::Parser)]
36 struct Cli {
37 #[command(flatten)]
38 manifest: Manifest,
39 }
40
41 use clap::CommandFactory;
42 Cli::command().debug_assert()
43 }
44
45 #[cfg(feature = "cargo_metadata")]
46 #[test]
47 fn metadata_with_path() {
48 let manifest = Manifest {
49 manifest_path: Some(path::PathBuf::from("tests/fixtures/simple/Cargo.toml")),
50 };
51 let metadata = manifest.metadata();
52 metadata.exec().unwrap();
53 // TODO verify we forwarded correctly.
54 }
55
56 #[cfg(feature = "cargo_metadata")]
57 #[test]
58 fn metadata_without_path() {
59 let cwd = path::PathBuf::from("tests/fixtures/simple");
60 let manifest = Manifest {
61 manifest_path: None,
62 };
63 let mut metadata = manifest.metadata();
64 metadata.current_dir(cwd).exec().unwrap();
65 // TODO verify we forwarded correctly.
66 }
67}
68