use crate::Operation::Read;
use crate::Reason::{FileNotFound, UnknownFileSystem};
use crate::{Error, FilePath, LocalPath};
impl FilePath {
pub fn read_as_vec(&self) -> Result<Vec<u8>, Error> {
if let Some(vec) = self.read_as_vec_if_exists()? {
Ok(vec)
} else {
Err(Error::new(self.clone(), Read, FileNotFound))
}
}
pub fn read_as_vec_if_exists(&self) -> Result<Option<Vec<u8>>, Error> {
let mut target: Vec<u8> = Vec::default();
if let Some(content_len) = self.read_to_vec_if_exists(&mut target)? {
debug_assert_eq!(content_len, target.len());
Ok(Some(target))
} else {
Ok(None)
}
}
pub fn read_to_vec(&self, target: &mut Vec<u8>) -> Result<usize, Error> {
if let Some(file_content_len) = self.read_to_vec_if_exists(target)? {
Ok(file_content_len)
} else {
Err(Error::new(self.clone(), Read, FileNotFound))
}
}
pub fn read_to_vec_if_exists(&self, target: &mut Vec<u8>) -> Result<Option<usize>, Error> {
if let Some(path) = LocalPath::new(self.path()) {
return path.read_to_vec_if_exists(target);
}
#[cfg(feature = "r2")]
if let Some(path) = crate::R2Path::new(self.path()) {
return path.read_to_vec_if_exists(target);
}
Err(Error::new(self.clone(), Read, UnknownFileSystem))
}
}