Skip to content

Commit

Permalink
rust: prepare semantic package to be a module in bear
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Oct 6, 2024
1 parent 5475e2f commit 4452195
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 259 deletions.
1 change: 0 additions & 1 deletion rust/semantic/src/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[cfg(test)]
mod fixtures {
#[macro_export]
Expand Down
4 changes: 2 additions & 2 deletions rust/semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

pub mod tools;
mod fixtures;
pub mod tools;

use std::path::PathBuf;
use intercept::ipc::Execution;
use std::path::PathBuf;

/// Represents a semantic recognition result.
#[derive(Debug, PartialEq)]
Expand Down
46 changes: 23 additions & 23 deletions rust/semantic/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@

use std::path::PathBuf;

use super::tools::configured::Configured;
use super::tools::ignore::{IgnoreByArgs, IgnoreByPath};
use super::tools::unix::Unix;
use super::tools::wrapper::Wrapper;
use super::{Meaning, RecognitionResult, Tool};
use intercept::ipc::Execution;
use crate::{RecognitionResult, Meaning, Tool};
use crate::tools::configured::Configured;
use crate::tools::ignore::{IgnoreByArgs, IgnoreByPath};
use crate::tools::unix::Unix;
use crate::tools::wrapper::Wrapper;

mod configured;
mod wrapper;
mod matchers;
mod unix;
mod gcc;
mod ignore;

mod matchers;
mod unix;
mod wrapper;

pub struct Builder {
tools: Vec<Box<dyn Tool>>,
Expand All @@ -41,7 +40,9 @@ pub struct Builder {
// TODO: write unit test for this!!!
impl Builder {
pub fn new() -> Self {
Builder { tools: vec![Unix::new(), Wrapper::new()] }
Builder {
tools: vec![Unix::new(), Wrapper::new()],
}
}

pub fn build(self) -> impl Tool {
Expand Down Expand Up @@ -78,7 +79,6 @@ impl Builder {
}
}


struct Any {
tools: Vec<Box<dyn Tool>>,
}
Expand All @@ -94,8 +94,9 @@ impl Tool for Any {
fn recognize(&self, x: &Execution) -> RecognitionResult {
for tool in &self.tools {
match tool.recognize(x) {
RecognitionResult::Recognized(result) =>
return RecognitionResult::Recognized(result),
RecognitionResult::Recognized(result) => {
return RecognitionResult::Recognized(result)
}
_ => continue,
}
}
Expand All @@ -119,7 +120,7 @@ mod test {
Box::new(MockTool::NotRecognize),
Box::new(MockTool::NotRecognize),
Box::new(MockTool::NotRecognize),
]
],
};

let input = any_execution();
Expand All @@ -137,14 +138,14 @@ mod test {
Box::new(MockTool::NotRecognize),
Box::new(MockTool::Recognize),
Box::new(MockTool::NotRecognize),
]
],
};

let input = any_execution();

match sut.recognize(&input) {
RecognitionResult::Recognized(Ok(_)) => assert!(true),
_ => assert!(false)
_ => assert!(false),
}
}

Expand All @@ -156,7 +157,7 @@ mod test {
Box::new(MockTool::RecognizeFailed),
Box::new(MockTool::Recognize),
Box::new(MockTool::NotRecognize),
]
],
};

