use crate::Operation::Delete;
#[cfg(feature = "r2")]
use crate::R2Path;
use crate::{Error, FilePath, LocalPath, Reason};
impl FilePath {
pub fn delete(&self) -> Result<(), Error> {
if let Some(path) = LocalPath::new(self.path()) {
return path.delete_if_exists().map(|_existed| ());
}
#[cfg(feature = "r2")]
if let Some(path) = R2Path::new(self.path()) {
return path.delete();
}
Err(Error::new(self.clone(), Delete, Reason::UnknownFileSystem))
}
pub fn delete_if_exists(&self) -> Result<bool, Error> {
if let Some(local) = LocalPath::new(self.path()) {
return local.delete_if_exists();
}
#[cfg(feature = "r2")]
if R2Path::new(self.path()).is_some() {
return Err(Error::new(
self.clone(),
Delete,
Reason::UnsupportedOperation,
));
}
Err(Error::new(self.clone(), Delete, Reason::UnknownFileSystem))
}
}
#[cfg(test)]
mod tests {
use crate::{Error, FilePath, Operation, Reason, StoragePath};
fn file(path: &str) -> FilePath {
StoragePath::parse(path).unwrap().to_file().unwrap()
}
#[test]
fn other_hosts_path_is_unknown_file_system() {
let path: &str = if cfg!(windows) {
"/folder/file.txt"
} else {
"C:\\folder\\file.txt"
};
let file: FilePath = file(path);
let errors: [Error; 2] = [
file.delete().unwrap_err(),
file.delete_if_exists().unwrap_err(),
];
for error in errors {
assert_eq!(error.operation(), Operation::Delete);
assert_eq!(error.reason(), Reason::UnknownFileSystem);
}
}
#[cfg(feature = "r2")]
#[test]
fn r2_delete_if_exists_is_unsupported() {
let file: FilePath = file("https://account.r2.cloudflarestorage.com/bucket/file.txt");
let error: Error = file.delete_if_exists().unwrap_err();
assert_eq!(error.operation(), Operation::Delete);
assert_eq!(error.reason(), Reason::UnsupportedOperation);
}
}