From 7fb19d5ef8dc3b183d7de811e373768de56f7ee8 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 14 Jun 2022 20:07:27 +0200 Subject: Unified file loading errors --- src/source.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/source.rs') 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) -> io::Result { - 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 { + let mut try_load = || -> io::Result { + 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. -- cgit v1.2.3