workspacer_cli/
detect_cycles_crate.rs

1// ---------------- [ File: workspacer-cli/src/detect_cycles_crate.rs ]
2crate::ix!();
3
4#[derive(Debug, StructOpt, Getters, Setters)]
5#[getset(get="pub")]
6pub struct DetectCyclesCrateCommand {
7    /// Name of the crate we want to ensure is present
8    #[structopt(long = "crate")]
9    crate_name: String,
10
11    /// If provided, custom workspace path
12    #[structopt(long = "workspace")]
13    workspace_path: Option<PathBuf>,
14
15    /// If true, we print extra logs
16    #[structopt(long = "verbose")]
17    verbose: bool,
18
19    /// If true, skip the Git clean check
20    #[structopt(long = "skip-git-check")]
21    skip_git_check: bool,
22}
23
24impl DetectCyclesCrateCommand {
25    pub async fn run(&self) -> Result<(), WorkspaceError> {
26        // Clone out each needed field so that the closure can be `'static`:
27        let crate_name_owned = self.crate_name.clone();
28        let workspace_path_owned = self.workspace_path.clone();
29        let skip_git_check_flag = self.skip_git_check;
30        let verbose_flag = self.verbose;
31
32        run_with_workspace_and_crate_name(
33            workspace_path_owned,
34            skip_git_check_flag,
35            crate_name_owned,
36            move |ws, name| {
37                Box::pin(async move {
38                    if verbose_flag {
39                        info!("Verifying crate='{}' is in workspace, then checking for cycles ...", name);
40                    }
41                    // The crate is guaranteed present by `run_with_workspace_and_crate_name`.
42                    // Next do the entire workspace check:
43                    ws.detect_circular_dependencies().await.map_err(|err| {
44                        error!("Failed detecting cycles in workspace: {:?}", err);
45                        err
46                    })?;
47
48                    if verbose_flag {
49                        info!("No circular dependencies detected in the workspace containing crate='{}'!", name);
50                    } else {
51                        println!("No circular dependencies found (crate='{}').", name);
52                    }
53
54                    Ok(())
55                })
56            },
57        )
58        .await
59    }
60}