mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Switch Loader from Option to io::Result
This commit is contained in:
parent
b0e5212973
commit
929f4d64fe
@ -86,7 +86,7 @@ impl<'a> EvalContext<'a> {
|
||||
/// Generates an error if the file is not found.
|
||||
pub fn resolve(&mut self, path: &str, span: Span) -> Option<FileId> {
|
||||
let base = *self.route.last()?;
|
||||
self.loader.resolve_from(base, Path::new(path)).or_else(|| {
|
||||
self.loader.resolve_from(base, Path::new(path)).ok().or_else(|| {
|
||||
self.diag(error!(span, "file not found"));
|
||||
None
|
||||
})
|
||||
@ -107,7 +107,7 @@ impl<'a> EvalContext<'a> {
|
||||
return Some(id);
|
||||
}
|
||||
|
||||
let buffer = self.loader.load_file(id).or_else(|| {
|
||||
let buffer = self.loader.load_file(id).ok().or_else(|| {
|
||||
self.diag(error!(span, "failed to load file"));
|
||||
None
|
||||
})?;
|
||||
|
@ -281,7 +281,7 @@ impl FontCache {
|
||||
let buffer = match self.buffers.entry(file) {
|
||||
Entry::Occupied(entry) => entry.into_mut(),
|
||||
Entry::Vacant(entry) => {
|
||||
let buffer = self.loader.load_file(file)?;
|
||||
let buffer = self.loader.load_file(file).ok()?;
|
||||
entry.insert(Rc::new(buffer))
|
||||
}
|
||||
};
|
||||
|
@ -74,7 +74,7 @@ impl ImageCache {
|
||||
pub fn load(&mut self, file: FileId) -> Option<ImageId> {
|
||||
let id = ImageId(file.into_raw());
|
||||
if let Entry::Vacant(entry) = self.images.entry(id) {
|
||||
let buffer = self.loader.load_file(file)?;
|
||||
let buffer = self.loader.load_file(file).ok()?;
|
||||
let image = Image::parse(&buffer)?;
|
||||
if let Some(callback) = &self.on_load {
|
||||
callback(id, &image);
|
||||
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user