file_storage/op/file/read/
read_string.rs1use crate::Operation::Read;
2use crate::Reason::{FileContentNotUTF8, FileNotFound};
3use crate::{Error, FilePath};
4
5impl FilePath {
6 pub fn read_as_string(&self) -> Result<String, Error> {
13 if let Some(file_content) = self.read_as_string_if_exists()? {
14 Ok(file_content)
15 } else {
16 Err(Error::new(self.clone(), Read, FileNotFound))
17 }
18 }
19
20 pub fn read_as_string_if_exists(&self) -> Result<Option<String>, Error> {
25 if let Some(file_content) = self.read_as_vec_if_exists()? {
26 match String::from_utf8(file_content) {
27 Ok(file_content) => Ok(Some(file_content)),
28 Err(_) => Err(Error::new(self.clone(), Read, FileContentNotUTF8)),
29 }
30 } else {
31 Ok(None)
32 }
33 }
34
35 pub fn read_to_string(&self, target: &mut String) -> Result<usize, Error> {
40 if let Some(file_content_len) = self.read_to_string_if_exists(target)? {
41 Ok(file_content_len)
42 } else {
43 Err(Error::new(self.clone(), Read, FileNotFound))
44 }
45 }
46
47 pub fn read_to_string_if_exists(&self, target: &mut String) -> Result<Option<usize>, Error> {
52 let target: &mut Vec<u8> = unsafe { target.as_mut_vec() };
54 let original_len: usize = target.len();
55 match self.read_to_vec_if_exists(target) {
56 Ok(file_content_len) => {
57 if let Some(file_content_len) = file_content_len {
58 debug_assert_eq!(target.len(), original_len + file_content_len);
59 let slice: &[u8] = &target[original_len..];
60 match std::str::from_utf8(slice) {
61 Ok(_) => Ok(Some(file_content_len)),
62 Err(_) => {
63 target.truncate(original_len);
64 Err(Error::new(self.clone(), Read, FileContentNotUTF8))
65 }
66 }
67 } else {
68 Ok(None)
69 }
70 }
71 Err(e) => {
72 target.truncate(original_len);
73 Err(e)
74 }
75 }
76 }
77}