Skip to content

Commit da35f0b

Browse files
committed
rust: settle on function name for creating objects
1 parent 76100b4 commit da35f0b

File tree

7 files changed

+37
-31
lines changed

7 files changed

+37
-31
lines changed

rust/bear/src/bin/bear.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ impl Application {
4949
match args.mode {
5050
args::Mode::Intercept { input, output } => {
5151
log::debug!("Mode: intercept");
52-
Intercept::from(input, output, config).map(Application::Intercept)
52+
Intercept::create(input, output, config).map(Application::Intercept)
5353
}
5454
args::Mode::Semantic { input, output } => {
5555
log::debug!("Mode: semantic analysis");
56-
Semantic::from(input, output, config).map(Application::Semantic)
56+
Semantic::create(input, output, config).map(Application::Semantic)
5757
}
5858
args::Mode::Combined { input, output } => {
5959
log::debug!("Mode: intercept and semantic analysis");
60-
Combined::from(input, output, config).map(Application::Combined)
60+
Combined::create(input, output, config).map(Application::Combined)
6161
}
6262
}
6363
}

rust/bear/src/intercept/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ impl CollectorService {
123123
///
124124
/// The `consumer` is a function that receives the events and processes them.
125125
/// The function is executed in a separate thread.
126-
pub fn new<F>(consumer: F) -> anyhow::Result<Self>
126+
pub fn create<F>(consumer: F) -> anyhow::Result<Self>
127127
where
128128
F: FnOnce(Receiver<Event>) -> anyhow::Result<()>,
129129
F: Send + 'static,
130130
{
131-
let collector = tcp::CollectorOnTcp::new()?;
131+
let collector = tcp::CollectorOnTcp::create()?;
132132
let collector_arc = Arc::new(collector);
133133
let (sender, receiver) = channel();
134134

@@ -202,7 +202,10 @@ impl InterceptEnvironment {
202202
/// The `config` is the intercept configuration that specifies the mode and the
203203
/// required parameters for the mode. The `collector` is the service to collect
204204
/// the execution events.
205-
pub fn new(config: &config::Intercept, collector: &CollectorService) -> anyhow::Result<Self> {
205+
pub fn create(
206+
config: &config::Intercept,
207+
collector: &CollectorService,
208+
) -> anyhow::Result<Self> {
206209
// Validate the configuration.
207210
let valid_config = config.validate()?;
208211

rust/bear/src/intercept/tcp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl CollectorOnTcp {
5656
///
5757
/// The collector listens to a random port on the loopback interface.
5858
/// The address of the collector can be obtained by the `address` method.
59-
pub fn new() -> Result<Self, anyhow::Error> {
59+
pub fn create() -> Result<Self, anyhow::Error> {
6060
let shutdown = Arc::new(AtomicBool::new(false));
6161
let listener = TcpListener::bind("127.0.0.1:0")?;
6262
let address = listener.local_addr()?;
@@ -191,7 +191,7 @@ mod tests {
191191
// if they are the same as the original events.
192192
#[test]
193193
fn tcp_reporter_and_collectors_work() {
194-
let collector = CollectorOnTcp::new().unwrap();
194+
let collector = CollectorOnTcp::create().unwrap();
195195
let reporter = ReporterOnTcp::new(collector.address()).unwrap();
196196

197197
// Create wrapper to share the collector across threads.

rust/bear/src/modes/intercept.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ pub(super) struct BuildInterceptor {
1717

1818
impl BuildInterceptor {
1919
/// Create a new process execution interceptor.
20-
pub(super) fn new<F>(config: config::Main, consumer: F) -> anyhow::Result<Self>
20+
pub(super) fn create<F>(config: config::Main, consumer: F) -> anyhow::Result<Self>
2121
where
2222
F: FnOnce(Receiver<Event>) -> anyhow::Result<()>,
2323
F: Send + 'static,
2424
{
25-
let service = CollectorService::new(consumer)
25+
let service = CollectorService::create(consumer)
2626
.with_context(|| "Failed to create the intercept service")?;
2727

28-
let environment = InterceptEnvironment::new(&config.intercept, &service)
28+
let environment = InterceptEnvironment::create(&config.intercept, &service)
2929
.with_context(|| "Failed to create the intercept environment")?;
3030

3131
Ok(Self {

rust/bear/src/modes/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pub mod intercept;
44
pub mod semantic;
55

6-
use crate::intercept::persistence::{read, write};
6+
use crate::intercept::persistence;
77
use crate::modes::intercept::BuildInterceptor;
88
use crate::modes::semantic::SemanticAnalysisPipeline;
99
use crate::{args, config};
@@ -27,7 +27,7 @@ pub struct Intercept {
2727

2828
impl Intercept {
2929
/// Create a new intercept mode instance.
30-
pub fn from(
30+
pub fn create(
3131
command: args::BuildCommand,
3232
output: args::BuildEvents,
3333
config: config::Main,
@@ -41,8 +41,9 @@ impl Intercept {
4141
.map(io::BufWriter::new)
4242
.with_context(|| format!("Failed to open file: {:?}", file_name))?;
4343

44-
let interceptor =
45-
BuildInterceptor::new(config, move |envelopes| write(output_file, envelopes))?;
44+
let interceptor = BuildInterceptor::create(config, move |events| {
45+
persistence::write(output_file, events)
46+
})?;
4647

4748
Ok(Self {
4849
command,
@@ -70,7 +71,7 @@ pub struct Semantic {
7071
}
7172

7273
impl Semantic {
73-
pub fn from(
74+
pub fn create(
7475
input: args::BuildEvents,
7576
output: args::BuildSemantic,
7677
config: config::Main,
@@ -82,7 +83,7 @@ impl Semantic {
8283
.map(BufReader::new)
8384
.with_context(|| format!("Failed to open file: {:?}", file_name))?;
8485

85-
let semantic = SemanticAnalysisPipeline::from(output, &config)?;
86+
let semantic = SemanticAnalysisPipeline::create(output, &config)?;
8687

8788
Ok(Self {
8889
event_file,
@@ -97,7 +98,7 @@ impl Mode for Semantic {
9798
/// The exit code is based on the result of the output writer.
9899
fn run(self) -> anyhow::Result<ExitCode> {
99100
self.semantic
100-
.analyze_and_write(read(self.event_file))
101+
.analyze_and_write(persistence::read(self.event_file))
101102
.map(|_| ExitCode::SUCCESS)
102103
}
103104
}
@@ -110,15 +111,14 @@ pub struct Combined {
110111

111112
impl Combined {
112113
/// Create a new all mode instance.
113-
pub fn from(
114+
pub fn create(
114115
command: args::BuildCommand,
115116
output: args::BuildSemantic,
116117
config: config::Main,
117118
) -> anyhow::Result<Self> {
118-
let semantic = SemanticAnalysisPipeline::from(output, &config)?;
119-
let interceptor = BuildInterceptor::new(config, move |envelopes| {
120-
semantic.analyze_and_write(envelopes)
121-
})?;
119+
let semantic = SemanticAnalysisPipeline::create(output, &config)?;
120+
let interceptor =
121+
BuildInterceptor::create(config, move |events| semantic.analyze_and_write(events))?;
122122

123123
Ok(Self {
124124
command,

rust/bear/src/modes/semantic.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::intercept::Event;
44
use crate::output::OutputWriter;
5-
use crate::semantic::interpreters::create_interpreter;
5+
use crate::semantic::interpreters;
66
use crate::semantic::transformation;
77
use crate::semantic::transformation::FilterAndFormat;
88
use crate::{args, config, output, semantic};
@@ -20,8 +20,11 @@ pub(super) struct SemanticAnalysisPipeline {
2020

2121
impl SemanticAnalysisPipeline {
2222
/// Create a new semantic mode instance.
23-
pub(super) fn from(output: args::BuildSemantic, config: &config::Main) -> anyhow::Result<Self> {
24-
let interpreter = create_interpreter(config);
23+
pub(super) fn create(
24+
output: args::BuildSemantic,
25+
config: &config::Main,
26+
) -> anyhow::Result<Self> {
27+
let interpreter = interpreters::create(config);
2528
let transformation = FilterAndFormat::try_from(&config.output)?;
2629
let output_writer = OutputWriterImpl::create(&output, &config.output)?;
2730

@@ -136,7 +139,7 @@ impl OutputWriter for ClangOutputWriter {
136139
let entries = semantics.flat_map(|semantic| self.formatter.apply(semantic));
137140
if self.append && self.output.exists() {
138141
let entries_from_db = Self::read_from_compilation_db(self.output.as_path())?;
139-
let final_entries = entries.chain(entries_from_db);
142+
let final_entries = entries_from_db.chain(entries);
140143
self.write_into_compilation_db(final_entries)
141144
} else {
142145
if self.append {

rust/bear/src/semantic/interpreters/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod matchers;
2121
// Use the CC or CXX environment variables and make sure those are not excluded.
2222
// Make sure the environment variables are passed to the method.
2323
// TODO: Take environment variables as input.
24-
pub fn create_interpreter<'a>(config: &config::Main) -> impl Interpreter + 'a {
24+
pub fn create<'a>(config: &config::Main) -> impl Interpreter + 'a {
2525
let compilers_to_include = match &config.intercept {
2626
config::Intercept::Wrapper { executables, .. } => executables.clone(),
2727
_ => vec![],
@@ -79,7 +79,7 @@ mod test {
7979
fn test_create_interpreter_with_default_config() {
8080
let config = config::Main::default();
8181

82-
let interpreter = create_interpreter(&config);
82+
let interpreter = create(&config);
8383
let input = any_execution();
8484

8585
match interpreter.recognize(&input) {
@@ -99,7 +99,7 @@ mod test {
9999
..Default::default()
100100
};
101101

102-
let interpreter = create_interpreter(&config);
102+
let interpreter = create(&config);
103103
let input = any_execution();
104104

105105
match interpreter.recognize(&input) {
@@ -124,7 +124,7 @@ mod test {
124124
..Default::default()
125125
};
126126

127-
let interpreter = create_interpreter(&config);
127+
let interpreter = create(&config);
128128
let input = any_execution();
129129

130130
let result = interpreter.recognize(&input);

0 commit comments

Comments
 (0)