Skip to content

Commit 52ec7f8

Browse files
committed
Refactor dependency parsing
Signed-off-by: Patrick Luca Fazzi <patrick91@live.it>
1 parent d1ecdac commit 52ec7f8

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

src/dependency_parsing.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
use crate::rust_file::RustFile;
22
use std::collections::HashSet;
3-
use std::fs;
43
use std::ops::Deref;
54
use syn::{File, Item, ItemUse, UseTree};
65

7-
pub fn get_dependencies_in_file(file: &RustFile) -> Vec<String> {
8-
let content = match fs::read_to_string(&file.path) {
9-
Ok(content) => content,
10-
Err(e) => panic!("Failed to read file file://{}: {}", file.path, e),
11-
};
12-
13-
let ast = match syn::parse_file(&content) {
14-
Ok(ast) => ast,
15-
Err(e) => panic!("Failed to parse file file://{}: {}", file.path, e),
16-
};
17-
18-
get_dependencies_in_ast(&ast, &file.logical_path)
19-
}
20-
216
fn parse_module_item(item: &Item, dependencies: &mut Vec<String>, current_module: &str) {
227
match item {
238
Item::Use(ItemUse { tree, .. }) => {
@@ -34,17 +19,11 @@ fn parse_module_item(item: &Item, dependencies: &mut Vec<String>, current_module
3419
}
3520
}
3621

37-
#[allow(dead_code)]
38-
fn get_dependencies_in_str(s: &str, module: &str) -> Vec<String> {
39-
let ast: File = match syn::parse_str(s) {
40-
Ok(ast) => ast,
41-
Err(e) => panic!("Failed to parse string '{}': {}", s, e),
42-
};
43-
44-
get_dependencies_in_ast(&ast, module)
22+
pub fn get_dependencies_in_file(file: &RustFile) -> Vec<String> {
23+
get_dependencies_in_ast(&file.ast, &file.logical_path)
4524
}
4625

47-
pub fn get_dependencies_in_ast(ast: &File, current_module_logical_path: &str) -> Vec<String> {
26+
fn get_dependencies_in_ast(ast: &File, current_module_logical_path: &str) -> Vec<String> {
4827
let mut dependencies = Vec::new();
4928

5029
for item in ast.items.iter() {
@@ -69,12 +48,9 @@ pub fn get_dependencies_in_ast(ast: &File, current_module_logical_path: &str) ->
6948
}
7049
}
7150

72-
unique_values(dependencies)
73-
}
74-
75-
fn unique_values<T: std::hash::Hash + Eq + Clone>(vec: Vec<T>) -> Vec<T> {
7651
let mut unique_set = HashSet::new();
77-
vec.into_iter()
52+
dependencies
53+
.into_iter()
7854
.filter(|item| unique_set.insert(item.clone()))
7955
.collect()
8056
}
@@ -144,6 +120,15 @@ fn collect_dependencies_from_tree(
144120
mod tests {
145121
use super::*;
146122

123+
fn get_dependencies_in_str(s: &str, current_module_logical_path: &str) -> Vec<String> {
124+
let ast: File = match syn::parse_str(s) {
125+
Ok(ast) => ast,
126+
Err(e) => panic!("Failed to parse string '{}': {}", s, e),
127+
};
128+
129+
get_dependencies_in_ast(&ast, current_module_logical_path)
130+
}
131+
147132
#[test]
148133
pub fn test_parsing() {
149134
let dependencies = get_dependencies_in_file(&RustFile::from(

src/rules/may_depend_on.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::dependency_parsing::get_dependencies_in_ast;
1+
use crate::dependency_parsing::get_dependencies_in_file;
22
use crate::rule::Rule;
33
use crate::rules::utils::IsChild;
44
use crate::rust_file::RustFile;
@@ -37,7 +37,7 @@ impl Display for MayDependOnRule {
3737

3838
impl Rule for MayDependOnRule {
3939
fn apply(&self, file: &RustFile) -> Result<(), String> {
40-
let dependencies = get_dependencies_in_ast(&file.ast, &file.logical_path);
40+
let dependencies = get_dependencies_in_file(&file);
4141

4242
let forbidden_dependencies: Vec<String> = dependencies
4343
.iter()

src/rules/must_not_depend_on.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Display for MustNotDependOnRule {
4646

4747
impl Rule for MustNotDependOnRule {
4848
fn apply(&self, file: &RustFile) -> Result<(), String> {
49-
let dependencies = get_dependencies_in_file(file);
49+
let dependencies = get_dependencies_in_file(&file);
5050

5151
let forbidden_dependencies: Vec<String> = dependencies
5252
.iter()

src/rules/must_not_depend_on_anything.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Display for MustNotDependOnAnythingRule {
3636

3737
impl Rule for MustNotDependOnAnythingRule {
3838
fn apply(&self, file: &RustFile) -> Result<(), String> {
39-
let dependencies = get_dependencies_in_file(file);
39+
let dependencies = get_dependencies_in_file(&file);
4040

4141
let forbidden_dependencies: Vec<String> = dependencies
4242
.iter()

0 commit comments

Comments
 (0)