let input = any_execution();
Expand All @@ -176,12 +177,11 @@ mod test {
impl Tool for MockTool {
fn recognize(&self, _: &Execution) -> RecognitionResult {
match self {
MockTool::Recognize =>
RecognitionResult::Recognized(Ok(Meaning::Ignored)),
MockTool::RecognizeFailed =>
RecognitionResult::Recognized(Err(String::from("problem"))),
MockTool::NotRecognize =>
RecognitionResult::NotRecognized,
MockTool::Recognize => RecognitionResult::Recognized(Ok(Meaning::Ignored)),
MockTool::RecognizeFailed => {
RecognitionResult::Recognized(Err(String::from("problem")))
}
MockTool::NotRecognize => RecognitionResult::NotRecognized,
}
}
}
Expand Down
75 changes: 41 additions & 34 deletions rust/semantic/src/tools/configured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
use std::path::{Path, PathBuf};
use std::vec;

use super::super::{CompilerPass, Meaning, RecognitionResult, Tool};
use super::matchers::source::looks_like_a_source_file;
use intercept::ipc::Execution;
use crate::{RecognitionResult, CompilerPass, Meaning, Tool};
use crate::tools::matchers::source::looks_like_a_source_file;
use crate::tools::RecognitionResult::{NotRecognized, Recognized};

pub struct Configured {
pub executable: PathBuf,
}

impl Configured {
pub fn new(compiler: &Path) -> Box<dyn Tool> {
Box::new(Self { executable: compiler.to_path_buf() })
Box::new(Self {
executable: compiler.to_path_buf(),
})
}
}

Expand All @@ -52,28 +53,23 @@ impl Tool for Configured {
}

if sources.is_empty() {
Recognized(Err(String::from("source file is not found")))
RecognitionResult::Recognized(Err(String::from("source file is not found")))
} else {
Recognized(
Ok(
Meaning::Compiler {
compiler: x.executable.clone(),
working_dir: x.working_dir.clone(),
passes: sources.iter()
.map(|source| {
CompilerPass::Compile {
source: source.clone(),
output: None,
flags: flags.clone(),
}
})
.collect(),
}
)
)
RecognitionResult::Recognized(Ok(Meaning::Compiler {
compiler: x.executable.clone(),
working_dir: x.working_dir.clone(),
passes: sources
.iter()
.map(|source| CompilerPass::Compile {
source: source.clone(),
output: None,
flags: flags.clone(),
})
.collect(),
}))
}
} else {
NotRecognized
RecognitionResult::NotRecognized
}
}
}
Expand All @@ -92,24 +88,32 @@ mod test {
fn test_matching() {
let input = Execution {
executable: PathBuf::from("/usr/bin/something"),
arguments: vec_of_strings!["something", "-Dthis=that", "-I.", "source.c", "-o", "source.c.o"],
arguments: vec_of_strings![
"something",
"-Dthis=that",
"-I.",
"source.c",
"-o",
"source.c.o"
],
working_dir: PathBuf::from("/home/user"),
environment: HashMap::new(),
};

let expected = Meaning::Compiler {
compiler: PathBuf::from("/usr/bin/something"),
working_dir: PathBuf::from("/home/user"),
passes: vec![
CompilerPass::Compile {
flags: vec_of_strings!["-Dthis=that", "-I.", "-o", "source.c.o"],
source: PathBuf::from("source.c"),
output: None,
}
],
passes: vec![CompilerPass::Compile {
flags: vec_of_strings!["-Dthis=that", "-I.", "-o", "source.c.o"],
source: PathBuf::from("source.c"),
output: None,
}],
};

assert_eq!(Recognized(Ok(expected)), SUT.recognize(&input));
assert_eq!(
RecognitionResult::Recognized(Ok(expected)),
SUT.recognize(&input)
);
}

#[test]
Expand All @@ -121,7 +125,10 @@ mod test {
environment: HashMap::new(),
};

assert_eq!(Recognized(Err(String::from("source file is not found"))), SUT.recognize(&input));
assert_eq!(
RecognitionResult::Recognized(Err(String::from("source file is not found"))),
SUT.recognize(&input)
);
}

#[test]
Expand All @@ -133,7 +140,7 @@ mod test {
environment: HashMap::new(),
};

assert_eq!(NotRecognized, SUT.recognize(&input));
assert_eq!(RecognitionResult::NotRecognized, SUT.recognize(&input));
}

lazy_static! {
Expand Down
Loading

0 comments on commit 4452195

Please sign in to comment.