Skip to main content

file_storage/op/file/delete/
delete.rs

1use crate::Operation::Delete;
2use crate::{Error, FilePath};
3use crate::{LocalPath, Reason};
4
5impl FilePath {
6    //! Delete
7
8    /// Deletes the file.
9    ///
10    /// Returns `Ok(())` if the file was deleted or if the file did not exist.
11    pub fn delete(&self) -> Result<(), Error> {
12        #[cfg(feature = "r2")]
13        if let Some(path) = crate::R2Path::from(self.path()) {
14            return path.delete();
15        }
16
17        self.delete_if_exists().map(|_| ())
18    }
19
20    /// Deletes the file if it exists.
21    ///
22    /// Returns `Ok(true)` if the file existed and was deleted.
23    /// Returns `Ok(false)` if the file did not exist.
24    pub fn delete_if_exists(&self) -> Result<bool, Error> {
25        if let Some(local) = LocalPath::from(self.path()) {
26            return local.delete_if_exists();
27        }
28
29        #[cfg(feature = "r2")]
30        if crate::R2Path::from(self.path()).is_some() {
31            return Err(Error::new(
32                self.clone(),
33                Delete,
34                Reason::UnsupportedOperation,
35            ));
36        }
37
38        Err(Error::new(self.clone(), Delete, Reason::UnknownFileSystem))
39    }
40}