-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmay_depend_on.rs
More file actions
138 lines (120 loc) · 4.23 KB
/
may_depend_on.rs
File metadata and controls
138 lines (120 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::dependency_parsing::get_dependencies_in_ast;
use crate::rules::rule::{Rule, RustFile};
use crate::rules::utils::IsChild;
use ansi_term::Color::RGB;
use ansi_term::Style;
use log::debug;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub struct MayDependOnRule {
pub subject: String,
pub allowed_dependencies: Vec<String>,
}
impl Display for MayDependOnRule {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut allowed_dependencies: Vec<String> = Vec::new();
allowed_dependencies.extend(self.allowed_dependencies.clone());
let bold = Style::new().bold().fg(RGB(255, 165, 0));
if allowed_dependencies.is_empty() {
write!(
f,
"{} may not depend on any modules",
bold.paint(&self.subject)
)
} else {
write!(
f,
"{} may depend on {}",
bold.paint(&self.subject),
bold.paint("[".to_string() + &allowed_dependencies.join(", ") + "]")
)
}
}
}
impl Rule for MayDependOnRule {
fn apply(&self, file: &RustFile) -> Result<(), String> {
let dependencies = get_dependencies_in_ast(&file.ast, &file.logical_path);
let forbidden_dependencies: Vec<String> = dependencies
.iter()
.filter(|&dependency| {
let is_child_of_subject = dependency.is_child_of(&self.subject);
if !is_child_of_subject {
let is_allowed = self
.allowed_dependencies
.iter()
.any(|ad| dependency.is_child_of(ad));
if !is_allowed {
return true;
}
}
false
})
.cloned()
.collect();
if !forbidden_dependencies.is_empty() {
let red = Style::new().fg(RGB(255, 0, 0)).bold();
return Err(format!(
"Forbidden dependencies to {} in file://{}",
red.paint("[".to_string() + &forbidden_dependencies.join(", ") + "]"),
file.path
));
}
Ok(())
}
fn is_applicable(&self, file: &RustFile) -> bool {
let orange = Style::new().bold().fg(ansi_term::Color::RGB(255, 165, 0));
let green = Style::new().bold().fg(ansi_term::Color::RGB(0, 255, 0));
debug!(
"File {} mapped to module {}",
green.paint(&file.path),
orange.paint(&file.logical_path)
);
file.logical_path.is_child_of(&self.subject)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dependency_rule() {
let rule = MayDependOnRule {
subject: "policy_management::domain".to_string(),
allowed_dependencies: vec!["conversion::domain::domain_function_1".to_string()],
};
let result = rule.apply(&RustFile::from(
"./../rust_arkitect/examples/sample_project/src/conversion/application.rs",
));
assert!(result.is_err());
}
#[test]
fn test_display_may_depend_on_with_dependencies() {
use ansi_term::Color::RGB;
use ansi_term::Style;
let rule = MayDependOnRule {
subject: "module_3".to_string(),
allowed_dependencies: vec!["dependency_a".to_string(), "dependency_b".to_string()],
};
let bold_orange = Style::new().bold().fg(RGB(255, 165, 0));
let expected = format!(
"{} may depend on {}",
bold_orange.paint("module_3"),
bold_orange.paint("[dependency_a, dependency_b]")
);
assert_eq!(format!("{}", rule), expected);
}
#[test]
fn test_display_may_depend_on_no_dependencies() {
use ansi_term::Color::RGB;
use ansi_term::Style;
let rule = MayDependOnRule {
subject: "module_4".to_string(),
allowed_dependencies: vec![],
};
let bold_orange = Style::new().bold().fg(RGB(255, 165, 0));
let expected = format!(
"{} may not depend on any modules",
bold_orange.paint("module_4")
);
assert_eq!(format!("{}", rule), expected);
}
}