file_storage/op/file/write/
write.rs

1use crate::system::LocalPath;
2use crate::Operation::Write;
3use crate::Reason::{FileAlreadyExists, UnknownFileSystem};
4use crate::{Error, FilePath, FileWrite};
5
6impl FilePath {
7    //! Write
8
9    /// Writes an empty file if the file does not exist.
10    ///
11    /// Returns `Ok(true)` if the file was written.
12    /// Returns `Ok(false)` if the file already exists.
13    pub fn write(&self) -> Result<FileWrite, Error> {
14        if let Some(write) = self.write_if_not_exists()? {
15            Ok(write)
16        } else {
17            Err(Error::new(self.path().clone(), Write, FileAlreadyExists))
18        }
19    }
20
21    /// Opens a write operation to the file.
22    ///
23    /// Returns `Ok(file_write)`.
24    /// Returns `Err(FileAlreadyExists)` if the file already exists.
25    pub fn write_if_not_exists(&self) -> Result<Option<FileWrite>, Error> {
26        if let Some(local) = LocalPath::from(self.path()) {
27            return local.write_if_not_exists();
28        }
29
30        Err(Error::new(self.path().clone(), Write, UnknownFileSystem))
31    }
32}