@@ -16,7 +16,7 @@ use std::{
16
16
borrow:: Cow ,
17
17
fs:: File ,
18
18
io:: { BufReader , Lines , Read , Write } ,
19
- path:: { Path , PathBuf } ,
19
+ path:: PathBuf ,
20
20
str:: FromStr ,
21
21
sync:: Mutex ,
22
22
time:: { SystemTime , UNIX_EPOCH } ,
@@ -245,32 +245,12 @@ pub fn mkdir<R: Runtime>(
245
245
#[ serde( rename_all = "camelCase" ) ]
246
246
#[ non_exhaustive]
247
247
pub struct DirEntry {
248
- pub name : Option < String > ,
248
+ pub name : String ,
249
249
pub is_directory : bool ,
250
250
pub is_file : bool ,
251
251
pub is_symlink : bool ,
252
252
}
253
253
254
- fn read_dir_inner < P : AsRef < Path > > ( path : P ) -> crate :: Result < Vec < DirEntry > > {
255
- let mut files_and_dirs: Vec < DirEntry > = vec ! [ ] ;
256
- for entry in std:: fs:: read_dir ( path) ? {
257
- let path = entry?. path ( ) ;
258
- let file_type = path. metadata ( ) ?. file_type ( ) ;
259
- files_and_dirs. push ( DirEntry {
260
- is_directory : file_type. is_dir ( ) ,
261
- is_file : file_type. is_file ( ) ,
262
- is_symlink : std:: fs:: symlink_metadata ( & path)
263
- . map ( |md| md. file_type ( ) . is_symlink ( ) )
264
- . unwrap_or ( false ) ,
265
- name : path
266
- . file_name ( )
267
- . map ( |name| name. to_string_lossy ( ) )
268
- . map ( |name| name. to_string ( ) ) ,
269
- } ) ;
270
- }
271
- Result :: Ok ( files_and_dirs)
272
- }
273
-
274
254
#[ tauri:: command]
275
255
pub async fn read_dir < R : Runtime > (
276
256
webview : Webview < R > ,
@@ -287,14 +267,37 @@ pub async fn read_dir<R: Runtime>(
287
267
options. as_ref ( ) . and_then ( |o| o. base_dir ) ,
288
268
) ?;
289
269
290
- read_dir_inner ( & resolved_path)
291
- . map_err ( |e| {
292
- format ! (
293
- "failed to read directory at path: {} with error: {e}" ,
294
- resolved_path. display( )
295
- )
270
+ let entries = std:: fs:: read_dir ( & resolved_path) . map_err ( |e| {
271
+ format ! (
272
+ "failed to read directory at path: {} with error: {e}" ,
273
+ resolved_path. display( )
274
+ )
275
+ } ) ?;
276
+
277
+ let entries = entries
278
+ . filter_map ( |entry| {
279
+ let entry = entry. ok ( ) ?;
280
+ let name = entry. file_name ( ) . into_string ( ) . ok ( ) ?;
281
+ let metadata = entry. file_type ( ) ;
282
+ macro_rules! method_or_false {
283
+ ( $method: ident) => {
284
+ if let Ok ( metadata) = & metadata {
285
+ metadata. $method( )
286
+ } else {
287
+ false
288
+ }
289
+ } ;
290
+ }
291
+ Some ( DirEntry {
292
+ name,
293
+ is_file : method_or_false ! ( is_file) ,
294
+ is_directory : method_or_false ! ( is_dir) ,
295
+ is_symlink : method_or_false ! ( is_symlink) ,
296
+ } )
296
297
} )
297
- . map_err ( Into :: into)
298
+ . collect ( ) ;
299
+
300
+ Ok ( entries)
298
301
}
299
302
300
303
#[ tauri:: command]
0 commit comments