@@ -7,20 +7,22 @@ pub mod transformation;
77use crate :: input:: EventFileReader ;
88use crate :: output:: OutputWriter ;
99use crate :: { args, config} ;
10+ use anyhow:: Context ;
1011use intercept:: { InterceptEnvironment , InterceptService } ;
1112use recognition:: Recognition ;
13+ use std:: io:: BufWriter ;
1214use std:: process:: ExitCode ;
1315use std:: thread;
1416use transformation:: Transformation ;
1517
1618/// The mode trait is used to run the application in different modes.
1719pub trait Mode {
18- fn run ( self ) -> ExitCode ;
20+ fn run ( self ) -> anyhow :: Result < ExitCode > ;
1921}
2022
2123/// The intercept mode we are only capturing the build commands.
2224pub struct Intercept {
23- input : args:: BuildCommand ,
25+ command : args:: BuildCommand ,
2426 output : args:: BuildEvents ,
2527 config : config:: Intercept ,
2628}
@@ -49,40 +51,43 @@ impl Intercept {
4951 config : config:: Intercept ,
5052 ) -> Self {
5153 Self {
52- input,
54+ command : input,
5355 output,
5456 config,
5557 }
5658 }
5759}
5860
5961impl Mode for Intercept {
60- fn run ( self ) -> ExitCode {
62+ fn run ( self ) -> anyhow :: Result < ExitCode > {
6163 match & self . config {
6264 config:: Intercept :: Wrapper { .. } => {
63- let service =
64- InterceptService :: new ( ) . expect ( "Failed to create the intercept service" ) ;
65+ let service = InterceptService :: new ( )
66+ . with_context ( || "Failed to create the intercept service" ) ? ;
6567 let environment = InterceptEnvironment :: new ( & self . config , service. address ( ) )
66- . expect ( "Failed to create the intercept environment" ) ;
68+ . with_context ( || "Failed to create the intercept environment" ) ? ;
6769
6870 // start writer thread
6971 let writer_thread = thread:: spawn ( move || {
70- let mut writer = std:: fs:: File :: create ( self . output . file_name )
71- . expect ( "Failed to create the output file" ) ;
72+ let file = std:: fs:: File :: create ( & self . output . file_name ) . expect (
73+ format ! ( "Failed to create output file: {:?}" , self . output. file_name)
74+ . as_str ( ) ,
75+ ) ;
76+ let mut writer = BufWriter :: new ( file) ;
7277 for envelope in service. receiver ( ) . iter ( ) {
7378 envelope
7479 . write_into ( & mut writer)
7580 . expect ( "Failed to write the envelope" ) ;
7681 }
7782 } ) ;
7883
79- let status = environment. execute_build_command ( self . input ) ;
84+ let status = environment. execute_build_command ( self . command ) ;
8085
8186 writer_thread
8287 . join ( )
8388 . expect ( "Failed to join the writer thread" ) ;
8489
85- status. unwrap_or ( ExitCode :: FAILURE )
90+ status
8691 }
8792 config:: Intercept :: Preload { .. } => {
8893 todo ! ( )
@@ -108,7 +113,7 @@ impl Semantic {
108113}
109114
110115impl Mode for Semantic {
111- fn run ( self ) -> ExitCode {
116+ fn run ( self ) -> anyhow :: Result < ExitCode > {
112117 // Set up the pipeline of compilation database entries.
113118 let entries = self
114119 . event_source
@@ -118,8 +123,8 @@ impl Mode for Semantic {
118123 // Consume the entries and write them to the output file.
119124 // The exit code is based on the result of the output writer.
120125 match self . output_writer . run ( entries) {
121- Ok ( _) => ExitCode :: SUCCESS ,
122- Err ( _) => ExitCode :: FAILURE ,
126+ Ok ( _) => Ok ( ExitCode :: SUCCESS ) ,
127+ Err ( _) => Ok ( ExitCode :: FAILURE ) ,
123128 }
124129 }
125130}
@@ -141,8 +146,8 @@ impl All {
141146}
142147
143148impl Mode for All {
144- fn run ( self ) -> ExitCode {
149+ fn run ( self ) -> anyhow :: Result < ExitCode > {
145150 // TODO: Implement the all mode.
146- ExitCode :: FAILURE
151+ Ok ( ExitCode :: FAILURE )
147152 }
148153}
0 commit comments