#[cfg(any(feature = "show", feature = "open", feature = "terminal"))]
use std::path::Path;
#[allow(unused_imports)]
use crate::FileHandleError;
#[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
mod linux_unix;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "windows")]
mod windows;
pub struct FileHandle;
impl FileHandle {
#[cfg(feature = "open")]
pub fn open_with_default<P: AsRef<Path>>(path: P) -> Result<(), FileHandleError> {
let path = path.as_ref();
if !path.exists() {
return Err(FileHandleError::NotFound(path.to_owned()));
}
Self::dispatch_open(path)
}
#[cfg(feature = "show")]
pub fn show<P: AsRef<Path>>(path: P) -> Result<(), FileHandleError> {
let path = path.as_ref();
let is_dir = path
.metadata()
.map_err(|_| FileHandleError::NotFound(path.to_owned()))?
.is_dir();
Self::dispatch_show(path, is_dir)
}
#[cfg(feature = "terminal")]
pub fn open_terminal<P: AsRef<Path>>(path: P) -> Result<(), FileHandleError> {
let path = path.as_ref();
let dir = if path.is_dir() {
path
} else {
path.parent()
.ok_or_else(|| FileHandleError::OpFailed("No parent".into()))?
};
Self::dispatch_terminal(dir)
}
#[cfg(feature = "trash")]
pub fn trash<P: AsRef<Path>>(path: P) -> Result<(), FileHandleError> {
trash_lib::delete(path).map_err(|e| FileHandleError::Trash(e.to_string()))
}
}