Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 6, 2023
1 parent a7619c1 commit 41edcdf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
49 changes: 20 additions & 29 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,9 @@ impl TrashContext {
/// Removes all files and folder paths recursively.
pub(crate) fn delete_all_canonicalized(&self, full_paths: Vec<PathBuf>) -> Result<(), Error> {
let mut collection = Vec::new();
Self::collect_all_canonicalized_paths_recursive(full_paths, &mut collection)?;
traverse_paths_recursively(full_paths, &mut collection)?;
self.delete_specified_canonicalized(collection)
}

/// Collects all paths in the given files and folders recursively.
///
/// # Arguments
/// 1. List of paths to remove.
/// 2. List to collect all the paths into.
fn collect_all_canonicalized_paths_recursive(
full_paths: Vec<PathBuf>,
collection: &mut Vec<PathBuf>,
) -> Result<(), Error> {
for base_path in full_paths.into_iter() {
if base_path.is_file() {
collection.push(base_path);
continue;
}

// directory
for entry in fs::read_dir(&base_path).unwrap() {
let entry = entry.unwrap();
let path = base_path.join(entry.file_name());
Self::collect_all_canonicalized_paths_recursive(Vec::from([path]), collection)?;
}

collection.push(base_path);
}

Ok(())
}
}

pub fn list() -> Result<Vec<TrashItem>, Error> {
Expand Down Expand Up @@ -292,3 +264,22 @@ thread_local! {
fn ensure_com_initialized() {
CO_INITIALIZER.with(|_| {});
}

fn traverse_paths_recursively(
paths: impl IntoIterator<Item = PathBuf>,
collection: &mut Vec<PathBuf>,
) -> Result<(), Error> {
for base_path in paths {
if base_path.is_file() {
collection.push(base_path);
continue;
}

for entry in fs::read_dir(&base_path).map_err(|err| Error::Unknown { description: err.to_string() })? {
let entry = entry.map_err(|err| Error::Unknown { description: err.to_string() })?;
traverse_paths_recursively(Some(entry.path()), collection)?;
}
collection.push(base_path);
}
Ok(())
}
5 changes: 1 addition & 4 deletions tests/trash.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fs::{create_dir, File};
use std::io::Write;
use std::path::{Path, PathBuf};

use log::trace;
Expand Down Expand Up @@ -161,9 +160,7 @@ fn recursive_file_with_content_deletion() {
std::fs::create_dir_all(&dir1).unwrap();
std::fs::create_dir_all(&dir2).unwrap();
File::create(dir1.join("same-name")).unwrap();
let file = File::create(dir2.join("same-name")).unwrap();
let mut file = std::io::LineWriter::new(file);
file.write_all(b"some content").unwrap();
std::fs::write(dir2.join("same-name"), b"some content").unwrap();

trash::delete(parent_dir).unwrap();
assert!(!parent_dir.exists());
Expand Down

0 comments on commit 41edcdf

Please sign in to comment.