Add convenience function to read the content from a file into a string given a path
This commit is contained in:
parent
15d26befcc
commit
c3564f1699
|
@ -1,8 +1,28 @@
|
|||
use std::path::{Path, Component};
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::io::{self, Read};
|
||||
use std::fs::{self, metadata, File};
|
||||
|
||||
/// Takes a path to a file and try to read the file into a String
|
||||
|
||||
pub fn file_to_string(path: &Path) -> Result<String, Box<Error>> {
|
||||
let mut file = match File::open(path) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
debug!("[*]: Failed to open {:?}", path);
|
||||
return Err(Box::new(e));
|
||||
},
|
||||
};
|
||||
|
||||
let mut content = String::new();
|
||||
|
||||
if let Err(e) = file.read_to_string(&mut content) {
|
||||
debug!("[*]: Failed to read {:?}", path);
|
||||
return Err(Box::new(e));
|
||||
}
|
||||
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
/// Takes a path and returns a path containing just enough `../` to point to the root of the given path.
|
||||
///
|
||||
|
|
Loading…
Reference in New Issue