Skip to content

Commit 8236370

Browse files
committed
chore: add protections to remove_version, add docs
1 parent b8499f8 commit 8236370

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/repo/versions.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ pub fn switch_version(repo_path: &Path, hash: &str, package_id: &str) -> Result<
6565
}
6666

6767
/// Gets all versions for the `package_id`
68+
///
69+
/// # Errors
70+
///
71+
/// - Filesystem Read Errors (Permissions, etc)
72+
///
73+
/// # Returns
74+
///
75+
/// A `vec` containing `String`s of all version hashes
6876
pub fn get_versions(repo_path: &Path, package_id: &str) -> Result<Vec<String>> {
6977
let mut versions = Vec::new();
7078

@@ -86,9 +94,18 @@ pub fn get_versions(repo_path: &Path, package_id: &str) -> Result<Vec<String>> {
8694
}
8795

8896
/// Removes a version of a package.
89-
pub fn remove_version(repo_path: &Path, hash: String, package_id: &str) -> Result<()> {
90-
let path = repo_path.join(format!("versions/{}-{}", package_id, hash));
91-
fs::remove_dir_all(path)?;
92-
93-
Ok(())
97+
///
98+
/// # Errors
99+
///
100+
/// - Version is not installed for the package
101+
/// - Filesystem Write Errors (Permissions, etc)
102+
pub fn remove_version(repo_path: &Path, hash: &str, package_id: &str) -> Result<()> {
103+
let path = repo_path.join(format!("versions/{package_id}-{hash}"));
104+
105+
if path.exists() {
106+
fs::remove_dir_all(path)?;
107+
Ok(())
108+
} else {
109+
anyhow::bail!("The version {hash} is not installed for package {package_id}")
110+
}
94111
}

0 commit comments

Comments
 (0)