Do not open every source file twice when reading it

This commit is contained in:
Jakub Beránek
2025-08-25 15:28:19 +02:00
parent ee361e8fca
commit 3145c06431
+8 -2
View File
@@ -9,6 +9,7 @@
//! within the `SourceMap`, which upon request can be converted to line and column
//! information, source code snippets, etc.
use std::fs::File;
use std::io::{self, BorrowedBuf, Read};
use std::{fs, path};
@@ -115,13 +116,18 @@ fn file_exists(&self, path: &Path) -> bool {
}
fn read_file(&self, path: &Path) -> io::Result<String> {
if path.metadata().is_ok_and(|metadata| metadata.len() > SourceFile::MAX_FILE_SIZE.into()) {
let mut file = File::open(path)?;
let size = file.metadata().map(|metadata| metadata.len()).ok().unwrap_or(0);
if size > SourceFile::MAX_FILE_SIZE.into() {
return Err(io::Error::other(format!(
"text files larger than {} bytes are unsupported",
SourceFile::MAX_FILE_SIZE
)));
}
fs::read_to_string(path)
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn read_binary_file(&self, path: &Path) -> io::Result<Arc<[u8]>> {