@@ -65,38 +65,48 @@ impl AuditService {
6565 // Check for OpenAI API key if AI analysis is enabled
6666 if request. ai_analysis {
6767 match std:: env:: var ( "OPENAI_API_KEY" ) {
68- Ok ( key) if !key. is_empty ( ) => { } ,
69- Ok ( _) => return Err ( AuditError :: EnvironmentError (
70- "OPENAI_API_KEY environment variable is empty" . to_string ( )
71- ) ) ,
72- Err ( _) => return Err ( AuditError :: EnvironmentError (
73- "OPENAI_API_KEY environment variable not found" . to_string ( )
74- ) ) ,
68+ Ok ( key) if !key. is_empty ( ) => { }
69+ Ok ( _) => {
70+ return Err ( AuditError :: EnvironmentError (
71+ "OPENAI_API_KEY environment variable is empty" . to_string ( ) ,
72+ ) )
73+ }
74+ Err ( _) => {
75+ return Err ( AuditError :: EnvironmentError (
76+ "OPENAI_API_KEY environment variable not found" . to_string ( ) ,
77+ ) )
78+ }
7579 }
7680 }
7781
7882 // Validate output format
7983 match request. format . as_str ( ) {
80- "typst" | "pdf" | "both" => { } ,
81- _ => return Err ( AuditError :: ConfigurationError (
82- format ! ( "Invalid format '{}'. Valid formats: typst, pdf, both" , request. format)
83- ) ) ,
84+ "typst" | "pdf" | "both" => { }
85+ _ => {
86+ return Err ( AuditError :: ConfigurationError ( format ! (
87+ "Invalid format '{}'. Valid formats: typst, pdf, both" ,
88+ request. format
89+ ) ) )
90+ }
8491 }
8592
8693 Ok ( ( ) )
8794 }
8895
8996 pub fn prepare_output_directory ( output_dir : & str ) -> Result < ( ) , AuditError > {
9097 fs:: create_dir_all ( output_dir) . map_err ( |e| {
91- AuditError :: OutputError ( format ! ( "Failed to create output directory '{}': {}" , output_dir, e) )
98+ AuditError :: OutputError ( format ! (
99+ "Failed to create output directory '{}': {}" ,
100+ output_dir, e
101+ ) )
92102 } ) ?;
93103 Ok ( ( ) )
94104 }
95105
96106 pub async fn execute_audit ( & self , request : & AuditRequest ) -> Result < AuditResult , AuditError > {
97107 // Validate environment first
98108 Self :: validate_environment ( request) ?;
99-
109+
100110 // Prepare output directory
101111 Self :: prepare_output_directory ( & request. output_dir ) ?;
102112
@@ -122,10 +132,14 @@ impl AuditService {
122132 if request. verbose > 0 {
123133 println ! ( "🐙 GitHub repository audit mode" ) ;
124134 }
125-
126- self . coordinator . audit_github_repository ( repo_spec) . await
127- . map_err ( |e| AuditError :: AuditExecutionError ( format ! ( "GitHub audit failed: {}" , e) ) ) ?;
128-
135+
136+ self . coordinator
137+ . audit_github_repository ( repo_spec)
138+ . await
139+ . map_err ( |e| {
140+ AuditError :: AuditExecutionError ( format ! ( "GitHub audit failed: {}" , e) )
141+ } ) ?;
142+
129143 // For GitHub mode, we return early as files are already generated and committed
130144 return Ok ( AuditResult {
131145 success : true ,
@@ -145,15 +159,19 @@ impl AuditService {
145159 self . coordinator . create_test_audit_report ( )
146160 } else {
147161 // Run security audit
148- self . coordinator . run_security_audit ( ) . await
149- . map_err ( |e| AuditError :: AuditExecutionError ( format ! ( "Security audit failed: {}" , e) ) ) ?
162+ self . coordinator . run_security_audit ( ) . await . map_err ( |e| {
163+ AuditError :: AuditExecutionError ( format ! ( "Security audit failed: {}" , e) )
164+ } ) ?
150165 } ;
151166
152167 if request. verbose > 0 {
153168 println ! ( "✅ Security audit completed successfully" ) ;
154- println ! ( "📊 Security Score: {:.1}/100" , report. summary. security_score) ;
169+ println ! (
170+ "📊 Security Score: {:.1}/100" ,
171+ report. summary. security_score
172+ ) ;
155173 println ! ( "🔍 Total Findings: {}" , report. summary. total_findings) ;
156-
174+
157175 if report. summary . critical_findings > 0 {
158176 println ! ( "🔴 Critical: {}" , report. summary. critical_findings) ;
159177 }
@@ -171,78 +189,101 @@ impl AuditService {
171189 // Generate outputs based on requested format
172190 let mut output_files = Vec :: new ( ) ;
173191 let timestamp = chrono:: Utc :: now ( ) . format ( "%Y%m%d_%H%M%S" ) ;
174- let typst_path = Path :: new ( & request. output_dir ) . join ( format ! ( "osvm_audit_report_{}.typ" , timestamp) ) ;
175- let pdf_path = Path :: new ( & request. output_dir ) . join ( format ! ( "osvm_audit_report_{}.pdf" , timestamp) ) ;
176-
192+ let typst_path =
193+ Path :: new ( & request. output_dir ) . join ( format ! ( "osvm_audit_report_{}.typ" , timestamp) ) ;
194+ let pdf_path =
195+ Path :: new ( & request. output_dir ) . join ( format ! ( "osvm_audit_report_{}.pdf" , timestamp) ) ;
196+
177197 match request. format . as_str ( ) {
178198 "typst" | "both" => {
179- self . coordinator . generate_typst_document ( & report, & typst_path)
180- . map_err ( |e| AuditError :: OutputError ( format ! ( "Failed to generate Typst document: {}" , e) ) ) ?;
181-
199+ self . coordinator
200+ . generate_typst_document ( & report, & typst_path)
201+ . map_err ( |e| {
202+ AuditError :: OutputError ( format ! ( "Failed to generate Typst document: {}" , e) )
203+ } ) ?;
204+
182205 if request. verbose > 0 {
183206 println ! ( "📄 Typst document generated: {}" , typst_path. display( ) ) ;
184207 }
185208 output_files. push ( typst_path. to_string_lossy ( ) . to_string ( ) ) ;
186-
209+
187210 if request. format == "both" {
188211 match self . coordinator . compile_to_pdf ( & typst_path, & pdf_path) {
189212 Ok ( _) => {
190213 if request. verbose > 0 {
191214 println ! ( "📋 PDF report generated: {}" , pdf_path. display( ) ) ;
192215 }
193216 output_files. push ( pdf_path. to_string_lossy ( ) . to_string ( ) ) ;
194- } ,
217+ }
195218 Err ( e) => {
196219 eprintln ! ( "❌ Failed to compile PDF: {}" , e) ;
197- eprintln ! ( " Typst document is available at: {}" , typst_path. display( ) ) ;
198- eprintln ! ( " You can compile it manually using: typst compile {}" , typst_path. display( ) ) ;
220+ eprintln ! (
221+ " Typst document is available at: {}" ,
222+ typst_path. display( )
223+ ) ;
224+ eprintln ! (
225+ " You can compile it manually using: typst compile {}" ,
226+ typst_path. display( )
227+ ) ;
199228 }
200229 }
201230 }
202231 }
203232 "pdf" => {
204233 // Generate Typst document first (temporary)
205- self . coordinator . generate_typst_document ( & report, & typst_path)
206- . map_err ( |e| AuditError :: OutputError ( format ! ( "Failed to generate Typst document: {}" , e) ) ) ?;
207-
208- self . coordinator . compile_to_pdf ( & typst_path, & pdf_path)
209- . map_err ( |e| AuditError :: OutputError ( format ! ( "Failed to compile PDF: {}" , e) ) ) ?;
210-
234+ self . coordinator
235+ . generate_typst_document ( & report, & typst_path)
236+ . map_err ( |e| {
237+ AuditError :: OutputError ( format ! ( "Failed to generate Typst document: {}" , e) )
238+ } ) ?;
239+
240+ self . coordinator
241+ . compile_to_pdf ( & typst_path, & pdf_path)
242+ . map_err ( |e| {
243+ AuditError :: OutputError ( format ! ( "Failed to compile PDF: {}" , e) )
244+ } ) ?;
245+
211246 // Remove temporary Typst file
212247 let _ = fs:: remove_file ( & typst_path) ;
213-
248+
214249 if request. verbose > 0 {
215250 println ! ( "📋 PDF report generated: {}" , pdf_path. display( ) ) ;
216251 }
217252 output_files. push ( pdf_path. to_string_lossy ( ) . to_string ( ) ) ;
218253 }
219- _ => return Err ( AuditError :: ConfigurationError (
220- format ! ( "Invalid format specified: {}" , request. format)
221- ) ) ,
254+ _ => {
255+ return Err ( AuditError :: ConfigurationError ( format ! (
256+ "Invalid format specified: {}" ,
257+ request. format
258+ ) ) )
259+ }
222260 }
223261
224262 if request. verbose > 0 {
225263 println ! ( "\n 📋 Audit Summary:" ) ;
226264 println ! ( " Compliance Level: {}" , report. summary. compliance_level) ;
227- println ! ( " System: {} {}" , report. system_info. os_info, report. system_info. architecture) ;
265+ println ! (
266+ " System: {} {}" ,
267+ report. system_info. os_info, report. system_info. architecture
268+ ) ;
228269 println ! ( " Rust Version: {}" , report. system_info. rust_version) ;
229270 if let Some ( ref solana_version) = report. system_info . solana_version {
230271 println ! ( " Solana Version: {}" , solana_version) ;
231272 }
232-
273+
233274 println ! ( "\n 💡 To view the full report, open the generated file(s):" ) ;
234275 for file in & output_files {
235276 println ! ( " 📁 {}" , file) ;
236277 }
237278 }
238279
239280 // Check if exit with error code is needed (critical or high findings)
240- let has_serious_findings = !request. test_mode &&
241- ( report. summary . critical_findings > 0 || report. summary . high_findings > 0 ) ;
281+ let has_serious_findings = !request. test_mode
282+ && ( report. summary . critical_findings > 0 || report. summary . high_findings > 0 ) ;
242283
243284 Ok ( AuditResult {
244285 success : !has_serious_findings,
245- security_score : report. summary . security_score ,
286+ security_score : report. summary . security_score as f64 ,
246287 total_findings : report. summary . total_findings ,
247288 critical_findings : report. summary . critical_findings ,
248289 high_findings : report. summary . high_findings ,
@@ -252,4 +293,4 @@ impl AuditService {
252293 output_files,
253294 } )
254295 }
255- }
296+ }
0 commit comments