summaryrefslogtreecommitdiff
path: root/src/source.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-06-14 20:07:27 +0200
committerLaurenz <laurmaedje@gmail.com>2022-06-14 20:07:27 +0200
commit7fb19d5ef8dc3b183d7de811e373768de56f7ee8 (patch)
tree885a94958e8270615aa57069b4d07db82e534b2c /src/source.rs
parent0dacb2d151e1790613b324b3051b8ce7aa26a90e (diff)
Unified file loading errors
Diffstat (limited to 'src/source.rs')
-rw-r--r--src/source.rs27
1 files changed, 15 insertions, 12 deletions
diff --git a/src/source.rs b/src/source.rs
index ce837f9e..c299da4f 100644
--- a/src/source.rs
+++ b/src/source.rs
@@ -8,7 +8,7 @@ use std::sync::Arc;
use unscanny::Scanner;
-use crate::diag::TypResult;
+use crate::diag::{failed_to_load, StrResult, TypResult};
use crate::loading::{FileHash, Loader};
use crate::parse::{is_newline, parse, reparse};
use crate::syntax::ast::Markup;
@@ -74,19 +74,22 @@ impl SourceStore {
///
/// If there already exists a source file for this path, it is
/// [replaced](SourceFile::replace).
- pub fn load(&mut self, path: impl AsRef<Path>) -> io::Result<SourceId> {
- let path = path.as_ref();
- let hash = self.loader.resolve(path)?;
- if let Some(&id) = self.files.get(&hash) {
- return Ok(id);
- }
+ pub fn load(&mut self, path: &Path) -> StrResult<SourceId> {
+ let mut try_load = || -> io::Result<SourceId> {
+ let hash = self.loader.resolve(path)?;
+ if let Some(&id) = self.files.get(&hash) {
+ return Ok(id);
+ }
+
+ let data = self.loader.load(path)?;
+ let src = String::from_utf8(data).map_err(|_| {
+ io::Error::new(io::ErrorKind::InvalidData, "file is not valid utf-8")
+ })?;
- let data = self.loader.load(path)?;
- let src = String::from_utf8(data).map_err(|_| {
- io::Error::new(io::ErrorKind::InvalidData, "file is not valid utf-8")
- })?;
+ Ok(self.provide(path, src))
+ };
- Ok(self.provide(path, src))
+ try_load().map_err(|err| failed_to_load("source file", path, err))
}
/// Directly provide a source file.