Skip to content

Commit 79379f2

Browse files
committed
rust: event file read and write using JSON only
1 parent 7e3788e commit 79379f2

File tree

8 files changed

+102
-313
lines changed

8 files changed

+102
-313
lines changed

rust/bear/src/bin/bear.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22

3-
use bear::input::EventFileReader;
4-
use bear::modes::recognition::Recognition;
5-
use bear::modes::transformation::Transformation;
63
use bear::modes::{All, Intercept, Mode, Semantic};
7-
use bear::output::OutputWriter;
84
use bear::{args, config};
95
use std::env;
106
use std::process::ExitCode;
@@ -53,38 +49,15 @@ impl Application {
5349
match args.mode {
5450
args::Mode::Intercept { input, output } => {
5551
log::debug!("Mode: intercept");
56-
let intercept_config = config.intercept;
57-
let mode = Intercept::new(input, output, intercept_config);
58-
Ok(Application::Intercept(mode))
52+
Intercept::from(input, output, config).map(Application::Intercept)
5953
}
6054
args::Mode::Semantic { input, output } => {
6155
log::debug!("Mode: semantic analysis");
62-
let event_source = EventFileReader::try_from(input)?;
63-
let semantic_recognition = Recognition::try_from(&config)?;
64-
let semantic_transform = Transformation::from(&config.output);
65-
let output_writer = OutputWriter::configure(&output, &config.output)?;
66-
let mode = Semantic::new(
67-
event_source,
68-
semantic_recognition,
69-
semantic_transform,
70-
output_writer,
71-
);
72-
Ok(Application::Semantic(mode))
56+
Semantic::from(input, output, config).map(Application::Semantic)
7357
}
7458
args::Mode::All { input, output } => {
7559
log::debug!("Mode: intercept and semantic analysis");
76-
let semantic_recognition = Recognition::try_from(&config)?;
77-
let semantic_transform = Transformation::from(&config.output);
78-
let output_writer = OutputWriter::configure(&output, &config.output)?;
79-
let intercept_config = config.intercept;
80-
let mode = All::new(
81-
input,
82-
intercept_config,
83-
semantic_recognition,
84-
semantic_transform,
85-
output_writer,
86-
);
87-
Ok(Application::All(mode))
60+
All::from(input, output, config).map(Application::All)
8861
}
8962
}
9063
}

rust/bear/src/bin/wrapper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use anyhow::{Context, Result};
2121
use bear::ipc::tcp::ReporterOnTcp;
2222
use bear::ipc::Reporter;
2323
use bear::ipc::{Event, Execution, ProcessId};
24-
use bear::modes::intercept::KEY_DESTINATION;
24+
use bear::modes::KEY_DESTINATION;
2525
use std::path::{Path, PathBuf};
2626

2727
/// Implementation of the wrapper process.

rust/bear/src/input.rs

Lines changed: 0 additions & 247 deletions
This file was deleted.

rust/bear/src/ipc/tcp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rand::random;
1414

1515
/// Implements convenient methods for the `Envelope` type.
1616
impl Envelope {
17-
pub fn new(rid: &ReporterId, event: Event) -> Self {
17+
fn new(rid: &ReporterId, event: Event) -> Self {
1818
let timestamp = Utc::now().timestamp_millis() as u64;
1919
Envelope {
2020
rid: rid.clone(),
@@ -27,7 +27,7 @@ impl Envelope {
2727
///
2828
/// The envelope is serialized using JSON and the length of the JSON
2929
/// is written as a 4 byte big-endian integer before the JSON.
30-
pub fn read_from(reader: &mut impl Read) -> Result<Self, anyhow::Error> {
30+
fn read_from(reader: &mut impl Read) -> Result<Self, anyhow::Error> {
3131
let mut length_bytes = [0; 4];
3232
reader.read_exact(&mut length_bytes)?;
3333
let length = u32::from_be_bytes(length_bytes) as usize;
@@ -43,7 +43,7 @@ impl Envelope {
4343
///
4444
/// The envelope is serialized using JSON and the length of the JSON
4545
/// is written as a 4 byte big-endian integer before the JSON.
46-
pub fn write_into(&self, writer: &mut impl Write) -> Result<u32, anyhow::Error> {
46+
fn write_into(&self, writer: &mut impl Write) -> Result<u32, anyhow::Error> {
4747
let serialized_envelope = serde_json::to_string(&self)?;
4848
let bytes = serialized_envelope.into_bytes();
4949
let length = bytes.len() as u32;

rust/bear/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
pub mod args;
44
pub mod config;
55
mod fixtures;
6-
pub mod input;
76
pub mod ipc;
87
pub mod modes;
98
pub mod output;

rust/bear/src/modes/input.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
use anyhow::Context;
4+
use serde_json::de::IoRead;
5+
use serde_json::{Error, StreamDeserializer};
6+
use std::fs::OpenOptions;
7+
use std::io::BufReader;
8+
use std::path::PathBuf;
9+
10+
use crate::args;
11+
use crate::ipc::{Envelope, Execution};
12+
13+
/// Responsible for reading the build events from the intercept mode.
14+
///
15+
/// The file syntax is defined by the `events` module, and the parsing logic is implemented there.
16+
/// Here we only handle the file opening and the error handling.
17+
pub struct EventFileReader {
18+
stream: Box<dyn Iterator<Item = Result<Envelope, Error>>>,
19+
}
20+
21+
impl TryFrom<args::BuildEvents> for EventFileReader {
22+
type Error = anyhow::Error;
23+
24+
/// Open the file and create a new instance of the event file reader.
25+
///
26+
/// If the file cannot be opened, the error will be logged and escalated.
27+
fn try_from(value: args::BuildEvents) -> Result<Self, Self::Error> {
28+
let file_name = PathBuf::from(value.file_name);
29+
let file = OpenOptions::new()
30+
.read(true)
31+
.open(file_name.as_path())
32+
.map(BufReader::new)
33+
.with_context(|| format!("Failed to open input file: {:?}", file_name))?;
34+
let stream = Box::new(StreamDeserializer::new(IoRead::new(file)));
35+
36+
Ok(EventFileReader { stream })
37+
}
38+
}
39+
40+
impl EventFileReader {
41+
/// Generate the build events from the file.
42+
///
43+
/// Returns an iterator over the build events. Any error during the reading
44+
/// of the file will be logged and the failed entries will be skipped.
45+
pub fn generate(self) -> impl Iterator<Item = Execution> {
46+
self.stream.filter_map(|result| match result {
47+
Ok(value) => Some(value.event.execution),
48+
Err(error) => {
49+
log::error!("Failed to read event: {:?}", error);
50+
None
51+
}
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)