1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

Add fs::read_file_sync_string

This commit is contained in:
Ryan Dahl 2018-08-07 00:54:18 -04:00
parent 040a042679
commit 72544de443
2 changed files with 13 additions and 8 deletions

View file

@ -70,7 +70,7 @@ impl DenoDir {
) -> std::io::Result<String> {
let path = self.cache_path(filename, source_code);
debug!("load_cache {}", path.display());
fs::read_file_sync(&path)
fs::read_file_sync_string(&path)
}
pub fn code_cache(
@ -251,7 +251,7 @@ fn test_code_cache() {
let r = deno_dir.code_cache(filename, source_code, output_code);
r.expect("code_cache error");
assert!(cache_path.exists());
assert_eq!(output_code, fs::read_file_sync(&cache_path).unwrap());
assert_eq!(output_code, fs::read_file_sync_string(&cache_path).unwrap());
}
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L25-L30
@ -413,6 +413,6 @@ fn get_source_code(
module_name == filename,
"if a module isn't remote, it should have the same filename"
);
fs::read_file_sync(Path::new(filename))
fs::read_file_sync_string(Path::new(filename))
}
}

View file

@ -3,15 +3,20 @@ use std::fs::File;
use std::io::Read;
use std::path::Path;
#[allow(dead_code)]
pub fn read_file_sync(path: &Path) -> std::io::Result<String> {
pub fn read_file_sync(path: &Path) -> std::io::Result<Vec<u8>> {
File::open(path).and_then(|mut f| {
let mut contents = String::new();
f.read_to_string(&mut contents)?;
Ok(contents)
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");