Skip to content

Commit

Permalink
rust: event file read and write using JSON only
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Nov 24, 2024
1 parent 7e3788e commit 79379f2
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 313 deletions.
33 changes: 3 additions & 30 deletions rust/bear/src/bin/bear.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later

use bear::input::EventFileReader;
use bear::modes::recognition::Recognition;
use bear::modes::transformation::Transformation;
use bear::modes::{All, Intercept, Mode, Semantic};
use bear::output::OutputWriter;
use bear::{args, config};
use std::env;
use std::process::ExitCode;
Expand Down Expand Up @@ -53,38 +49,15 @@ impl Application {
match args.mode {
args::Mode::Intercept { input, output } => {
log::debug!("Mode: intercept");
let intercept_config = config.intercept;
let mode = Intercept::new(input, output, intercept_config);
Ok(Application::Intercept(mode))
Intercept::from(input, output, config).map(Application::Intercept)
}
args::Mode::Semantic { input, output } => {
log::debug!("Mode: semantic analysis");
let event_source = EventFileReader::try_from(input)?;
let semantic_recognition = Recognition::try_from(&config)?;
let semantic_transform = Transformation::from(&config.output);
let output_writer = OutputWriter::configure(&output, &config.output)?;
let mode = Semantic::new(
event_source,
semantic_recognition,
semantic_transform,
output_writer,
);
Ok(Application::Semantic(mode))
Semantic::from(input, output, config).map(Application::Semantic)
}
args::Mode::All { input, output } => {
log::debug!("Mode: intercept and semantic analysis");
let semantic_recognition = Recognition::try_from(&config)?;
let semantic_transform = Transformation::from(&config.output);
let output_writer = OutputWriter::configure(&output, &config.output)?;
let intercept_config = config.intercept;
let mode = All::new(
input,
intercept_config,
semantic_recognition,
semantic_transform,
output_writer,
);
Ok(Application::All(mode))
All::from(input, output, config).map(Application::All)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/bear/src/bin/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use anyhow::{Context, Result};
use bear::ipc::tcp::ReporterOnTcp;
use bear::ipc::Reporter;
use bear::ipc::{Event, Execution, ProcessId};
use bear::modes::intercept::KEY_DESTINATION;
use bear::modes::KEY_DESTINATION;
use std::path::{Path, PathBuf};

/// Implementation of the wrapper process.
Expand Down
247 changes: 0 additions & 247 deletions rust/bear/src/input.rs

This file was deleted.

6 changes: 3 additions & 3 deletions rust/bear/src/ipc/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rand::random;

/// Implements convenient methods for the `Envelope` type.
impl Envelope {
pub fn new(rid: &ReporterId, event: Event) -> Self {
fn new(rid: &ReporterId, event: Event) -> Self {
let timestamp = Utc::now().timestamp_millis() as u64;
Envelope {
rid: rid.clone(),
Expand All @@ -27,7 +27,7 @@ impl Envelope {
///
/// The envelope is serialized using JSON and the length of the JSON
/// is written as a 4 byte big-endian integer before the JSON.
pub fn read_from(reader: &mut impl Read) -> Result<Self, anyhow::Error> {
fn read_from(reader: &mut impl Read) -> Result<Self, anyhow::Error> {
let mut length_bytes = [0; 4];
reader.read_exact(&mut length_bytes)?;
let length = u32::from_be_bytes(length_bytes) as usize;
Expand All @@ -43,7 +43,7 @@ impl Envelope {
///
/// The envelope is serialized using JSON and the length of the JSON
/// is written as a 4 byte big-endian integer before the JSON.
pub fn write_into(&self, writer: &mut impl Write) -> Result<u32, anyhow::Error> {
fn write_into(&self, writer: &mut impl Write) -> Result<u32, anyhow::Error> {
let serialized_envelope = serde_json::to_string(&self)?;
let bytes = serialized_envelope.into_bytes();
let length = bytes.len() as u32;
Expand Down
1 change: 0 additions & 1 deletion rust/bear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pub mod args;
pub mod config;
mod fixtures;
pub mod input;
pub mod ipc;
pub mod modes;
pub mod output;
Expand Down
54 changes: 54 additions & 0 deletions rust/bear/src/modes/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: GPL-3.0-or-later

use anyhow::Context;
use serde_json::de::IoRead;
use serde_json::{Error, StreamDeserializer};
use std::fs::OpenOptions;
use std::io::BufReader;
use std::path::PathBuf;

use crate::args;
use crate::ipc::{Envelope, Execution};

/// Responsible for reading the build events from the intercept mode.
///
/// The file syntax is defined by the `events` module, and the parsing logic is implemented there.
/// Here we only handle the file opening and the error handling.
pub struct EventFileReader {
stream: Box<dyn Iterator<Item = Result<Envelope, Error>>>,
}

impl TryFrom<args::BuildEvents> for EventFileReader {
type Error = anyhow::Error;

/// Open the file and create a new instance of the event file reader.
///
/// If the file cannot be opened, the error will be logged and escalated.
fn try_from(value: args::BuildEvents) -> Result<Self, Self::Error> {
let file_name = PathBuf::from(value.file_name);
let file = OpenOptions::new()
.read(true)
.open(file_name.as_path())
.map(BufReader::new)
.with_context(|| format!("Failed to open input file: {:?}", file_name))?;
let stream = Box::new(StreamDeserializer::new(IoRead::new(file)));

Ok(EventFileReader { stream })
}
}

impl EventFileReader {
/// Generate the build events from the file.
///
/// Returns an iterator over the build events. Any error during the reading
/// of the file will be logged and the failed entries will be skipped.
pub fn generate(self) -> impl Iterator<Item = Execution> {
self.stream.filter_map(|result| match result {
Ok(value) => Some(value.event.execution),
Err(error) => {
log::error!("Failed to read event: {:?}", error);
None
}
})
}
}
Loading

0 comments on commit 79379f2

Please sign in to comment.