1- use crate :: fs:: { copy_dir_recursive, get_dist_path} ;
1+ use crate :: fs:: { copy_dir_recursive, get_dist_path, make_dir } ;
22use crate :: types:: OutputFormat ;
33use crate :: utils:: escape_json_values;
4+ use csv:: Writer ;
45use open;
56use serde_json:: Value ;
67use std:: fs:: File ;
78use std:: io:: Write ;
89use std:: path:: { Path , PathBuf } ;
9- use tokio:: fs ;
10+ use tokio:: { fs , task } ;
1011
1112pub async fn generate_output (
1213 output_format : OutputFormat ,
@@ -18,6 +19,8 @@ pub async fn generate_output(
1819 let dist_path: PathBuf = get_dist_path ( )
1920 . expect ( "Error: Could not get current dist path." ) ;
2021
22+ make_dir ( output_directory) . await ;
23+
2124 copy_dir_recursive ( & dist_path, Path :: new ( output_directory) )
2225 . await
2326 . expect ( "Error copying directory" ) ;
@@ -54,13 +57,7 @@ pub async fn generate_output(
5457 }
5558 }
5659 OutputFormat :: Json => {
57- if let Err ( e) = fs:: create_dir_all ( output_directory) . await {
58- eprintln ! (
59- "Error creating output directory {}: {:?}" ,
60- output_directory, e
61- ) ;
62- return ;
63- }
60+ make_dir ( output_directory) . await ;
6461
6562 let json_path = Path :: new ( output_directory) . join ( "index.json" ) ;
6663 let mut file = File :: create ( & json_path)
@@ -71,5 +68,78 @@ pub async fn generate_output(
7168 file. write_all ( formatted_json. as_bytes ( ) )
7269 . expect ( "Failed to write JSON data" ) ;
7370 }
71+ OutputFormat :: Csv => {
72+ make_dir ( output_directory) . await ;
73+
74+ let csv_path = Path :: new ( output_directory) . join ( "index.csv" ) ;
75+
76+ let json_data_clone = json_data. clone ( ) ;
77+
78+ task:: spawn_blocking ( move || {
79+ let file = File :: create ( & csv_path)
80+ . expect ( "Failed to create CSV report file" ) ;
81+
82+ let mut wtr = Writer :: from_writer ( file) ;
83+
84+ if let Some ( data_array) =
85+ json_data_clone. get ( "data" ) . and_then ( |d| d. as_array ( ) )
86+ {
87+ wtr. write_record ( & [
88+ "Path" ,
89+ "Line" ,
90+ "Kind" ,
91+ "Comment" ,
92+ "Author" ,
93+ "Date" ,
94+ "Commit Hash" ,
95+ ] )
96+ . expect ( "Failed to write CSV headers" ) ;
97+
98+ for item in data_array {
99+ let path = item
100+ . get ( "path" )
101+ . and_then ( |v| v. as_str ( ) )
102+ . unwrap_or ( "" ) ;
103+ let line = item
104+ . get ( "line" )
105+ . and_then ( |v| v. as_i64 ( ) )
106+ . unwrap_or ( 0 )
107+ . to_string ( ) ;
108+ let kind = item
109+ . get ( "kind" )
110+ . and_then ( |v| v. as_str ( ) )
111+ . unwrap_or ( "" ) ;
112+ let comment = item
113+ . get ( "comment" )
114+ . and_then ( |v| v. as_str ( ) )
115+ . unwrap_or ( "" ) ;
116+
117+ let blame = item. get ( "blame" ) . unwrap_or ( & Value :: Null ) ;
118+ let author = blame
119+ . get ( "author" )
120+ . and_then ( |v| v. as_str ( ) )
121+ . unwrap_or ( "" ) ;
122+ let date = blame
123+ . get ( "date" )
124+ . and_then ( |v| v. as_str ( ) )
125+ . unwrap_or ( "" ) ;
126+ let hash = blame
127+ . get ( "hash" )
128+ . and_then ( |v| v. as_str ( ) )
129+ . unwrap_or ( "" ) ;
130+
131+ wtr. write_record ( & [
132+ path, & line, kind, comment, author, date, hash,
133+ ] )
134+ . expect ( "Failed to write CSV record" ) ;
135+ }
136+ } else {
137+ eprintln ! ( "No data found in json_data" ) ;
138+ }
139+ wtr. flush ( ) . expect ( "Failed to flush CSV writer" ) ;
140+ } )
141+ . await
142+ . expect ( "Failed to write CSV data" ) ;
143+ }
74144 }
75145}
0 commit comments