workspacer_cli/name.rs
1// ---------------- [ File: workspacer-cli/src/name.rs ]
2crate::ix!();
3
4#[derive(Debug, StructOpt)]
5pub enum NameSubcommand {
6 /// Name all files in a single crate (e.g., `--crate some/path/to/crate`)
7 Crate {
8 #[structopt(long = "crate")]
9 crate_name: PathBuf,
10
11 #[structopt(long = "skip-git-check")]
12 skip_git_check: bool,
13 },
14
15 /// Name all files in an entire workspace (e.g., `--path some/path/to/workspace`)
16 Workspace {
17 #[structopt(long = "path")]
18 path: PathBuf,
19
20 #[structopt(long = "skip-git-check")]
21 skip_git_check: bool,
22 },
23}
24
25impl NameSubcommand {
26 pub async fn run(&self) -> Result<(), WorkspaceError> {
27 match self {
28 // --------------------------------------
29 // 1) Single Crate
30 // --------------------------------------
31 NameSubcommand::Crate { crate_name, skip_git_check } => {
32 trace!("Naming all .rs files in single crate at '{}'", crate_name.display());
33
34 // We'll use our standard `run_with_crate` helper
35 // to construct a CrateHandle, optionally check Git, then run our closure.
36 run_with_crate(crate_name.clone(), *skip_git_check, move |handle| {
37 Box::pin(async move {
38 // Using the `NameAllFiles` trait implemented for CrateHandle
39 handle.name_all_files().await.map_err(|crate_err| {
40 error!("Error naming all files in crate='{}': {:?}", handle.name(), crate_err);
41 // Wrap or convert into WorkspaceError as needed
42 WorkspaceError::CrateError(crate_err)
43 })?;
44
45 info!("Successfully named all files in crate='{}'", handle.name());
46 Ok(())
47 })
48 })
49 .await
50 }
51
52 // --------------------------------------
53 // 2) Entire Workspace
54 // --------------------------------------
55 NameSubcommand::Workspace { path, skip_git_check } => {
56 trace!("Naming all .rs files in workspace at '{}'", path.display());
57
58 // We'll use `run_with_workspace` so that we automatically load the workspace,
59 // optionally check Git cleanliness, etc.
60 run_with_workspace(Some(path.clone()), *skip_git_check, move |ws| {
61 Box::pin(async move {
62 // The trait `NameAllFiles` is also impl’d for `Workspace<P,H>`.
63 ws.name_all_files().await.map_err(|we| {
64 error!("Error naming all files in workspace at '{}': {:?}", ws.as_ref().display(), we);
65 we
66 })?;
67
68 info!(
69 "Successfully named all .rs files in workspace at '{}'",
70 ws.as_ref().display()
71 );
72 Ok(())
73 })
74 })
75 .await
76 }
77 }
78 }
79}