@@ -5,27 +5,51 @@ use std::{path::{Path, PathBuf}, str::Utf8Error};
5
5
6
6
use crate :: zip:: Zip ;
7
7
8
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
9
+ pub enum FileType {
10
+ File ,
11
+ Directory ,
12
+ }
13
+
8
14
#[ derive( Clone , Debug , Deserialize , PartialEq ) ]
9
15
#[ serde( rename_all = "camelCase" ) ]
10
- pub struct VPathInfo {
16
+ pub struct ZipInfo {
11
17
pub base_path : String ,
12
18
pub virtual_segments : Option < ( String , String ) > ,
13
- pub zip_path : Option < String > ,
19
+ pub zip_path : String ,
20
+ }
21
+
22
+ #[ derive( Clone , Debug , Deserialize , PartialEq ) ]
23
+ #[ serde( rename_all = "camelCase" ) ]
24
+ pub struct VirtualInfo {
25
+ pub base_path : String ,
26
+ pub virtual_segments : ( String , String ) ,
14
27
}
15
28
16
- impl VPathInfo {
17
- pub fn physical_base_path ( & self ) -> PathBuf {
29
+ pub trait VPathInfo {
30
+ fn physical_base_path ( & self ) -> PathBuf ;
31
+ }
32
+
33
+ impl VPathInfo for ZipInfo {
34
+ fn physical_base_path ( & self ) -> PathBuf {
18
35
match & self . virtual_segments {
19
36
None => PathBuf :: from ( & self . base_path ) ,
20
37
Some ( segments) => PathBuf :: from ( & self . base_path ) . join ( & segments. 1 ) ,
21
38
}
22
39
}
23
40
}
24
41
42
+ impl VPathInfo for VirtualInfo {
43
+ fn physical_base_path ( & self ) -> PathBuf {
44
+ PathBuf :: from ( & self . base_path ) . join ( & self . virtual_segments . 1 )
45
+ }
46
+ }
47
+
25
48
#[ derive( Clone , Debug , Deserialize , PartialEq ) ]
26
49
#[ serde( untagged) ]
27
50
pub enum VPath {
28
- Virtual ( VPathInfo ) ,
51
+ Zip ( ZipInfo ) ,
52
+ Virtual ( VirtualInfo ) ,
29
53
Native ( PathBuf ) ,
30
54
}
31
55
@@ -107,11 +131,7 @@ pub trait ZipCache<Storage>
107
131
where Storage : AsRef < [ u8 ] > + Send + Sync {
108
132
fn act < T , P : AsRef < Path > , F : FnOnce ( & Zip < Storage > ) -> T > ( & self , p : P , cb : F ) -> Result < T , std:: io:: Error > ;
109
133
110
- fn canonicalize < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , sub : S ) -> Result < PathBuf , std:: io:: Error > ;
111
-
112
- fn is_dir < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , sub : S ) -> bool ;
113
- fn is_file < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , sub : S ) -> bool ;
114
-
134
+ fn file_type < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , sub : S ) -> Result < FileType , std:: io:: Error > ;
115
135
fn read < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , sub : S ) -> Result < Vec < u8 > , std:: io:: Error > ;
116
136
fn read_to_string < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , sub : S ) -> Result < String , std:: io:: Error > ;
117
137
}
@@ -143,18 +163,8 @@ where Storage: AsRef<[u8]> + Send + Sync {
143
163
Ok ( cb ( zip. value ( ) ) )
144
164
}
145
165
146
- fn canonicalize < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , sub : S ) -> Result < PathBuf , std:: io:: Error > {
147
- let res = std:: fs:: canonicalize ( zip_path) ?;
148
-
149
- Ok ( res. join ( sub. as_ref ( ) ) )
150
- }
151
-
152
- fn is_dir < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , p : S ) -> bool {
153
- self . act ( zip_path, |zip| zip. is_dir ( p. as_ref ( ) ) ) . unwrap_or ( false )
154
- }
155
-
156
- fn is_file < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , p : S ) -> bool {
157
- self . act ( zip_path, |zip| zip. is_file ( p. as_ref ( ) ) ) . unwrap_or ( false )
166
+ fn file_type < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , p : S ) -> Result < FileType , std:: io:: Error > {
167
+ self . act ( zip_path, |zip| zip. file_type ( p. as_ref ( ) ) ) ?
158
168
}
159
169
160
170
fn read < P : AsRef < Path > , S : AsRef < str > > ( & self , zip_path : P , p : S ) -> Result < Vec < u8 > , std:: io:: Error > {
@@ -261,13 +271,18 @@ fn vpath(p: &Path) -> std::io::Result<VPath> {
261
271
return Ok ( VPath :: Native ( PathBuf :: from ( p_str) ) ) ;
262
272
}
263
273
264
- Ok ( VPath :: Virtual ( VPathInfo {
265
- base_path : io_bytes_to_str ( base_path_u8) ?. to_string ( ) ,
266
- virtual_segments,
267
- zip_path : zip_path_u8. map ( |data| {
268
- io_bytes_to_str ( data) . map ( |str| str. to_string ( ) )
269
- } ) . transpose ( ) ?,
270
- } ) )
274
+ if let Some ( zip_path_u8) = zip_path_u8 {
275
+ Ok ( VPath :: Zip ( ZipInfo {
276
+ base_path : io_bytes_to_str ( base_path_u8) ?. to_string ( ) ,
277
+ virtual_segments,
278
+ zip_path : io_bytes_to_str ( zip_path_u8) ?. to_string ( ) ,
279
+ } ) )
280
+ } else {
281
+ Ok ( VPath :: Virtual ( VirtualInfo {
282
+ base_path : io_bytes_to_str ( base_path_u8) ?. to_string ( ) ,
283
+ virtual_segments : virtual_segments. unwrap ( ) ,
284
+ } ) )
285
+ }
271
286
}
272
287
273
288
#[ cfg( test) ]
0 commit comments