Skip to main content

file_storage/op/file/write/
write.rs

1use crate::op::WriteOpInner;
2use crate::system::LocalPath;
3use crate::Operation::Write;
4use crate::Reason;
5use crate::{Error, FilePath, WriteOp};
6
7impl FilePath {
8    //! Write
9
10    /// Opens a write operation to the file.
11    ///
12    /// Returns `Err(FileAlreadyExists)` if the file already exists.
13    pub fn write(&self) -> Result<WriteOp, Error> {
14        if let Some(write) = self.write_if_not_exists()? {
15            Ok(write)
16        } else {
17            Err(Error::new(
18                self.path().clone(),
19                Write,
20                Reason::FileAlreadyExists,
21            ))
22        }
23    }
24
25    /// Opens a write operation to the file if it exists.
26    pub fn write_if_not_exists(&self) -> Result<Option<WriteOp>, Error> {
27        if let Some(path) = LocalPath::from(self.path()) {
28            return path.write_if_not_exists().map(|op| {
29                op.map(|op| WriteOp {
30                    inner: WriteOpInner::Local(op),
31                })
32            });
33        }
34
35        #[cfg(feature = "r2")]
36        if crate::R2Path::from(self.path()).is_some() {
37            // todo -- r2 write operations
38            return Err(Error::new(
39                self.path().clone(),
40                Write,
41                Reason::UnsupportedOperation,
42            ));
43        }
44
45        Err(Error::new(
46            self.path().clone(),
47            Write,
48            Reason::UnknownFileSystem,
49        ))
50    }
51}