Skip to content

Commit 010280f

Browse files
authored
fix(util): improve remove_all_files_except function (#141)
- Address inconsistent behavior of remove_all_files function by saving partial results in a vector before removing them - EXTRA: clippy --fix applied to project
1 parent 33ee24e commit 010280f

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn get_java_home(configuration: &Option<Value>, worktree: &Worktree) -> Opti
2121
match expand_home_path(worktree, java_home.to_string()) {
2222
Ok(home_path) => return Some(home_path),
2323
Err(err) => {
24-
println!("{}", err);
24+
println!("{err}");
2525
}
2626
};
2727
}
@@ -86,7 +86,7 @@ pub fn get_jdtls_launcher(configuration: &Option<Value>, worktree: &Worktree) ->
8686
match expand_home_path(worktree, launcher_path.to_string()) {
8787
Ok(path) => return Some(path),
8888
Err(err) => {
89-
println!("{}", err);
89+
println!("{err}");
9090
}
9191
}
9292
}
@@ -103,7 +103,7 @@ pub fn get_lombok_jar(configuration: &Option<Value>, worktree: &Worktree) -> Opt
103103
match expand_home_path(worktree, jar_path.to_string()) {
104104
Ok(path) => return Some(path),
105105
Err(err) => {
106-
println!("{}", err);
106+
println!("{err}");
107107
}
108108
}
109109
}
@@ -120,7 +120,7 @@ pub fn get_java_debug_jar(configuration: &Option<Value>, worktree: &Worktree) ->
120120
match expand_home_path(worktree, jar_path.to_string()) {
121121
Ok(path) => return Some(path),
122122
Err(err) => {
123-
println!("{}", err);
123+
println!("{err}");
124124
}
125125
}
126126
}

src/debugger.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ impl Debugger {
157157
)
158158
.map_err(|err| {
159159
format!(
160-
"Failed to download java-debug fork from {}: {err}",
161-
JAVA_DEBUG_PLUGIN_FORK_URL
160+
"Failed to download java-debug fork from {JAVA_DEBUG_PLUGIN_FORK_URL}: {err}"
162161
)
163162
})?;
164163

@@ -198,8 +197,7 @@ impl Debugger {
198197
}
199198

200199
println!(
201-
"Could not fetch debugger: {}\nFalling back to local version.",
202-
err
200+
"Could not fetch debugger: {err}\nFalling back to local version."
203201
);
204202

205203
let exists = fs::read_dir(prefix)

src/jdtls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn find_equinox_launcher(jdtls_base_directory: &Path) -> Result<PathBuf, String>
283283

284284
// else get the first file that matches the glob 'org.eclipse.equinox.launcher_*.jar'
285285
let entries =
286-
read_dir(&plugins_dir).map_err(|e| format!("Failed to read plugins directory: {}", e))?;
286+
read_dir(&plugins_dir).map_err(|e| format!("Failed to read plugins directory: {e}"))?;
287287

288288
entries
289289
.filter_map(Result::ok)
@@ -325,7 +325,7 @@ fn get_jdtls_data_path(worktree: &Worktree) -> zed::Result<PathBuf> {
325325
let cache_key = worktree.root_path();
326326

327327
let hex_digest = get_sha1_hex(&cache_key);
328-
let unique_dir_name = format!("jdtls-{}", hex_digest);
328+
let unique_dir_name = format!("jdtls-{hex_digest}");
329329
Ok(base_cachedir.join(unique_dir_name))
330330
}
331331

