1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use errors::*;
use std::fs::File;
use std::io::{Write, Read};
pub fn write_file(data: &str, path: &str) -> Result<()> {
File::create(&path)
.chain_err(|| format!("Failed to open file {} for writing", &path))?
.write_all(data.as_bytes())
.chain_err(|| format!("Failed to write to file {}", &path))?;
Ok(())
}
pub fn read_file(path: &str) -> Result<String> {
let mut value_str = String::new();
let _ = File::open(path)
.chain_err(|| format!("Failed to open file {} for reading", &path))?
.read_to_string(&mut value_str)
.chain_err(|| format!("Failed to read from file {}", &path))?;
Ok(value_str)
}