summaryrefslogtreecommitdiff
path: root/src/image.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/image.rs
parent0dacb2d151e1790613b324b3051b8ce7aa26a90e (diff)
Unified file loading errors
Diffstat (limited to 'src/image.rs')
-rw-r--r--src/image.rs32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/image.rs b/src/image.rs
index 6b21bdd9..8ef404fe 100644
--- a/src/image.rs
+++ b/src/image.rs
@@ -10,6 +10,7 @@ use std::sync::Arc;
use image::io::Reader as ImageReader;
use image::{DynamicImage, ImageFormat};
+use crate::diag::{failed_to_load, StrResult};
use crate::loading::{FileHash, Loader};
/// A unique identifier for a loaded image.
@@ -60,19 +61,24 @@ impl ImageStore {
/// Load and decode an image file from a path relative to the compilation
/// environment's root.
- pub fn load(&mut self, path: &Path) -> io::Result<ImageId> {
- let hash = self.loader.resolve(path)?;
- Ok(*match self.files.entry(hash) {
- Entry::Occupied(entry) => entry.into_mut(),
- Entry::Vacant(entry) => {
- let buffer = self.loader.load(path)?;
- let ext = path.extension().and_then(OsStr::to_str).unwrap_or_default();
- let image = Image::parse(&buffer, ext)?;
- let id = ImageId(self.images.len() as u32);
- self.images.push(image);
- entry.insert(id)
- }
- })
+ pub fn load(&mut self, path: &Path) -> StrResult<ImageId> {
+ let mut try_load = || -> io::Result<ImageId> {
+ let hash = self.loader.resolve(path)?;
+ Ok(*match self.files.entry(hash) {
+ Entry::Occupied(entry) => entry.into_mut(),
+ Entry::Vacant(entry) => {
+ let buffer = self.loader.load(path)?;
+ let ext =
+ path.extension().and_then(OsStr::to_str).unwrap_or_default();
+ let image = Image::parse(&buffer, ext)?;
+ let id = ImageId(self.images.len() as u32);
+ self.images.push(image);
+ entry.insert(id)
+ }
+ })
+ };
+
+ try_load().map_err(|err| failed_to_load("image", path, err))
}
}