11use std:: fs:: File ;
22use std:: io:: { BufRead , BufReader } ;
3- use std:: path:: PathBuf ;
3+ use std:: path:: { Path , PathBuf } ;
44use std:: process;
55
66use anyhow:: { anyhow, bail, Context , Result } ;
77use chrono:: Utc ;
88use clap:: Parser ;
99use serde:: Deserialize ;
1010
11- const APP_VERSION : & str = "0.4.0" ;
12-
1311#[ derive( Parser , Debug , Clone ) ]
14- #[ command( version = APP_VERSION , about = "Convert npm audit JSON lines into a Markdown summary." ) ]
12+ #[ command(
13+ version,
14+ about = "Convert npm audit JSON lines into a Markdown summary."
15+ ) ]
1516struct Cli {
1617 #[ arg(
1718 short = 'i' ,
@@ -88,15 +89,21 @@ struct Vulnerabilities {
8889 critical : i64 ,
8990}
9091
91- fn parse_json ( path : & PathBuf ) -> Result < Vec < AuditLine > > {
92- let file = File :: open ( path)
93- . with_context ( || format ! ( "unable to open audit file {}" , path. display( ) ) ) ?;
92+ fn parse_json ( path : impl AsRef < Path > ) -> Result < Vec < AuditLine > > {
93+ let path_ref = path. as_ref ( ) ;
94+ let file = File :: open ( path_ref)
95+ . with_context ( || format ! ( "unable to open audit file {}" , path_ref. display( ) ) ) ?;
9496 let reader = BufReader :: new ( file) ;
9597 let mut lines = Vec :: new ( ) ;
9698
9799 for ( index, line_result) in reader. lines ( ) . enumerate ( ) {
98- let line = line_result
99- . with_context ( || format ! ( "error reading line {} from {}" , index + 1 , path. display( ) ) ) ?;
100+ let line = line_result. with_context ( || {
101+ format ! (
102+ "error reading line {} from {}" ,
103+ index + 1 ,
104+ path_ref. display( )
105+ )
106+ } ) ?;
100107 if line. trim ( ) . is_empty ( ) {
101108 continue ;
102109 }
@@ -105,7 +112,7 @@ fn parse_json(path: &PathBuf) -> Result<Vec<AuditLine>> {
105112 format ! (
106113 "error parsing JSON on line {} of {}" ,
107114 index + 1 ,
108- path . display( )
115+ path_ref . display( )
109116 )
110117 } ) ?;
111118 lines. push ( parsed) ;
@@ -234,7 +241,7 @@ mod tests {
234241 writeln ! ( file, "{}" , sample_summary_line( ) ) . unwrap ( ) ;
235242 writeln ! ( file, "{}" , sample_advisory_line( ) ) . unwrap ( ) ;
236243
237- let lines = parse_json ( & file. path ( ) . to_path_buf ( ) ) . expect ( "parse JSON lines" ) ;
244+ let lines = parse_json ( file. path ( ) ) . expect ( "parse JSON lines" ) ;
238245 assert_eq ! ( lines. len( ) , 2 ) ;
239246 assert_eq ! ( lines[ 0 ] . kind, "auditSummary" ) ;
240247 assert_eq ! ( lines[ 1 ] . kind, "auditAdvisory" ) ;
@@ -243,7 +250,7 @@ mod tests {
243250 #[ test]
244251 fn parse_json_errors_on_empty_file ( ) {
245252 let file = NamedTempFile :: new ( ) . expect ( "create temp file" ) ;
246- let err = parse_json ( & file. path ( ) . to_path_buf ( ) ) . expect_err ( "expected error" ) ;
253+ let err = parse_json ( file. path ( ) ) . expect_err ( "expected error" ) ;
247254 assert_eq ! ( err. root_cause( ) . to_string( ) , "no data in the audit file" ) ;
248255 }
249256
0 commit comments