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        if self.is_local_path() {
13            return self.delete_if_exists().map(|_| ());
14        }
15
16        #[cfg(feature = "r2")]
17        if let Some(r2) = crate::R2Path::from(self.path()) {
18            return r2.delete();
19        }
20
21        Err(Error::new(self.clone(), Delete, Reason::UnknownFileSystem))
22    }
23
24    /// Deletes the file if it exists.
25    ///
26    /// Returns `Ok(true)` if the file existed and was deleted.
27    /// Returns `Ok(false)` if the file did not exist.
28    pub fn delete_if_exists(&self) -> Result<bool, Error> {
29        if let Some(local) = LocalPath::from(self.path()) {
30            return local.delete_if_exists();
31        }
32
33        #[cfg(feature = "r2")]
34        if crate::R2Path::from(self.path()).is_some() {
35            return Err(Error::new(
36                self.clone(),
37                Delete,
38                Reason::UnsupportedOperation,
39            ));
40        }
41
42        Err(Error::new(self.clone(), Delete, Reason::UnknownFileSystem))
43    }
44}