Skip to content

Commit 1be5284

Browse files
committed
rust: mode run return error
1 parent 1dafa26 commit 1be5284

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

rust/bear/src/bin/bear.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ impl Application {
8080
}
8181

8282
fn run(self) -> ExitCode {
83-
match self {
83+
let status = match self {
8484
Application::Intercept(intercept) => intercept.run(),
8585
Application::Semantic(semantic) => semantic.run(),
8686
Application::All(all) => all.run(),
87-
}
87+
};
88+
status.unwrap_or_else(|_| ExitCode::FAILURE)
8889
}
8990
}

rust/bear/src/modes/mod.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ pub mod transformation;
77
use crate::input::EventFileReader;
88
use crate::output::OutputWriter;
99
use crate::{args, config};
10+
use anyhow::Context;
1011
use intercept::{InterceptEnvironment, InterceptService};
1112
use recognition::Recognition;
13+
use std::io::BufWriter;
1214
use std::process::ExitCode;
1315
use std::thread;
1416
use transformation::Transformation;
1517

1618
/// The mode trait is used to run the application in different modes.
1719
pub 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.
2224
pub 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

5961
impl 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

110115
impl 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

143148
impl 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

Comments
 (0)