summaryrefslogtreecommitdiff
path: root/src/loading
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-07-21 12:27:40 +0200
committerLaurenz <laurmaedje@gmail.com>2021-07-21 12:27:40 +0200
commit929f4d64fef8054cbaf34d556467a7d2b9d09b47 (patch)
treeca2d071033659754db2e3e3a363431c1f9901d4d /src/loading
parentb0e5212973ce2efcb1433323d67c06eea1a81785 (diff)
Switch Loader from Option to io::Result
Diffstat (limited to 'src/loading')
-rw-r--r--src/loading/fs.rs13
-rw-r--r--src/loading/mod.rs16
2 files changed, 18 insertions, 11 deletions
diff --git a/src/loading/fs.rs b/src/loading/fs.rs
index 407e2d94..0f6f1076 100644
--- a/src/loading/fs.rs
+++ b/src/loading/fs.rs
@@ -187,13 +187,16 @@ impl Loader for FsLoader {
&self.faces
}
- fn resolve_from(&self, base: FileId, path: &Path) -> Option<FileId> {
- let full = self.paths.borrow()[&base].parent()?.join(path);
- self.resolve(&full).ok()
+ fn resolve_from(&self, base: FileId, path: &Path) -> io::Result<FileId> {
+ let full = self.paths.borrow()[&base]
+ .parent()
+ .expect("base is a file")
+ .join(path);
+ self.resolve(&full)
}
- fn load_file(&self, id: FileId) -> Option<Vec<u8>> {
- fs::read(&self.paths.borrow()[&id]).ok()
+ fn load_file(&self, id: FileId) -> io::Result<Vec<u8>> {
+ fs::read(&self.paths.borrow()[&id])
}
}
diff --git a/src/loading/mod.rs b/src/loading/mod.rs
index 3be74428..64a65580 100644
--- a/src/loading/mod.rs
+++ b/src/loading/mod.rs
@@ -6,6 +6,7 @@ mod fs;
#[cfg(feature = "fs")]
pub use fs::*;
+use std::io;
use std::path::Path;
use serde::{Deserialize, Serialize};
@@ -21,10 +22,13 @@ pub trait Loader {
///
/// This should return the same id for all paths pointing to the same file
/// and `None` if the file does not exist.
- fn resolve_from(&self, base: FileId, path: &Path) -> Option<FileId>;
+ fn resolve_from(&self, base: FileId, path: &Path) -> io::Result<FileId>;
/// Load a file by id.
- fn load_file(&self, id: FileId) -> Option<Vec<u8>>;
+ ///
+ /// This must only be called with an `id` returned by a call to this
+ /// loader's `resolve_from` method.
+ fn load_file(&self, id: FileId) -> io::Result<Vec<u8>>;
}
/// A file id that can be [resolved](Loader::resolve_from) from a path.
@@ -53,11 +57,11 @@ impl Loader for BlankLoader {
&[]
}
- fn resolve_from(&self, _: FileId, _: &Path) -> Option<FileId> {
- None
+ fn resolve_from(&self, _: FileId, _: &Path) -> io::Result<FileId> {
+ Err(io::ErrorKind::NotFound.into())
}
- fn load_file(&self, _: FileId) -> Option<Vec<u8>> {
- None
+ fn load_file(&self, _: FileId) -> io::Result<Vec<u8>> {
+ panic!("resolve_from never returns an id")
}
}