Skip to content

Commit

Permalink
Reorganized code for cross-platform compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
bicarlsen committed Jul 5, 2023
1 parent 46e0697 commit 1c09e48
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 55 deletions.
53 changes: 1 addition & 52 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,42 +109,7 @@ impl TrashContext {
trace!("Starting canonicalize_paths");
let full_paths = canonicalize_paths(paths)?;
trace!("Finished canonicalize_paths");

if cfg!(target_os = "windows") {
self.delete_recursive(full_paths)
} else {
self.delete_all_canonicalized(full_paths)
}
}

/// **window only**
///
/// Removes all files/directories specified by the collection of paths provided as an argument, and all sub files and folders recursively.
///
/// When a symbolic link is provided to this function, the symbolic link will be removed and the link
/// target will be kept intact.
///
/// # Example
///
/// ```
/// use std::fs::File;
/// use trash::delete_recursive;
/// File::create("delete_me_1").unwrap();
/// File::create("delete_me_2").unwrap();
/// delete_recursive(&["delete_me_1", "delete_me_2"]).unwrap();
/// assert!(File::open("delete_me_1").is_err());
/// assert!(File::open("delete_me_2").is_err());
/// ```
#[cfg(target_os = "windows")]
pub fn delete_recursive<I, T>(&self, paths: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<Path>,
{
trace!("Starting canonicalize_paths");
let full_paths = canonicalize_paths(paths)?;
trace!("Finished canonicalize_paths");
self.delete_all_canonicalized_recursive(full_paths)
self.delete_all_canonicalized(full_paths)
}
}

Expand All @@ -166,23 +131,7 @@ where
DEFAULT_TRASH_CTX.delete_all(paths)
}

/// **window only**
///
/// Convenience method for `DEFAULT_TRASH_CTX.delete_recursive()`.
///
/// See: [`TrashContext::delete_recursive`](TrashContext::delete_recursive)
#[cfg(target_os = "windows")]
pub fn delete_recursive<I, T>(paths: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: AsRef<Path>,
{
DEFAULT_TRASH_CTX.delete_recursive(paths)
}

///
/// Provides information about an error.
///
#[derive(Debug)]
pub enum Error {
Unknown {
Expand Down
6 changes: 3 additions & 3 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl PlatformTrashContext {
}
impl TrashContext {
/// See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-_shfileopstructa
pub(crate) fn delete_all_canonicalized(&self, full_paths: Vec<PathBuf>) -> Result<(), Error> {
pub(crate) fn delete_specified_canonicalized(&self, full_paths: Vec<PathBuf>) -> Result<(), Error> {
ensure_com_initialized();
unsafe {
let pfo: IFileOperation = CoCreateInstance(&FileOperation as *const _, None, CLSCTX_ALL).unwrap();
Expand All @@ -70,10 +70,10 @@ impl TrashContext {
}

/// Removes all files and folder paths recursively.
pub(crate) fn delete_all_canonicalized_recursive(&self, full_paths: Vec<PathBuf>) -> Result<(), Error> {
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)?;
self.delete_all_canonicalized(collection)
self.delete_specified_canonicalized(collection)
}

/// Collects all paths in the given files and folders recursively.
Expand Down

0 comments on commit 1c09e48

Please sign in to comment.