This commit is contained in:
Michael Zhang 2020-03-04 00:18:18 -06:00
parent a64e391364
commit cf26ffedb1
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
4 changed files with 13 additions and 3 deletions

View file

@ -12,6 +12,7 @@ use crate::XDG;
pub struct TrashDir(pub PathBuf);
impl TrashDir {
/// Constructor for a new trash directory.
pub fn from(path: impl AsRef<Path>) -> Self {
TrashDir(path.as_ref().to_path_buf())
}
@ -27,11 +28,15 @@ impl TrashDir {
TrashDir::from(XDG.get_data_home().join("Trash"))
}
/// Create a trash directory from an optional path
///
/// If the option is None, then the home trash will be selected instead.
pub fn from_opt(opt: Option<impl AsRef<Path>>) -> Self {
opt.map(|path| TrashDir::from(path.as_ref().to_path_buf()))
.unwrap_or_else(|| TrashDir::get_home_trash())
}
/// Actually create the directory on disk corresponding to this trash directory
pub fn create(&self) -> Result<(), Error> {
let path = &self.0;
if !path.exists() {
@ -45,6 +50,7 @@ impl TrashDir {
self.0.as_ref()
}
/// Get the `files` directory
pub fn files_dir(&self) -> Result<PathBuf, Error> {
let target = self.0.join("files");
if !target.exists() {
@ -53,6 +59,7 @@ impl TrashDir {
Ok(target)
}
/// Get the `info` directory
pub fn info_dir(&self) -> Result<PathBuf, Error> {
let target = self.0.join("info");
if !target.exists() {
@ -61,6 +68,7 @@ impl TrashDir {
Ok(target)
}
/// Iterate over trash infos within this trash directory
pub fn iter(&self) -> Result<TrashDirIter, Error> {
let iter = WalkDir::new(&self.info_dir()?)
.contents_first(true)

View file

@ -4,6 +4,7 @@ use anyhow::Result;
use crate::TrashDir;
/// Options to pass to list
#[derive(StructOpt)]
pub struct ListOptions {
/// The path to the trash directory to list.
@ -12,6 +13,7 @@ pub struct ListOptions {
trash_dir: Option<PathBuf>,
}
/// List the contents of a trash directory
pub fn list(options: ListOptions) -> Result<()> {
let trash_dir = TrashDir::from_opt(options.trash_dir);

View file

@ -9,7 +9,7 @@ use chrono::Local;
use crate::utils;
use crate::{TrashDir, TrashInfo};
use crate::{HOME_MOUNT, HOME_TRASH, MOUNTS};
use crate::{HOME_MOUNT, MOUNTS};
#[derive(Debug, Error)]
pub enum Error {
@ -275,7 +275,6 @@ fn should_use_topdir_trash(mount: impl AsRef<Path>) -> bool {
}
/// Can we use $topdir/.Trash-uid?
fn should_use_topdir_trash_uid(path: impl AsRef<Path>) -> bool {
let path = path.as_ref();
if !path.exists() {
@ -284,6 +283,5 @@ fn should_use_topdir_trash_uid(path: impl AsRef<Path>) -> bool {
Err(_) => return false,
};
}
return true;
}

View file

@ -6,6 +6,7 @@ use anyhow::Result;
use crate::TrashDir;
/// Options to pass to restore
#[derive(StructOpt)]
pub struct RestoreOptions {
/// The path to the trash directory to restore from.
@ -14,6 +15,7 @@ pub struct RestoreOptions {
trash_dir: Option<PathBuf>,
}
/// Restore files from a trash directory
pub fn restore(options: RestoreOptions) -> Result<()> {
let trash_dir = TrashDir::from_opt(options.trash_dir);