summaryrefslogtreecommitdiff
path: root/src/loading
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-05-31 12:00:13 +0200
committerLaurenz <laurmaedje@gmail.com>2021-05-31 22:33:40 +0200
commit00ac68b8451179468aa39cba0d7fbea1ee20e0a1 (patch)
tree8271cf0189dc2a99109af72ce54867fb0cf90802 /src/loading
parente023bf2ac9f5796355d9485afc16781196bf212b (diff)
Fix and improve
- Set context location to resolved path during module evaluation. - Dump module diagnostics on import - Use same-file for more robustness than fs::canonicalize
Diffstat (limited to 'src/loading')
-rw-r--r--src/loading/fs.rs14
-rw-r--r--src/loading/mod.rs21
2 files changed, 21 insertions, 14 deletions
diff --git a/src/loading/fs.rs b/src/loading/fs.rs
index bf768bd5..e2654a2e 100644
--- a/src/loading/fs.rs
+++ b/src/loading/fs.rs
@@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
use std::rc::Rc;
use memmap2::Mmap;
+use same_file::Handle;
use serde::{Deserialize, Serialize};
use ttf_parser::{name_id, Face};
use walkdir::WalkDir;
@@ -167,10 +168,6 @@ impl Loader for FsLoader {
&self.faces
}
- fn resolve(&self, path: &Path) -> Option<FileHash> {
- hash(path)
- }
-
fn load_face(&mut self, idx: usize) -> Option<Buffer> {
load(&mut self.cache, &self.files[idx])
}
@@ -178,6 +175,10 @@ impl Loader for FsLoader {
fn load_file(&mut self, path: &Path) -> Option<Buffer> {
load(&mut self.cache, path)
}
+
+ fn resolve(&self, path: &Path) -> Option<FileHash> {
+ hash(path)
+ }
}
/// Load from the file system using a cache.
@@ -191,8 +192,11 @@ fn load(cache: &mut FileCache, path: &Path) -> Option<Buffer> {
})
}
+/// Create a hash that is the same for all paths pointing to the same file.
fn hash(path: &Path) -> Option<FileHash> {
- path.canonicalize().ok().map(|p| FileHash(fxhash::hash64(&p)))
+ Handle::from_path(path)
+ .map(|handle| FileHash(fxhash::hash64(&handle)))
+ .ok()
}
#[cfg(test)]
diff --git a/src/loading/mod.rs b/src/loading/mod.rs
index b4e5d160..f57b5c73 100644
--- a/src/loading/mod.rs
+++ b/src/loading/mod.rs
@@ -19,19 +19,22 @@ pub trait Loader {
/// Descriptions of all font faces this loader serves.
fn faces(&self) -> &[FaceInfo];
- /// Resolve a hash that is the same for all paths pointing to the same file.
- ///
- /// Should return `None` if the file does not exist.
- fn resolve(&self, path: &Path) -> Option<FileHash>;
-
/// Load the font face with the given index in [`faces()`](Self::faces).
fn load_face(&mut self, idx: usize) -> Option<Buffer>;
/// Load a file from a path.
fn load_file(&mut self, path: &Path) -> Option<Buffer>;
+
+ /// Resolve a hash for the file the path points to.
+ ///
+ /// This should return the same hash for all paths pointing to the same file
+ /// and `None` if the file does not exist.
+ fn resolve(&self, path: &Path) -> Option<FileHash>;
}
-/// A hash that must be the same for all paths pointing to the same file.
+/// A file hash that can be [resolved](Loader::resolve) from a path.
+///
+/// Should be the same for all paths pointing to the same file.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct FileHash(pub u64);
@@ -43,15 +46,15 @@ impl Loader for BlankLoader {
&[]
}
- fn resolve(&self, _: &Path) -> Option<FileHash> {
+ fn load_face(&mut self, _: usize) -> Option<Buffer> {
None
}
- fn load_face(&mut self, _: usize) -> Option<Buffer> {
+ fn load_file(&mut self, _: &Path) -> Option<Buffer> {
None
}
- fn load_file(&mut self, _: &Path) -> Option<Buffer> {
+ fn resolve(&self, _: &Path) -> Option<FileHash> {
None
}
}