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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use Result;
pub fn read_file<P>(path: P) -> Result<String>
where
P: AsRef<Path>,
{
let mut f = File::open(path)?;
let mut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
}
pub fn write_file<P>(name: P, data: &str) -> Result<()>
where
P: AsRef<Path>,
{
let mut f = match File::create(&name) {
Ok(f) => Ok(f),
Err(err) => {
Err(format!(
"create error in file '{}': {}",
name.as_ref().display(),
err
))
}
}?;
match write!(&mut f, "{}", data) {
Ok(f) => Ok(f),
Err(err) => Err(format!(
"write error in file '{}': {}",
name.as_ref().display(),
err
)),
}?;
Ok(())
}