src/lsp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl LspClient {
118118
fn string_to_hex(s: &str) -> String {
119119
let mut hex_string = String::new();
120120
for byte in s.as_bytes() {
121-
hex_string.push_str(&format!("{:02x}", byte));
121+
hex_string.push_str(&format!("{byte:02x}"));
122122
}
123123
hex_string
124124
}

src/util.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const EXPAND_ERROR: &str = "Failed to expand ~";
2020
const CURR_DIR_ERROR: &str = "Could not get current dir";
2121
const DIR_ENTRY_LOAD_ERROR: &str = "Failed to load directory entry";
2222
const DIR_ENTRY_RM_ERROR: &str = "Failed to remove directory entry";
23-
const DIR_ENTRY_LS_ERROR: &str = "Failed to list prefix directory";
23+
const ENTRY_TYPE_ERROR: &str = "Could not determine entry type";
24+
const FILE_ENTRY_RM_ERROR: &str = "Failed to remove file entry";
2425
const PATH_TO_STR_ERROR: &str = "Failed to convert path to string";
2526
const JAVA_EXEC_ERROR: &str = "Failed to convert Java executable path to string";
2627
const JAVA_VERSION_ERROR: &str = "Failed to determine Java major version";
@@ -49,7 +50,7 @@ pub fn create_path_if_not_exists<P: AsRef<Path>>(path: P) -> zed::Result<()> {
4950
if metadata.is_dir() {
5051
Ok(())
5152
} else {
52-
Err(format!("{PATH_IS_NOT_DIR}: {:?}", path_ref))
53+
Err(format!("{PATH_IS_NOT_DIR}: {path_ref:?}"))
5354
}
5455
}
5556
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
@@ -280,22 +281,31 @@ pub fn path_to_string<P: AsRef<Path>>(path: P) -> zed::Result<String> {
280281
///
281282
/// Returns `Ok(())` on success, even if some removals fail (errors are printed to stdout).
282283
pub fn remove_all_files_except<P: AsRef<Path>>(prefix: P, filename: &str) -> zed::Result<()> {
283-
match fs::read_dir(prefix) {
284-
Ok(entries) => {
285-
for entry in entries {
286-
match entry {
287-
Ok(entry) => {
288-
if entry.file_name().to_str() != Some(filename)
289-
&& let Err(err) = fs::remove_dir_all(entry.path())
290-
{
291-
println!("{msg}: {err}", msg = DIR_ENTRY_RM_ERROR, err = err);
292-
}
284+
let entries: Vec<_> = match fs::read_dir(prefix) {
285+
Ok(entries) => entries.filter_map(|e| e.ok()).collect(),
286+
Err(err) => {
287+
println!("{DIR_ENTRY_LOAD_ERROR}: {err}");
288+
return Err(format!("{DIR_ENTRY_LOAD_ERROR}: {err}"));
289+
}
290+
};
291+
292+
for entry in entries {
293+
if entry.file_name().to_str() != Some(filename) {
294+
match entry.file_type() {
295+
Ok(t) => {
296+
if t.is_dir()
297+
&& let Err(err) = fs::remove_dir_all(entry.path())
298+
{
299+
println!("{DIR_ENTRY_RM_ERROR}: {err}");
300+
} else if t.is_file()
301+
&& let Err(err) = fs::remove_file(entry.path())
302+
{
303+
println!("{FILE_ENTRY_RM_ERROR}: {err}");
293304
}
294-
Err(err) => println!("{msg}: {err}", msg = DIR_ENTRY_LOAD_ERROR, err = err),
295305
}
306+
Err(type_err) => println!("{ENTRY_TYPE_ERROR}: {type_err}"),
296307
}
297308
}
298-
Err(err) => println!("{msg}: {err}", msg = DIR_ENTRY_LS_ERROR, err = err),
299309
}
300310

301311
Ok(())
@@ -328,8 +338,7 @@ pub fn should_use_local_or_download(
328338
CheckUpdates::Never => match local {
329339
Some(path) => Ok(Some(path)),
330340
None => Err(format!(
331-
"{} for {}",
332-
NO_LOCAL_INSTALL_NEVER_ERROR, component_name
341+
"{NO_LOCAL_INSTALL_NEVER_ERROR} for {component_name}"
333342
)),
334343
},
335344
CheckUpdates::Once => Ok(local),

0 commit comments

Comments
 (0)