-
Notifications
You must be signed in to change notification settings - Fork 33
added: restore_single TrashItem that can restore to arbitrary destination and force restore #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
292910d
0331e20
04eff70
209f500
84e492e
d552156
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -343,6 +343,13 @@ pub struct TrashItemMetadata { | |
| pub size: TrashItemSize, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Default)] | ||
| pub enum RestoreMode { | ||
| Force, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variants could probably be less abstract by spelling out more closely what they do. |
||
| #[default] | ||
| Soft, | ||
| } | ||
|
|
||
| #[cfg(any( | ||
| target_os = "windows", | ||
| all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")) | ||
|
|
@@ -357,7 +364,7 @@ pub mod os_limited { | |
| hash::{Hash, Hasher}, | ||
| }; | ||
|
|
||
| use super::{platform, Error, TrashItem, TrashItemMetadata}; | ||
| use super::{platform, Error, RestoreMode, TrashItem, TrashItemMetadata}; | ||
|
|
||
| /// Returns all [`TrashItem`]s that are currently in the trash. | ||
| /// | ||
|
|
@@ -493,30 +500,31 @@ pub mod os_limited { | |
| /// ``` | ||
| /// use std::fs::File; | ||
| /// use trash::os_limited::{list, restore_all}; | ||
| /// use trash::RestoreMode; | ||
| /// | ||
| /// let filename = "trash-restore_all-example"; | ||
| /// File::create_new(filename).unwrap(); | ||
| /// restore_all(list().unwrap().into_iter().filter(|x| x.name == filename)).unwrap(); | ||
| /// restore_all(list().unwrap().into_iter().filter(|x| x.name == filename), RestoreMode::Soft).unwrap(); | ||
| /// std::fs::remove_file(filename).unwrap(); | ||
| /// ``` | ||
| /// | ||
| /// Retry restoring when encountering [`RestoreCollision`] error: | ||
| /// | ||
| /// ```no_run | ||
| /// use trash::os_limited::{list, restore_all}; | ||
| /// use trash::Error::RestoreCollision; | ||
| /// use trash::{RestoreMode, Error::RestoreCollision}; | ||
| /// | ||
| /// let items = list().unwrap(); | ||
| /// if let Err(RestoreCollision { path, mut remaining_items }) = restore_all(items) { | ||
| /// if let Err(RestoreCollision { path, mut remaining_items }) = restore_all(items, RestoreMode::Soft) { | ||
| /// // keep all except the one(s) that couldn't be restored | ||
| /// remaining_items.retain(|e| e.original_path() != path); | ||
| /// restore_all(remaining_items).unwrap(); | ||
| /// restore_all(remaining_items, RestoreMode::Soft).unwrap(); | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// [`RestoreCollision`]: Error::RestoreCollision | ||
| /// [`RestoreTwins`]: Error::RestoreTwins | ||
| pub fn restore_all<I>(items: I) -> Result<(), Error> | ||
| pub fn restore_all<I>(items: I, mode: RestoreMode) -> Result<(), Error> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this can be a breaking change, after all this is only implemented on one platform.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I totally agree. I went this route because I noticed that the same functions on Windows and Freedesktop are routed to I did consider adding configuration attributes specifically for Freedesktop, but since there wasn’t a precedent for that, I wasn’t quite sure how to approach it. Some guidance on this would be much appreciated!
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Undo the public API change and instead expose |
||
| where | ||
| I: IntoIterator<Item = TrashItem>, | ||
| { | ||
|
|
@@ -540,6 +548,6 @@ pub mod os_limited { | |
| return Err(Error::RestoreTwins { path: item.original_path(), items }); | ||
| } | ||
| } | ||
| platform::restore_all(items) | ||
| platform::restore_all(items, mode) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't documented at all. Could you add
#![deny(missing_docs)]tolib.rs? That way this would light up in CI.