1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::Operation::Read;
#[cfg(feature = "r2")]
use crate::R2Path;
use crate::Reason::{FileNotFound, UnknownFileSystem};
use crate::{Error, FilePath, LocalPath};
impl FilePath {
//! Read Vec
/// Reads the file as a `Vec`.
///
/// # Note
/// - A missing file fails with `FileNotFound`. See `read_if_exists` for what counts as missing.
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))
}
}
/// Reads the file as a `Vec` if it exists.
///
/// # Note
/// - It is `None` if the file does not exist. See `read_if_exists` for what counts as missing.
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)
}
}
/// Reads the file to the end of the `target` `Vec`.
///
/// # Note
/// - It returns the number of bytes read.
/// - A missing file fails with `FileNotFound`. See `read_if_exists` for what counts as missing.
/// - On error the `target` is left as it was.
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))
}
}
/// Reads the file to the end of the `target` `Vec` if it exists.
///
/// # Note
/// - It returns the number of bytes read. It is `None` if the file does not exist.
/// - See `read_if_exists` for what counts as missing.
/// - On error the `target` is left as it was.
pub fn read_to_vec_if_exists(&self, target: &mut Vec<u8>) -> Result<Option<usize>, Error> {
let original_len: usize = target.len();
let result: Result<Option<usize>, Error> = self.read_to_vec_from_backend(target);
if result.is_err() {
target.truncate(original_len);
}
result
}
/// Reads the file to the `target` `Vec` from its backend.
fn read_to_vec_from_backend(&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) = R2Path::new(self.path()) {
return path.read_to_vec_if_exists(target);
}
Err(Error::new(self.clone(), Read, UnknownFileSystem))
}
}