From 560fc06d702f9672b02323489d8e90d668f7a064 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Fri, 19 Feb 2016 14:52:43 +0100 Subject: [PATCH] Add file_to_string to return the content of the file given a path --- src/utils/fs/mod.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/utils/fs/mod.rs b/src/utils/fs/mod.rs index 3f3a720f..7479a330 100644 --- a/src/utils/fs/mod.rs +++ b/src/utils/fs/mod.rs @@ -1,7 +1,29 @@ use std::path::{Path, Component}; use std::fs::{self, metadata, File}; use std::error::Error; -use std::io; +use std::io::{self, Read}; + +/// Takes a path to a file and try to read the file into a String + +pub fn file_to_string(path: &Path) -> Result> { + 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. ///