From 3932bb2cb93be95d67fc56998423eb9ce047fdfa Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 9 Aug 2021 11:06:37 +0200 Subject: New source loading architecture --- src/loading/mod.rs | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) (limited to 'src/loading/mod.rs') diff --git a/src/loading/mod.rs b/src/loading/mod.rs index 65eb25c6..7d697310 100644 --- a/src/loading/mod.rs +++ b/src/loading/mod.rs @@ -13,41 +13,24 @@ use serde::{Deserialize, Serialize}; use crate::font::FaceInfo; +/// A hash that identifies a file. +/// +/// Such a hash can be [resolved](Loader::resolve) from a path. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Serialize, Deserialize)] +pub struct FileHash(pub u64); + /// Loads resources from a local or remote source. pub trait Loader { /// Descriptions of all font faces this loader serves. fn faces(&self) -> &[FaceInfo]; - /// Resolve a `path` relative to a `base` file. - /// - /// 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) -> io::Result; - - /// Load a file by id. - /// - /// 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>; -} - -/// A file id that can be [resolved](Loader::resolve_from) from a path. -/// -/// Should be the same for all paths pointing to the same file. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] -#[derive(Serialize, Deserialize)] -pub struct FileId(u64); + /// Resolve a hash that is the same for this and all other paths pointing to + /// the same file. + fn resolve(&self, path: &Path) -> io::Result; -impl FileId { - /// Create a file id from a raw value. - pub const fn from_raw(v: u64) -> Self { - Self(v) - } - - /// Convert into the raw underlying value. - pub const fn into_raw(self) -> u64 { - self.0 - } + /// Load a file from a path. + fn load(&self, path: &Path) -> io::Result>; } /// A loader which serves nothing. @@ -58,11 +41,11 @@ impl Loader for BlankLoader { &[] } - fn resolve_from(&self, _: FileId, _: &Path) -> io::Result { + fn resolve(&self, _: &Path) -> io::Result { Err(io::ErrorKind::NotFound.into()) } - fn load_file(&self, _: FileId) -> io::Result> { - panic!("resolve_from never returns an id") + fn load(&self, _: &Path) -> io::Result> { + Err(io::ErrorKind::NotFound.into()) } } -- cgit v1.2.3