Skip to content

Commit fe038c1

Browse files
committed
rust: semantic result renamed
1 parent 55a9c76 commit fe038c1

File tree

7 files changed

+49
-55
lines changed

7 files changed

+49
-55
lines changed

rust/bear/src/recognition.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,27 @@ impl TryFrom<&config::Main> for Recognition {
4141
impl Recognition {
4242
pub fn apply(&self, execution: Execution) -> Option<semantic::Meaning> {
4343
match self.tool.recognize(&execution) {
44-
semantic::RecognitionResult::Recognized(Ok(semantic::Meaning::Ignored)) => {
44+
semantic::Recognition::Success(semantic::Meaning::Ignored) => {
4545
log::debug!("execution recognized, but ignored: {:?}", execution);
4646
None
4747
}
48-
semantic::RecognitionResult::Recognized(Ok(semantic)) => {
48+
semantic::Recognition::Success(semantic) => {
4949
log::debug!(
5050
"execution recognized as compiler call, {:?} : {:?}",
5151
semantic,
5252
execution
5353
);
5454
Some(semantic)
5555
}
56-
semantic::RecognitionResult::Recognized(Err(reason)) => {
56+
semantic::Recognition::Error(reason) => {
5757
log::debug!(
5858
"execution recognized with failure, {:?} : {:?}",
5959
reason,
6060
execution
6161
);
6262
None
6363
}
64-
semantic::RecognitionResult::NotRecognized => {
64+
semantic::Recognition::Unknown => {
6565
log::debug!("execution not recognized: {:?}", execution);
6666
None
6767
}

rust/bear/src/semantic/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ pub mod tools;
55
use intercept::Execution;
66
use std::path::PathBuf;
77

8-
/// Represents a semantic recognition result.
9-
#[derive(Debug, PartialEq)]
10-
pub enum RecognitionResult {
11-
Recognized(Result<Meaning, String>),
12-
NotRecognized,
13-
}
14-
158
/// Represents an executed command semantic.
169
#[derive(Debug, PartialEq)]
1710
pub enum Meaning {
@@ -25,7 +18,7 @@ pub enum Meaning {
2518
Ignored,
2619
}
2720

28-
/// Represents a compiler call.
21+
/// Represents a compiler call pass.
2922
#[derive(Debug, PartialEq)]
3023
pub enum CompilerPass {
3124
Preprocess,
@@ -43,5 +36,13 @@ pub enum CompilerPass {
4336
/// represent a set of tools, and the recognition process can be distributed
4437
/// amongst the tools.
4538
pub trait Tool: Send {
46-
fn recognize(&self, _: &Execution) -> RecognitionResult;
39+
fn recognize(&self, _: &Execution) -> Recognition<Meaning>;
40+
}
41+
42+
/// Represents a semantic recognition result.
43+
#[derive(Debug, PartialEq)]
44+
pub enum Recognition<T> {
45+
Success(T),
46+
Error(String),
47+
Unknown,
4748
}

rust/bear/src/semantic/tools/combinators.rs

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

3-
use super::super::{RecognitionResult, Tool};
3+
use super::super::{Meaning, Recognition, Tool};
44
use intercept::Execution;
55

66
/// Represents a set of tools, where any of them can recognize the semantic.
@@ -17,16 +17,14 @@ impl Any {
1717
}
1818

1919
impl Tool for Any {
20-
fn recognize(&self, x: &Execution) -> RecognitionResult {
20+
fn recognize(&self, x: &Execution) -> Recognition<Meaning> {
2121
for tool in &self.tools {
2222
match tool.recognize(x) {
23-
RecognitionResult::Recognized(result) => {
24-
return RecognitionResult::Recognized(result)
25-
}
26-
_ => continue,
23+
Recognition::Unknown => continue,
24+
result => return result,
2725
}
2826
}
29-
RecognitionResult::NotRecognized
27+
Recognition::Unknown
3028
}
3129
}
3230

@@ -51,7 +49,7 @@ mod test {
5149
let input = any_execution();
5250

5351
match sut.recognize(&input) {
54-
RecognitionResult::NotRecognized => assert!(true),
52+
Recognition::Unknown => assert!(true),
5553
_ => assert!(false),
5654
}
5755
}
@@ -69,7 +67,7 @@ mod test {
6967
let input = any_execution();
7068

7169
match sut.recognize(&input) {
72-
RecognitionResult::Recognized(Ok(_)) => assert!(true),
70+
Recognition::Success(_) => assert!(true),
7371
_ => assert!(false),
7472
}
7573
}
@@ -88,7 +86,7 @@ mod test {
8886
let input = any_execution();
8987

9088
match sut.recognize(&input) {
91-
RecognitionResult::Recognized(Err(_)) => assert!(true),
89+
Recognition::Error(_) => assert!(true),
9290
_ => assert!(false),
9391
}
9492
}
@@ -100,13 +98,11 @@ mod test {
10098
}
10199

102100
impl Tool for MockTool {
103-
fn recognize(&self, _: &Execution) -> RecognitionResult {
101+
fn recognize(&self, _: &Execution) -> Recognition<Meaning> {
104102
match self {
105-
MockTool::Recognize => RecognitionResult::Recognized(Ok(Meaning::Ignored)),
106-
MockTool::RecognizeFailed => {
107-
RecognitionResult::Recognized(Err(String::from("problem")))
108-
}
109-
MockTool::NotRecognize => RecognitionResult::NotRecognized,
103+
MockTool::Recognize => Recognition::Success(Meaning::Ignored),
104+
MockTool::RecognizeFailed => Recognition::Error(String::from("problem")),
105+
MockTool::NotRecognize => Recognition::Unknown,
110106
}
111107
}
112108
}

rust/bear/src/semantic/tools/gcc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use nom::branch::alt;
44
use nom::multi::many1;
55
use nom::sequence::preceded;
66

7-
use super::super::{Meaning, RecognitionResult, Tool};
7+
use super::super::{Meaning, Recognition, Tool};
88
use intercept::Execution;
99
use internal::Argument;
1010

@@ -17,7 +17,7 @@ impl Gcc {
1717
}
1818

1919
impl Tool for Gcc {
20-
fn recognize(&self, execution: &Execution) -> RecognitionResult {
20+
fn recognize(&self, execution: &Execution) -> Recognition<Meaning> {
2121
let mut parser = preceded(
2222
internal::compiler,
2323
many1(alt((internal::flag, internal::source))),
@@ -29,15 +29,15 @@ impl Tool for Gcc {
2929
let flags = result.1;
3030
let passes = Argument::passes(flags.as_slice());
3131

32-
RecognitionResult::Recognized(Ok(Meaning::Compiler {
32+
Recognition::Success(Meaning::Compiler {
3333
compiler: execution.executable.clone(),
3434
working_dir: execution.working_dir.clone(),
3535
passes,
36-
}))
36+
})
3737
}
3838
Err(error) => {
3939
log::debug!("Gcc failed to parse it: {error}.");
40-
RecognitionResult::NotRecognized
40+
Recognition::Unknown
4141
}
4242
}
4343
}

rust/bear/src/semantic/tools/generic.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashSet;
44
use std::path::PathBuf;
55
use std::vec;
66

7-
use super::super::{CompilerPass, Meaning, RecognitionResult, Tool};
7+
use super::super::{CompilerPass, Meaning, Recognition, Tool};
88
use super::matchers::source::looks_like_a_source_file;
99
use intercept::Execution;
1010

@@ -25,7 +25,7 @@ impl Tool for Generic {
2525
/// - the executable name,
2626
/// - one of the arguments is a source file,
2727
/// - the rest of the arguments are flags.
28-
fn recognize(&self, x: &Execution) -> RecognitionResult {
28+
fn recognize(&self, x: &Execution) -> Recognition<Meaning> {
2929
if self.executables.contains(&x.executable) {
3030
let mut flags = vec![];
3131
let mut sources = vec![];
@@ -40,9 +40,9 @@ impl Tool for Generic {
4040
}
4141

4242
if sources.is_empty() {
43-
RecognitionResult::Recognized(Err(String::from("source file is not found")))
43+
Recognition::Error(String::from("source file is not found"))
4444
} else {
45-
RecognitionResult::Recognized(Ok(Meaning::Compiler {
45+
Recognition::Success(Meaning::Compiler {
4646
compiler: x.executable.clone(),
4747
working_dir: x.working_dir.clone(),
4848
passes: sources
@@ -53,10 +53,10 @@ impl Tool for Generic {
5353
flags: flags.clone(),
5454
})
5555
.collect(),
56-
}))
56+
})
5757
}
5858
} else {
59-
RecognitionResult::NotRecognized
59+
Recognition::Unknown
6060
}
6161
}
6262
}
@@ -97,10 +97,7 @@ mod test {
9797
}],
9898
};
9999

100-
assert_eq!(
101-
RecognitionResult::Recognized(Ok(expected)),
102-
SUT.recognize(&input)
103-
);
100+
assert_eq!(Recognition::Success(expected), SUT.recognize(&input));
104101
}
105102

106103
#[test]
@@ -113,7 +110,7 @@ mod test {
113110
};
114111

115112
assert_eq!(
116-
RecognitionResult::Recognized(Err(String::from("source file is not found"))),
113+
Recognition::Error(String::from("source file is not found")),
117114
SUT.recognize(&input)
118115
);
119116
}
@@ -127,7 +124,7 @@ mod test {
127124
environment: HashMap::new(),
128125
};
129126

130-
assert_eq!(RecognitionResult::NotRecognized, SUT.recognize(&input));
127+
assert_eq!(Recognition::Unknown, SUT.recognize(&input));
131128
}
132129

133130
lazy_static! {

rust/bear/src/semantic/tools/ignore.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::collections::HashSet;
44
use std::path::PathBuf;
55

6-
use super::super::{Meaning, RecognitionResult, Tool};
6+
use super::super::{Meaning, Recognition, Tool};
77
use intercept::Execution;
88

99
/// A tool to ignore a command execution by executable name.
@@ -25,11 +25,11 @@ impl IgnoreByPath {
2525

2626
/// A tool to ignore a command execution by arguments.
2727
impl Tool for IgnoreByPath {
28-
fn recognize(&self, execution: &Execution) -> RecognitionResult {
28+
fn recognize(&self, execution: &Execution) -> Recognition<Meaning> {
2929
if self.executables.contains(&execution.executable) {
30-
RecognitionResult::Recognized(Ok(Meaning::Ignored))
30+
Recognition::Success(Meaning::Ignored)
3131
} else {
32-
RecognitionResult::NotRecognized
32+
Recognition::Unknown
3333
}
3434
}
3535
}
@@ -163,7 +163,7 @@ mod test {
163163
let sut = IgnoreByPath::new();
164164

165165
assert_eq!(
166-
RecognitionResult::Recognized(Ok(Meaning::Ignored)),
166+
Recognition::Success(Meaning::Ignored),
167167
sut.recognize(&input)
168168
)
169169
}
@@ -178,6 +178,6 @@ mod test {
178178
};
179179
let sut = IgnoreByPath::new();
180180

181-
assert_eq!(RecognitionResult::NotRecognized, sut.recognize(&input))
181+
assert_eq!(Recognition::Unknown, sut.recognize(&input))
182182
}
183183
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mod test {
6464
use std::collections::HashMap;
6565
use std::path::PathBuf;
6666

67-
use super::super::{Meaning, RecognitionResult};
67+
use super::super::{Meaning, Recognition};
6868
use super::*;
6969
use crate::{vec_of_pathbuf, vec_of_strings};
7070
use intercept::Execution;
@@ -75,7 +75,7 @@ mod test {
7575

7676
let input = any_execution();
7777
match sut.recognize(&input) {
78-
RecognitionResult::Recognized(Ok(Meaning::Compiler { .. })) => assert!(true),
78+
Recognition::Success(Meaning::Compiler { .. }) => assert!(true),
7979
_ => assert!(false),
8080
}
8181
}
@@ -87,7 +87,7 @@ mod test {
8787

8888
let input = any_execution();
8989
match sut.recognize(&input) {
90-
RecognitionResult::Recognized(Ok(Meaning::Ignored)) => assert!(true),
90+
Recognition::Success(Meaning::Ignored) => assert!(true),
9191
_ => assert!(false),
9292
}
9393
}

0 commit comments

Comments
 (0)