forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.rs
30 lines (27 loc) · 788 Bytes
/
fs.rs
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
use std;
use std::fs::File;
use std::io::Read;
use std::path::Path;
pub fn read_file_sync(path: &Path) -> std::io::Result<Vec<u8>> {
File::open(path).and_then(|mut f| {
let mut buffer = Vec::new();
f.read_to_end(&mut buffer)?;
Ok(buffer)
})
}
pub fn read_file_sync_string(path: &Path) -> std::io::Result<String> {
let vec = read_file_sync(path)?;
String::from_utf8(vec)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))
}
pub fn mkdir(path: &Path) -> std::io::Result<()> {
debug!("mkdir -p {}", path.display());
assert!(path.has_root(), "non-has_root not yet implemented");
std::fs::create_dir_all(path).or_else(|err| {
if err.kind() == std::io::ErrorKind::AlreadyExists {
Ok(())
} else {
Err(err)
}
})
}