[][src]Function bloguen::util::read_file

pub fn read_file(
    whom: &(String, PathBuf),
    what_for: &'static str
) -> Result<String, Error>

Read the contents of the specified file into a String.

Examples

Given the following:

$ROOT
  index.html
  image.png

The following holds:

let root: PathBuf = /* obtained elsewhere */;

assert_eq!(read_file(&("$ROOT/index.html".to_string(), root.join("index.html")), "root HTML"),
           Ok("<html>{henlo}</html>".to_string()));
assert_eq!(read_file(&("$ROOT/image.png".to_string(), root.join("image.png")), "image"),
           Err(Error::Parse {
               tp: "UTF-8 string",
               wher: "image".into(),
               more: "stream did not contain valid UTF-8".into(),
           }));
assert_eq!(read_file(&("$ROOT/nonexistant".to_string(), root.join("nonexistant")), "∅"),
           Err(Error::FileNotFound {
               who: "∅",
               path: "$ROOT/nonexistant".into(),
           }));