@@ -16,7 +16,7 @@ use std::{
1616 borrow:: Cow ,
1717 fs:: File ,
1818 io:: { BufReader , Lines , Read , Write } ,
19- path:: { Path , PathBuf } ,
19+ path:: PathBuf ,
2020 str:: FromStr ,
2121 sync:: Mutex ,
2222 time:: { SystemTime , UNIX_EPOCH } ,
@@ -245,32 +245,12 @@ pub fn mkdir<R: Runtime>(
245245#[ serde( rename_all = "camelCase" ) ]
246246#[ non_exhaustive]
247247pub struct DirEntry {
248- pub name : Option < String > ,
248+ pub name : String ,
249249 pub is_directory : bool ,
250250 pub is_file : bool ,
251251 pub is_symlink : bool ,
252252}
253253
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-
274254#[ tauri:: command]
275255pub async fn read_dir < R : Runtime > (
276256 webview : Webview < R > ,
@@ -287,14 +267,37 @@ pub async fn read_dir<R: Runtime>(
287267 options. as_ref ( ) . and_then ( |o| o. base_dir ) ,
288268 ) ?;
289269
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+ } )
296297 } )
297- . map_err ( Into :: into)
298+ . collect ( ) ;
299+
300+ Ok ( entries)
298301}
299302
300303#[ tauri:: command]
0 commit comments