Skip to content

Commit 8cb3f75

Browse files
committed
refactor
1 parent 0b42fc0 commit 8cb3f75

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

src/freedesktop.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ impl PlatformTrashContext {
3131
impl TrashContext {
3232
pub(crate) fn delete_all_canonicalized(&self, full_paths: Vec<PathBuf>) -> Result<(), Error> {
3333
let home_trash = home_trash()?;
34-
// Sorted by longest first
3534
let sorted_mount_points = get_sorted_mount_points()?;
3635
let home_topdir = home_topdir(&sorted_mount_points)?;
3736
debug!("The home topdir is {:?}", home_topdir);
@@ -620,17 +619,7 @@ fn home_topdir(mnt_points: &[MountPoint]) -> Result<PathBuf, Error> {
620619

621620
fn get_first_topdir_containing_path<'a>(path: &Path, mnt_points: &'a [MountPoint]) -> &'a Path {
622621
let root: &'static Path = Path::new("/");
623-
let mut topdir: Option<&Path> = None;
624-
for mount_point in mnt_points {
625-
if mount_point.mnt_dir == root {
626-
continue;
627-
}
628-
if path.starts_with(&mount_point.mnt_dir) {
629-
topdir = Some(&mount_point.mnt_dir);
630-
break;
631-
}
632-
}
633-
topdir.unwrap_or(root)
622+
mnt_points.iter().map(|mp| mp.mnt_dir.as_path()).find(|mount_path| path.starts_with(mount_path)).unwrap_or(root)
634623
}
635624

636625
struct MountPoint {
@@ -639,11 +628,14 @@ struct MountPoint {
639628
_mnt_fsname: String,
640629
}
641630

631+
/// Sorted by longest path first
642632
fn get_sorted_mount_points() -> Result<Vec<MountPoint>, Error> {
643-
// Returns longest mount points first
644633
let mut mount_points = get_mount_points()?;
645-
mount_points
646-
.sort_unstable_by(|a, b| b.mnt_dir.as_os_str().as_bytes().len().cmp(&a.mnt_dir.as_os_str().as_bytes().len()));
634+
mount_points.sort_unstable_by(|a, b| {
635+
let a = a.mnt_dir.as_os_str().as_bytes().len();
636+
let b = b.mnt_dir.as_os_str().as_bytes().len();
637+
a.cmp(&b).reverse()
638+
});
647639
Ok(mount_points)
648640
}
649641

0 commit comments

Comments
 (0)