Skip to content

Commit 2dee764

Browse files
committed
Address review comments
1 parent 19320a3 commit 2dee764

4 files changed

Lines changed: 30 additions & 36 deletions

File tree

src/datastore.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,11 @@ impl DataType for Image {
191191
.map_err(|e| StoreEntryError::new(source_root.to_path_buf(), e.into()))?;
192192

193193
for (entry_name, is_dir) in entries {
194-
let full_rel = source_root.join(&entry_name);
195194
if is_dir {
196195
// The spec forbids directories.
197-
return Err(StoreEntryError::new(full_rel, StoreError::Subdir));
198-
} else {
199-
let key = full_rel.strip_prefix(source_root).unwrap().to_path_buf();
200-
paths.push(key);
196+
return Err(StoreEntryError::new(entry_name, StoreError::Subdir));
201197
}
198+
paths.push(entry_name);
202199
}
203200

204201
Ok(paths)
@@ -806,16 +803,13 @@ mod tests {
806803

807804
/// A FontSource backed by in-memory data, with `as_path() -> None`.
808805
/// This triggers the eager-loading branch in `Store::new`.
806+
#[derive(Default)]
809807
struct MemorySource {
810808
files: HashMap<PathBuf, Vec<u8>>,
811809
dirs: HashMap<PathBuf, Vec<(PathBuf, bool)>>,
812810
}
813811

814812
impl MemorySource {
815-
fn new() -> Self {
816-
MemorySource { files: HashMap::new(), dirs: HashMap::new() }
817-
}
818-
819813
fn add_file(&mut self, path: impl Into<PathBuf>, data: Vec<u8>) {
820814
self.files.insert(path.into(), data);
821815
}
@@ -841,7 +835,7 @@ mod tests {
841835

842836
#[test]
843837
fn data_eager_loading_from_memory_source() {
844-
let mut source = MemorySource::new();
838+
let mut source = MemorySource::default();
845839
source.add_dir(
846840
"data",
847841
vec![(PathBuf::from("a.txt"), false), (PathBuf::from("b.txt"), false)],
@@ -865,7 +859,7 @@ mod tests {
865859
#[test]
866860
fn image_eager_loading_from_memory_source() {
867861
let img = png_data(b"test");
868-
let mut source = MemorySource::new();
862+
let mut source = MemorySource::default();
869863
source.add_dir("images", vec![(PathBuf::from("x.png"), false)]);
870864
source.add_file("images/x.png", img.clone());
871865

@@ -879,7 +873,7 @@ mod tests {
879873

880874
#[test]
881875
fn eager_load_invalid_image_fails_at_construction() {
882-
let mut source = MemorySource::new();
876+
let mut source = MemorySource::default();
883877
source.add_dir("images", vec![(PathBuf::from("bad.png"), false)]);
884878
source.add_file("images/bad.png", b"not a png".to_vec());
885879

@@ -890,7 +884,7 @@ mod tests {
890884

891885
#[test]
892886
fn eager_load_nested_data_from_memory_source() {
893-
let mut source = MemorySource::new();
887+
let mut source = MemorySource::default();
894888
source
895889
.add_dir("data", vec![(PathBuf::from("top.txt"), false), (PathBuf::from("sub"), true)]);
896890
source.add_dir("data/sub", vec![(PathBuf::from("deep.txt"), false)]);

src/error.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,11 @@ impl StoreEntryError {
313313
/// Returns `true` if the store could not be populated because the source
314314
/// does not support directory listing or the directory does not exist.
315315
pub(crate) fn is_missing_or_unsupported(&self) -> bool {
316-
matches!(&self.source, StoreError::Io(e)
317-
if matches!(e.kind(), std::io::ErrorKind::Unsupported | std::io::ErrorKind::NotFound))
316+
if let StoreError::Io(e) = &self.source {
317+
matches!(e.kind(), std::io::ErrorKind::Unsupported | std::io::ErrorKind::NotFound)
318+
} else {
319+
false
320+
}
318321
}
319322
}
320323

src/font_source.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,24 @@ use std::path::{Path, PathBuf};
1212
/// Paths passed to methods are always relative to the UFO root, e.g.
1313
/// `"metainfo.plist"`, `"glyphs/contents.plist"`, `"glyphs/A_.glif"`.
1414
///
15-
/// A filesystem directory (a `&Path`) implements this trait directly, so you
16-
/// can pass a path wherever a `FontSource` is expected.
15+
/// Two implementations are provided out of the box:
16+
///
17+
/// - A filesystem directory (a `&Path`) implements this trait directly, so you
18+
/// can pass a path wherever a `FontSource` is expected.
19+
/// - Any closure `Fn(&Path) -> Option<Result<Vec<u8>, io::Error>>` implements it
20+
/// too, which is handy for a quick ad-hoc source without defining a type:
21+
///
22+
/// ```
23+
/// use std::io;
24+
/// use std::path::Path;
25+
/// use norad::{DataRequest, Font};
26+
///
27+
/// # fn example(lookup: impl Fn(&Path) -> Option<Vec<u8>> + Sync) -> Result<(), Box<dyn std::error::Error>> {
28+
/// let source = |path: &Path| lookup(path).map(Ok::<_, io::Error>);
29+
/// let font = Font::load_from_source(&DataRequest::all(), &source)?;
30+
/// # Ok(())
31+
/// # }
32+
/// ```
1733
///
1834
/// # Implementing
1935
///

src/fontinfo.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//!
33
//! [`fontinfo.plist`]: https://unifiedfontobject.org/versions/ufo3/fontinfo.plist/
44
5-
use std::path::Path;
65
use std::{collections::HashSet, convert::TryFrom, ops::Deref};
76

87
use serde::de::Deserializer;
@@ -488,24 +487,6 @@ struct FontInfoV1 {
488487
}
489488

490489
impl FontInfo {
491-
/// Returns [`FontInfo`] from a file, upgrading from the supplied `format_version` to the highest
492-
/// internally supported version.
493-
///
494-
/// The conversion follows what ufoLib and defcon are doing, e.g. various fields that were
495-
/// implicitly signed integers before and are unsigned integers in the newest spec, are
496-
/// converted by taking their absolute value. Fields that could be floats before and are
497-
/// integers now are rounded. Fields that could be floats before and are unsigned integers
498-
/// now are rounded before taking their absolute value.
499-
#[allow(dead_code)]
500-
pub(crate) fn from_file<P: AsRef<Path>>(
501-
path: P,
502-
format_version: FormatVersion,
503-
lib: &mut Plist,
504-
) -> Result<Self, FontInfoLoadError> {
505-
let data = std::fs::read(path.as_ref()).map_err(FontInfoLoadError::Io)?;
506-
Self::from_bytes(&data, format_version, lib)
507-
}
508-
509490
pub(crate) fn from_bytes(
510491
data: &[u8],
511492
format_version: FormatVersion,

0 commit comments

Comments
 (0)