use crate::SPath;
use crate::error::{Cause, PathAndCause};
use crate::safer::{SaferTrashOptions, support};
use crate::{Error, Result};
pub fn safer_trash_dir<'a>(dir_path: &SPath, options: impl Into<SaferTrashOptions<'a>>) -> Result<bool> {
let options = options.into();
if !dir_path.exists() {
return Ok(false);
}
let causes = support::check_path_safety_causes(
dir_path,
options.restrict_to_current_dir,
options.must_contain_any,
options.must_contain_all,
)?;
if !causes.is_empty() {
return Err(Error::DirNotSafeToTrash(PathAndCause {
path: dir_path.to_string(),
cause: Cause::Custom(format!("Safety check failed: {}", causes.join("; "))),
}));
}
trash::delete(dir_path.as_std_path()).map_err(|e| {
Error::CantTrash(PathAndCause {
path: dir_path.to_string(),
cause: Cause::Custom(e.to_string()),
})
})?;
Ok(true)
}
pub fn safer_trash_file<'a>(file_path: &SPath, options: impl Into<SaferTrashOptions<'a>>) -> Result<bool> {
let options = options.into();
if !file_path.exists() {
return Ok(false);
}
let causes = support::check_path_safety_causes(
file_path,
options.restrict_to_current_dir,
options.must_contain_any,
options.must_contain_all,
)?;
if !causes.is_empty() {
return Err(Error::FileNotSafeToTrash(PathAndCause {
path: file_path.to_string(),
cause: Cause::Custom(format!("Safety check failed: {}", causes.join("; "))),
}));
}
trash::delete(file_path.as_std_path()).map_err(|e| {
Error::CantTrash(PathAndCause {
path: file_path.to_string(),
cause: Cause::Custom(e.to_string()),
})
})?;
Ok(true)
}