@@ -2,22 +2,30 @@ use std::fs;
2
2
use std:: io:: Read ;
3
3
use std:: path:: Path ;
4
4
5
- pub fn read_directory_contents ( dir : & Path ) -> anyhow:: Result < String > {
5
+ pub fn read_contents ( path : & Path ) -> anyhow:: Result < String > {
6
6
let mut combined_content = String :: new ( ) ;
7
7
8
- if dir . is_file ( ) {
9
- read_file ( dir , & mut combined_content) ?;
8
+ if path . is_file ( ) {
9
+ read_file ( path , & mut combined_content) ?;
10
10
} else {
11
- read_directory_contents_recursive ( dir , dir , & mut combined_content) ?;
11
+ read_directory_contents_recursive ( path , path , & mut combined_content) ?;
12
12
}
13
13
14
14
Ok ( combined_content)
15
15
}
16
16
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 ( ) ) ) ;
19
19
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' ) ;
21
29
22
30
Ok ( ( ) )
23
31
}
@@ -32,18 +40,7 @@ fn read_directory_contents_recursive(
32
40
let path = entry. path ( ) ;
33
41
34
42
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) ?;
47
44
} else if path. is_dir ( ) {
48
45
read_directory_contents_recursive ( base_path, & path, combined_content) ?;
49
46
}
0 commit comments