mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
Add fs::read_file_sync_string
This commit is contained in:
parent
040a042679
commit
72544de443
2 changed files with 13 additions and 8 deletions
|
@ -70,7 +70,7 @@ impl DenoDir {
|
||||||
) -> std::io::Result<String> {
|
) -> std::io::Result<String> {
|
||||||
let path = self.cache_path(filename, source_code);
|
let path = self.cache_path(filename, source_code);
|
||||||
debug!("load_cache {}", path.display());
|
debug!("load_cache {}", path.display());
|
||||||
fs::read_file_sync(&path)
|
fs::read_file_sync_string(&path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn code_cache(
|
pub fn code_cache(
|
||||||
|
@ -251,7 +251,7 @@ fn test_code_cache() {
|
||||||
let r = deno_dir.code_cache(filename, source_code, output_code);
|
let r = deno_dir.code_cache(filename, source_code, output_code);
|
||||||
r.expect("code_cache error");
|
r.expect("code_cache error");
|
||||||
assert!(cache_path.exists());
|
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
|
// https://github.com/denoland/deno/blob/golang/deno_dir.go#L25-L30
|
||||||
|
@ -413,6 +413,6 @@ fn get_source_code(
|
||||||
module_name == filename,
|
module_name == filename,
|
||||||
"if a module isn't remote, it should have the same 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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
src/fs.rs
15
src/fs.rs
|
@ -3,15 +3,20 @@ use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
pub fn read_file_sync(path: &Path) -> std::io::Result<Vec<u8>> {
|
||||||
pub fn read_file_sync(path: &Path) -> std::io::Result<String> {
|
|
||||||
File::open(path).and_then(|mut f| {
|
File::open(path).and_then(|mut f| {
|
||||||
let mut contents = String::new();
|
let mut buffer = Vec::new();
|
||||||
f.read_to_string(&mut contents)?;
|
f.read_to_end(&mut buffer)?;
|
||||||
Ok(contents)
|
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<()> {
|
pub fn mkdir(path: &Path) -> std::io::Result<()> {
|
||||||
debug!("mkdir -p {}", path.display());
|
debug!("mkdir -p {}", path.display());
|
||||||
assert!(path.has_root(), "non-has_root not yet implemented");
|
assert!(path.has_root(), "non-has_root not yet implemented");
|
||||||
|
|
Loading…
Reference in a new issue