Skip to content

Commit

Permalink
Tweaked tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturKovacs committed Apr 19, 2021
1 parent 18337bf commit 330b1ec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
12 changes: 6 additions & 6 deletions examples/delete_method.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::fs::File;
use trash::TrashContext;

#[cfg(target_os = "macos")]
use trash::macos::{DeleteMethod, TrashContextExtMacos};

#[cfg(not(target_os = "macos"))]
fn main() {
println!("This example is only available on macOS");
}

#[cfg(target_os = "macos")]
fn main() {
use std::fs::File;
use trash::{
macos::{DeleteMethod, TrashContextExtMacos},
TrashContext,
};

env_logger::init();

let mut trash_ctx = TrashContext::default();
Expand Down
42 changes: 23 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,42 +233,46 @@ where

/// This struct holds information about a single item within the trash.
///
/// Some functions associated with this struct are defined in the `TrahsItemPlatformDep` trait.
/// That trait is implemented for `TrashItem` by each platform specific source file individually.
/// Some functions associated with this struct are defined in the
/// `TrahsItemPlatformDep` trait. That trait is implemented for `TrashItem` by
/// each platform specific source file individually.
///
/// A trahs item can be a file or folder or any other object that the target operating system
/// allows to put into the trash.
/// A trahs item can be a file or folder or any other object that the target
/// operating system allows to put into the trash.
#[derive(Debug)]
pub struct TrashItem {
/// A system specific identifier of the item in the trash.
///
/// On Windows it is the string returned by `IShellFolder::GetDisplayNameOf` with the
/// `SHGDN_FORPARSING` flag.
/// On Windows it is the string returned by `IShellFolder::GetDisplayNameOf`
/// with the `SHGDN_FORPARSING` flag.
///
/// On Linux it is an absolute path to the `.trashinfo` file associated with the item.
/// On Linux it is an absolute path to the `.trashinfo` file associated with
/// the item.
pub id: OsString,

/// The name of the item. For example if the folder '/home/user/New Folder' was deleted,
/// its `name` is 'New Folder'
/// The name of the item. For example if the folder '/home/user/New Folder'
/// was deleted, its `name` is 'New Folder'
pub name: String,

/// The path to the parent folder of this item before it was put inside the trash.
/// For example if the folder '/home/user/New Folder' is in the trash, its `original_parent`
/// is '/home/user'.
/// The path to the parent folder of this item before it was put inside the
/// trash. For example if the folder '/home/user/New Folder' is in the
/// trash, its `original_parent` is '/home/user'.
///
/// To get the full path to the file in its original location use the `original_path`
/// function.
/// To get the full path to the file in its original location use the
/// `original_path` function.
pub original_parent: PathBuf,

/// The date and time in UNIX Epoch time when the item was put into the trash.
/// The number of non-leap seconds elapsed between the UNIX Epoch and the
/// moment the file was deleted.
pub time_deleted: i64,
}

/// Platform independent functions of `TrashItem`.
///
/// See `TrahsItemPlatformDep` for platform dependent functions.
impl TrashItem {
/// Joins the `original_parent` and `name` fields to obtain the full path to the original file.
/// Joins the `original_parent` and `name` fields to obtain the full path to
/// the original file.
pub fn original_path(&self) -> PathBuf {
self.original_parent.join(&self.name)
}
Expand Down Expand Up @@ -315,7 +319,7 @@ pub mod os_limited {
/// # Example
///
/// ```
/// use trash::extra::list;
/// use trash::os_limited::list;
/// let trash_items = list().unwrap();
/// println!("{:#?}", trash_items);
/// ```
Expand All @@ -331,7 +335,7 @@ pub mod os_limited {
///
/// ```
/// use std::fs::File;
/// use trash::{delete, extra::{list, purge_all}};
/// use trash::{delete, os_limited::{list, purge_all}};
/// let filename = "trash-purge_all-example";
/// File::create(filename).unwrap();
/// delete(filename).unwrap();
Expand Down Expand Up @@ -367,7 +371,7 @@ pub mod os_limited {
///
/// ```
/// use std::fs::File;
/// use trash::extra::{list, restore_all};
/// use trash::os_limited::{list, restore_all};
/// let filename = "trash-restore_all-example";
/// File::create(filename).unwrap();
/// restore_all(list().unwrap().into_iter().filter(|x| x.name == filename)).unwrap();
Expand Down
19 changes: 18 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ mod os_limited {

#[test]
fn list() {
const MAX_SECONDS_DIFFERENCE: i64 = 10;
init_logging();

let deletion_time = chrono::Utc::now();
let actual_unix_deletion_time = deletion_time.naive_utc().timestamp();
assert_eq!(actual_unix_deletion_time, deletion_time.naive_local().timestamp());
let file_name_prefix = get_unique_name();
let batches: usize = 2;
let files_per_batch: usize = 3;
Expand Down Expand Up @@ -159,7 +164,19 @@ mod os_limited {
});
for name in names {
match items.get(&name) {
Some(items) => assert_eq!(items.len(), batches),
Some(items) => {
assert_eq!(items.len(), batches);
for item in items {
let diff = (item.time_deleted - actual_unix_deletion_time).abs();
if diff > MAX_SECONDS_DIFFERENCE {
panic!(
"The deleted item does not have the timestamp that represents its deletion time. Expected: {}. Got: {}",
actual_unix_deletion_time,
item.time_deleted
);
}
}
}
None => panic!("ERROR Could not find '{}' in {:#?}", name, items),
}
}
Expand Down

0 comments on commit 330b1ec

Please sign in to comment.