Skip to content

Commit

Permalink
rust: mode run return error
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Nov 16, 2024
1 parent 1dafa26 commit 1be5284
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
5 changes: 3 additions & 2 deletions rust/bear/src/bin/bear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ impl Application {
}

fn run(self) -> ExitCode {
match self {
let status = match self {
Application::Intercept(intercept) => intercept.run(),
Application::Semantic(semantic) => semantic.run(),
Application::All(all) => all.run(),
}
};
status.unwrap_or_else(|_| ExitCode::FAILURE)
}
}
37 changes: 21 additions & 16 deletions rust/bear/src/modes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ pub mod transformation;
use crate::input::EventFileReader;
use crate::output::OutputWriter;
use crate::{args, config};
use anyhow::Context;
use intercept::{InterceptEnvironment, InterceptService};
use recognition::Recognition;
use std::io::BufWriter;
use std::process::ExitCode;
use std::thread;
use transformation::Transformation;

/// The mode trait is used to run the application in different modes.
pub trait Mode {
fn run(self) -> ExitCode;
fn run(self) -> anyhow::Result<ExitCode>;
}

/// The intercept mode we are only capturing the build commands.
pub struct Intercept {
input: args::BuildCommand,
command: args::BuildCommand,
output: args::BuildEvents,
config: config::Intercept,
}
Expand Down Expand Up @@ -49,40 +51,43 @@ impl Intercept {
config: config::Intercept,
) -> Self {
Self {
input,
command: input,
output,
config,
}
}
}

impl Mode for Intercept {
fn run(self) -> ExitCode {
fn run(self) -> anyhow::Result<ExitCode> {
match &self.config {
config::Intercept::Wrapper { .. } => {
let service =
InterceptService::new().expect("Failed to create the intercept service");
let service = InterceptService::new()
.with_context(|| "Failed to create the intercept service")?;
let environment = InterceptEnvironment::new(&self.config, service.address())
.expect("Failed to create the intercept environment");
.with_context(|| "Failed to create the intercept environment")?;

// start writer thread
let writer_thread = thread::spawn(move || {
let mut writer = std::fs::File::create(self.output.file_name)
.expect("Failed to create the output file");
let file = std::fs::File::create(&self.output.file_name).expect(
format!("Failed to create output file: {:?}", self.output.file_name)
.as_str(),
);
let mut writer = BufWriter::new(file);
for envelope in service.receiver().iter() {
envelope
.write_into(&mut writer)
.expect("Failed to write the envelope");
}
});

let status = environment.execute_build_command(self.input);
let status = environment.execute_build_command(self.command);

writer_thread
.join()
.expect("Failed to join the writer thread");

status.unwrap_or(ExitCode::FAILURE)
status
}
config::Intercept::Preload { .. } => {
todo!()
Expand All @@ -108,7 +113,7 @@ impl Semantic {
}

impl Mode for Semantic {
fn run(self) -> ExitCode {
fn run(self) -> anyhow::Result<ExitCode> {
// Set up the pipeline of compilation database entries.
let entries = self
.event_source
Expand All @@ -118,8 +123,8 @@ impl Mode for Semantic {
// Consume the entries and write them to the output file.
// The exit code is based on the result of the output writer.
match self.output_writer.run(entries) {
Ok(_) => ExitCode::SUCCESS,
Err(_) => ExitCode::FAILURE,
Ok(_) => Ok(ExitCode::SUCCESS),
Err(_) => Ok(ExitCode::FAILURE),
}
}
}
Expand All @@ -141,8 +146,8 @@ impl All {
}

impl Mode for All {
fn run(self) -> ExitCode {
fn run(self) -> anyhow::Result<ExitCode> {
// TODO: Implement the all mode.
ExitCode::FAILURE
Ok(ExitCode::FAILURE)
}
}

0 comments on commit 1be5284

Please sign in to comment.