@@ -8,7 +8,7 @@ use std::{
88} ;
99
1010use crate :: error:: { StoreEntryError , StoreError } ;
11- use crate :: font_source:: FontSource ;
11+ use crate :: font_source:: { DirEntry , FontSource } ;
1212
1313/// A generic file store for UFO [data][spec_data] and [images][spec_images],
1414/// mapping [`PathBuf`] keys to [`Vec<u8>`] values.
@@ -141,13 +141,14 @@ impl DataType for Data {
141141 . list_dir ( & dir_path)
142142 . map_err ( |e| StoreEntryError :: new ( dir_path. clone ( ) , e. into ( ) ) ) ?;
143143
144- for ( entry_name, is_dir) in entries {
145- let full_rel = dir_path. join ( & entry_name) ;
146- if is_dir {
147- dir_queue. push ( full_rel) ;
148- } else {
149- let key = full_rel. strip_prefix ( source_root) . unwrap ( ) . to_path_buf ( ) ;
150- paths. push ( key) ;
144+ for entry in entries {
145+ match entry {
146+ DirEntry :: Dir ( name) => dir_queue. push ( dir_path. join ( name) ) ,
147+ DirEntry :: File ( name) => {
148+ let full_rel = dir_path. join ( name) ;
149+ let key = full_rel. strip_prefix ( source_root) . unwrap ( ) . to_path_buf ( ) ;
150+ paths. push ( key) ;
151+ }
151152 }
152153 }
153154 }
@@ -190,14 +191,13 @@ impl DataType for Image {
190191 . list_dir ( source_root)
191192 . map_err ( |e| StoreEntryError :: new ( source_root. to_path_buf ( ) , e. into ( ) ) ) ?;
192193
193- for ( entry_name, is_dir) in entries {
194- let full_rel = source_root. join ( & entry_name) ;
195- if is_dir {
194+ for entry in entries {
195+ match entry {
196196 // 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 ) ;
197+ DirEntry :: Dir ( name ) => {
198+ return Err ( StoreEntryError :: new ( name , StoreError :: Subdir ) )
199+ }
200+ DirEntry :: File ( name ) => paths. push ( name ) ,
201201 }
202202 }
203203
@@ -806,21 +806,18 @@ mod tests {
806806
807807 /// A FontSource backed by in-memory data, with `as_path() -> None`.
808808 /// This triggers the eager-loading branch in `Store::new`.
809+ #[ derive( Default ) ]
809810 struct MemorySource {
810811 files : HashMap < PathBuf , Vec < u8 > > ,
811- dirs : HashMap < PathBuf , Vec < ( PathBuf , bool ) > > ,
812+ dirs : HashMap < PathBuf , Vec < DirEntry > > ,
812813 }
813814
814815 impl MemorySource {
815- fn new ( ) -> Self {
816- MemorySource { files : HashMap :: new ( ) , dirs : HashMap :: new ( ) }
817- }
818-
819816 fn add_file ( & mut self , path : impl Into < PathBuf > , data : Vec < u8 > ) {
820817 self . files . insert ( path. into ( ) , data) ;
821818 }
822819
823- fn add_dir ( & mut self , path : impl Into < PathBuf > , entries : Vec < ( PathBuf , bool ) > ) {
820+ fn add_dir ( & mut self , path : impl Into < PathBuf > , entries : Vec < DirEntry > ) {
824821 self . dirs . insert ( path. into ( ) , entries) ;
825822 }
826823 }
@@ -830,7 +827,7 @@ mod tests {
830827 self . files . get ( path) . cloned ( ) . map ( Ok )
831828 }
832829
833- fn list_dir ( & self , path : & Path ) -> Result < Vec < ( PathBuf , bool ) > , std:: io:: Error > {
830+ fn list_dir ( & self , path : & Path ) -> Result < Vec < DirEntry > , std:: io:: Error > {
834831 self . dirs . get ( path) . cloned ( ) . ok_or_else ( || {
835832 std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , format ! ( "{path:?} not found" ) )
836833 } )
@@ -841,10 +838,10 @@ mod tests {
841838
842839 #[ test]
843840 fn data_eager_loading_from_memory_source ( ) {
844- let mut source = MemorySource :: new ( ) ;
841+ let mut source = MemorySource :: default ( ) ;
845842 source. add_dir (
846843 "data" ,
847- vec ! [ ( PathBuf :: from ( "a.txt" ) , false ) , ( PathBuf :: from ( "b.txt" ) , false ) ] ,
844+ vec ! [ DirEntry :: File ( "a.txt" . into ( ) ) , DirEntry :: File ( "b.txt" . into ( ) ) ] ,
848845 ) ;
849846 source. add_file ( "data/a.txt" , b"aaa" . to_vec ( ) ) ;
850847 source. add_file ( "data/b.txt" , b"bbb" . to_vec ( ) ) ;
@@ -865,8 +862,8 @@ mod tests {
865862 #[ test]
866863 fn image_eager_loading_from_memory_source ( ) {
867864 let img = png_data ( b"test" ) ;
868- let mut source = MemorySource :: new ( ) ;
869- source. add_dir ( "images" , vec ! [ ( PathBuf :: from ( "x.png" ) , false ) ] ) ;
865+ let mut source = MemorySource :: default ( ) ;
866+ source. add_dir ( "images" , vec ! [ DirEntry :: File ( "x.png" . into ( ) ) ] ) ;
870867 source. add_file ( "images/x.png" , img. clone ( ) ) ;
871868
872869 let store = ImageStore :: new ( & source) . unwrap ( ) ;
@@ -879,8 +876,8 @@ mod tests {
879876
880877 #[ test]
881878 fn eager_load_invalid_image_fails_at_construction ( ) {
882- let mut source = MemorySource :: new ( ) ;
883- source. add_dir ( "images" , vec ! [ ( PathBuf :: from ( "bad.png" ) , false ) ] ) ;
879+ let mut source = MemorySource :: default ( ) ;
880+ source. add_dir ( "images" , vec ! [ DirEntry :: File ( "bad.png" . into ( ) ) ] ) ;
884881 source. add_file ( "images/bad.png" , b"not a png" . to_vec ( ) ) ;
885882
886883 // Eager loading validates during construction, so this should fail.
@@ -890,10 +887,9 @@ mod tests {
890887
891888 #[ test]
892889 fn eager_load_nested_data_from_memory_source ( ) {
893- let mut source = MemorySource :: new ( ) ;
894- source
895- . add_dir ( "data" , vec ! [ ( PathBuf :: from( "top.txt" ) , false ) , ( PathBuf :: from( "sub" ) , true ) ] ) ;
896- source. add_dir ( "data/sub" , vec ! [ ( PathBuf :: from( "deep.txt" ) , false ) ] ) ;
890+ let mut source = MemorySource :: default ( ) ;
891+ source. add_dir ( "data" , vec ! [ DirEntry :: File ( "top.txt" . into( ) ) , DirEntry :: Dir ( "sub" . into( ) ) ] ) ;
892+ source. add_dir ( "data/sub" , vec ! [ DirEntry :: File ( "deep.txt" . into( ) ) ] ) ;
897893 source. add_file ( "data/top.txt" , b"top" . to_vec ( ) ) ;
898894 source. add_file ( "data/sub/deep.txt" , b"deep" . to_vec ( ) ) ;
899895
0 commit comments