@@ -3,7 +3,9 @@ use number_prefix::NumberPrefix;
33use std:: path:: PathBuf ;
44use std:: { fs, io} ;
55
6- use crate :: command:: DirectoryEnum ;
6+ use crate :: command:: { DirectoryEnum , LanguageEnum } ;
7+
8+ pub type PathsResult = io:: Result < Vec < Result < String , io:: Error > > > ;
79
810#[ derive( Debug , Copy , Clone ) ]
911pub struct DirInfo {
@@ -38,90 +40,90 @@ impl DirInfo {
3840 NumberPrefix :: Standalone ( bytes) => format ! ( "{bytes} bytes" ) ,
3941 }
4042 }
41- }
42-
43- fn is_valid_target ( path : PathBuf , directory : & DirectoryEnum ) -> bool {
44- if directory == & DirectoryEnum :: Target {
45- let file_path = path. join ( ".rustc_info.json" ) ;
46- return file_path. exists ( ) ;
47- }
4843
49- true
50- }
44+ fn is_valid_target ( path : PathBuf , directory : & DirectoryEnum ) -> bool {
45+ if directory == & DirectoryEnum :: Target {
46+ let file_path = path. join ( ".rustc_info.json" ) ;
47+ return file_path. exists ( ) ;
48+ }
5149
52- pub type PathsResult = io:: Result < Vec < Result < String , io:: Error > > > ;
50+ true
51+ }
5352
54- pub fn get_paths_to_delete ( path : impl Into < PathBuf > , directory : & DirectoryEnum ) -> PathsResult {
55- fn walk ( dir : io:: Result < fs:: ReadDir > , directory : & DirectoryEnum ) -> PathsResult {
56- let mut dir = match dir {
57- Ok ( dir) => dir,
58- Err ( e) => {
59- return Ok ( vec ! [ Err ( e) ] ) ;
60- }
61- } ;
53+ pub fn get_paths_to_delete ( path : impl Into < PathBuf > , language : & LanguageEnum ) -> PathsResult {
54+ let directory: DirectoryEnum = language. clone ( ) . into ( ) ;
6255
63- dir. try_fold (
64- Vec :: new ( ) ,
65- |mut acc : Vec < Result < String , io:: Error > > , file| {
66- let file = file?;
56+ fn walk ( dir : io:: Result < fs:: ReadDir > , directory : & DirectoryEnum ) -> PathsResult {
57+ let mut dir = match dir {
58+ Ok ( dir) => dir,
59+ Err ( e) => {
60+ return Ok ( vec ! [ Err ( e) ] ) ;
61+ }
62+ } ;
6763
68- let size = match file. metadata ( ) {
69- Ok ( data) if data. is_dir ( ) => {
70- if file. file_name ( ) == directory. to_string ( ) [ ..] {
71- if is_valid_target ( file. path ( ) , directory) {
72- acc. push ( Ok ( file. path ( ) . display ( ) . to_string ( ) ) ) ;
64+ dir. try_fold (
65+ Vec :: new ( ) ,
66+ |mut acc : Vec < Result < String , io:: Error > > , file| {
67+ let file = file?;
68+
69+ let size = match file. metadata ( ) {
70+ Ok ( data) if data. is_dir ( ) => {
71+ if file. file_name ( ) == directory. to_string ( ) [ ..] {
72+ if DirInfo :: is_valid_target ( file. path ( ) , directory) {
73+ acc. push ( Ok ( file. path ( ) . display ( ) . to_string ( ) ) ) ;
74+ }
75+ } else {
76+ acc. append ( & mut walk ( fs:: read_dir ( file. path ( ) ) , directory) ?) ;
7377 }
74- } else {
75- acc. append ( & mut walk ( fs:: read_dir ( file. path ( ) ) , directory) ?) ;
78+ acc
7679 }
77- acc
78- }
79- _ => acc,
80- } ;
80+ _ => acc,
81+ } ;
82+
83+ Ok ( size)
84+ } ,
85+ )
86+ }
8187
82- Ok ( size)
83- } ,
84- )
88+ walk ( fs:: read_dir ( path. into ( ) ) , & directory)
8589 }
8690
87- walk ( fs:: read_dir ( path. into ( ) ) , directory)
88- }
91+ pub fn dir_size ( path : impl Into < PathBuf > ) -> io:: Result < DirInfo > {
92+ fn walk ( dir : io:: Result < fs:: ReadDir > ) -> io:: Result < DirInfo > {
93+ let mut dir = match dir {
94+ Ok ( dir) => dir,
95+ Err ( _) => {
96+ // Return empty stats for unreadable directories instead of failing
97+ return Ok ( DirInfo :: new ( 0 , 0 , 0 ) ) ;
98+ }
99+ } ;
89100
90- pub fn dir_size ( path : impl Into < PathBuf > ) -> io:: Result < DirInfo > {
91- fn walk ( dir : io:: Result < fs:: ReadDir > ) -> io:: Result < DirInfo > {
92- let mut dir = match dir {
93- Ok ( dir) => dir,
94- Err ( _) => {
95- // Return empty stats for unreadable directories instead of failing
96- return Ok ( DirInfo :: new ( 0 , 0 , 0 ) ) ;
97- }
98- } ;
101+ dir. try_fold ( DirInfo :: new ( 0 , 0 , 0 ) , |acc, file| {
102+ let file = file?;
99103
100- dir. try_fold ( DirInfo :: new ( 0 , 0 , 0 ) , |acc, file| {
101- let file = file?;
104+ let info = match file. metadata ( ) {
105+ // For directories: count 1 directory + recursively count its contents
106+ Ok ( data) if data. is_dir ( ) => {
107+ let sub_info = walk ( fs:: read_dir ( file. path ( ) ) ) ?;
108+ DirInfo :: new ( 1 + sub_info. dir_count , sub_info. file_count , sub_info. size )
109+ }
110+ // For files: count 1 file and its size in bytes
111+ Ok ( data) => DirInfo :: new ( 0 , 1 , data. len ( ) as usize ) ,
112+ // Skip entries we can't read metadata for
113+ _ => DirInfo :: new ( 0 , 0 , 0 ) ,
114+ } ;
102115
103- let info = match file. metadata ( ) {
104- // For directories: count 1 directory + recursively count its contents
105- Ok ( data) if data. is_dir ( ) => {
106- let sub_info = walk ( fs:: read_dir ( file. path ( ) ) ) ?;
107- DirInfo :: new ( 1 + sub_info. dir_count , sub_info. file_count , sub_info. size )
108- }
109- // For files: count 1 file and its size in bytes
110- Ok ( data) => DirInfo :: new ( 0 , 1 , data. len ( ) as usize ) ,
111- // Skip entries we can't read metadata for
112- _ => DirInfo :: new ( 0 , 0 , 0 ) ,
113- } ;
116+ // Accumulate counts from this entry with running totals
117+ Ok ( DirInfo :: new (
118+ acc. dir_count + info. dir_count ,
119+ acc. file_count + info. file_count ,
120+ acc. size + info. size ,
121+ ) )
122+ } )
123+ }
114124
115- // Accumulate counts from this entry with running totals
116- Ok ( DirInfo :: new (
117- acc. dir_count + info. dir_count ,
118- acc. file_count + info. file_count ,
119- acc. size + info. size ,
120- ) )
121- } )
125+ walk ( fs:: read_dir ( path. into ( ) ) )
122126 }
123-
124- walk ( fs:: read_dir ( path. into ( ) ) )
125127}
126128
127129#[ cfg( test) ]
0 commit comments