file_storage/op/file/read/
read.rs

1use crate::system::LocalPath;
2use crate::{Error, FilePath, FileRead, Operation, Reason};
3
4impl FilePath {
5    //! Read
6
7    /// Reads the file if it exists.
8    pub fn read_if_exists(&self) -> Result<Option<FileRead>, Error> {
9        if let Some(local) = LocalPath::from(self.path()) {
10            return local.read_if_exists();
11        }
12
13        #[cfg(feature = "r2")]
14        if let Some(r2) = crate::R2Path::from(self.path()) {
15            return r2.read_if_exists();
16        }
17
18        Err(Error::new(
19            self.clone(),
20            Operation::Read,
21            Reason::UnknownFileSystem,
22        ))
23    }
24
25    /// Reads the file.
26    pub fn read(&self) -> Result<FileRead, Error> {
27        if let Some(read) = self.read_if_exists()? {
28            Ok(read)
29        } else {
30            Err(Error::new(
31                self.clone(),
32                Operation::Read,
33                Reason::FileNotFound,
34            ))
35        }
36    }
37}