@@ -2,22 +2,30 @@ use std::fs;
22use std:: io:: Read ;
33use std:: path:: Path ;
44
5- pub fn read_directory_contents ( dir : & Path ) -> anyhow:: Result < String > {
5+ pub fn read_contents ( path : & Path ) -> anyhow:: Result < String > {
66 let mut combined_content = String :: new ( ) ;
77
8- if dir . is_file ( ) {
9- read_file ( dir , & mut combined_content) ?;
8+ if path . is_file ( ) {
9+ read_file ( path , & mut combined_content) ?;
1010 } else {
11- read_directory_contents_recursive ( dir , dir , & mut combined_content) ?;
11+ read_directory_contents_recursive ( path , path , & mut combined_content) ?;
1212 }
1313
1414 Ok ( combined_content)
1515}
1616
17- pub fn read_file ( path : & Path , content : & mut String ) -> anyhow:: Result < ( ) > {
18- let file_content = std :: fs :: read_to_string ( path) ? ;
17+ pub fn read_file ( path : & Path , combined_content : & mut String ) -> anyhow:: Result < ( ) > {
18+ combined_content . push_str ( & format ! ( "File: {} \n " , path. display ( ) ) ) ;
1919
20- * content = file_content;
20+ let mut file_content = String :: new ( ) ;
21+ let mut file = fs:: File :: open ( & path) ?;
22+
23+ if let Err ( _) = file. read_to_string ( & mut file_content) {
24+ return Ok ( ( ) ) ;
25+ }
26+
27+ combined_content. push_str ( & file_content) ;
28+ combined_content. push ( '\n' ) ;
2129
2230 Ok ( ( ) )
2331}
@@ -32,18 +40,7 @@ fn read_directory_contents_recursive(
3240 let path = entry. path ( ) ;
3341
3442 if path. is_file ( ) {
35- let relative_path = path. strip_prefix ( base_path) . unwrap ( ) . to_string_lossy ( ) ;
36- combined_content. push_str ( & format ! ( "File: {}\n " , relative_path) ) ;
37-
38- let mut file_content = String :: new ( ) ;
39- let mut file = fs:: File :: open ( & path) ?;
40-
41- if let Err ( _) = file. read_to_string ( & mut file_content) {
42- return Ok ( ( ) ) ;
43- }
44-
45- combined_content. push_str ( & file_content) ;
46- combined_content. push ( '\n' ) ;
43+ read_file ( & path, combined_content) ?;
4744 } else if path. is_dir ( ) {
4845 read_directory_contents_recursive ( base_path, & path, combined_content) ?;
4946 }
0 commit comments