workspacer_cleanup/
cleanup_crate.rs

1// ---------------- [ File: workspacer-cleanup/src/cleanup_crate.rs ]
2crate::ix!();
3
4// 1) Introduce a new trait for cleaning up a single crate:
5#[async_trait]
6pub trait CleanupCrate {
7    async fn cleanup_crate(&self) -> Result<(), WorkspaceError>;
8}
9
10#[async_trait]
11impl CleanupCrate for CrateHandle {
12    async fn cleanup_crate(&self) -> Result<(), WorkspaceError> {
13        let crate_path = self.root_dir_path_buf();
14        let target_dir = crate_path.join("target");
15        let lock_file  = crate_path.join("Cargo.lock");
16
17        info!("cleanup_crate for '{}'", self.name());
18        // Remove target/
19        if fs::metadata(&target_dir).await.is_ok() {
20            fs::remove_dir_all(&target_dir)
21                .await
22                .map_err(|_| WorkspaceError::DirectoryRemovalError)?;
23            info!("Removed '{:?}'", target_dir);
24        }
25
26        // Remove Cargo.lock
27        if fs::metadata(&lock_file).await.is_ok() {
28            fs::remove_file(&lock_file)
29                .await
30                .map_err(|_| WorkspaceError::FileRemovalError)?;
31            info!("Removed '{:?}'", lock_file);
32        }
33
34        Ok(())
35    }
36}