forte_engine/utils/files.rs
1use std::{fs::File, io::Read};
2
3/// A struct to contain functions for loading files.
4pub struct Files;
5impl Files {
6 /// A function to load the bytes of a file at the given relative path.
7 ///
8 /// Arguments:
9 /// * path: &str - the relative path to the file.
10 ///
11 /// Returns a result:
12 /// * Ok - Vec<u8> - The bytes of the file.
13 /// * Error - std::io::Error - The io error that occured while failing to load the file.
14 pub fn load_bytes(path: &str) -> Result<Vec<u8>, std::io::Error> {
15 // get file and metadata
16 let mut file = File::open(&path)?;
17 let metadata = file.metadata()?;
18
19 // load file into a buffer
20 let mut buffer = vec![0; metadata.len() as usize];
21 let _ = file.read(&mut buffer);
22
23 // return the buffer
24 return Ok(buffer);
25 }
26}