From 2db7fb03db0a642ad543b1cfefeb02d7d1bd4276 Mon Sep 17 00:00:00 2001 From: Juan Pablo Miceli Date: Wed, 3 May 2023 01:14:16 -0300 Subject: [PATCH] Add method to export trash path (issue #38) --- src/freedesktop.rs | 15 +++++++++++++++ src/lib.rs | 7 +++++++ tests/trash.rs | 16 +++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/freedesktop.rs b/src/freedesktop.rs index 331eaa8..17ffe97 100644 --- a/src/freedesktop.rs +++ b/src/freedesktop.rs @@ -49,6 +49,21 @@ impl TrashContext { } Ok(()) } + + /// Retrieves the path of the Trash directory. + /// + /// # Example + /// + /// ``` + /// use std::fs::File; + /// use trash::{ delete, trash_path }; + /// File::create("mv_to_trash").unwrap(); + /// delete("mv_to_trash").unwrap(); + /// assert!(trash_path().unwrap().join("files").join("mv_to_trash").exists()) + /// ``` + pub fn trash_path(&self) -> Result { + home_trash() + } } pub fn list() -> Result, Error> { diff --git a/src/lib.rs b/src/lib.rs index fbbb7ee..415d80d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,6 +113,13 @@ impl TrashContext { } } +/// Convenience method for `DEFAULT_TRASH_CTX.trash_path()` +/// +/// See: [`TrashContext::trash_path`](TrashContext::trash_path) +pub fn trash_path() -> Result { + DEFAULT_TRASH_CTX.trash_path() +} + /// Convenience method for `DEFAULT_TRASH_CTX.delete()`. /// /// See: [`TrashContext::delete`](TrashContext::delete) diff --git a/tests/trash.rs b/tests/trash.rs index c0d29f3..1240d07 100644 --- a/tests/trash.rs +++ b/tests/trash.rs @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf}; use log::trace; use serial_test::serial; -use trash::{delete, delete_all}; +use trash::{delete, delete_all, trash_path}; mod util { use std::sync::atomic::{AtomicI64, Ordering}; @@ -40,6 +40,20 @@ fn test_delete_file() { trace!("Finished test_delete_file"); } +#[test] +#[serial] +fn test_trash_path() { + init_logging(); + trace!("Started test_trash_path"); + + let path = PathBuf::from(get_unique_name()); + File::create(&path).unwrap(); + + delete(&path).unwrap(); + assert!(trash_path().unwrap().join("files").join(path).exists()); + trace!("Finished test_trash_path"); +} + #[test] #[serial] fn test_delete_folder() {