Skip to content

Commit 9919f06

Browse files
committed
feat(libredirectionio): avoid code duplication
1 parent cc2d002 commit 9919f06

File tree

3 files changed

+61
-184
lines changed

3 files changed

+61
-184
lines changed

src/api/impact.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,15 @@ impl ImpactOutput {
101101

102102
impact_router.remove(impact_input.rule.id.as_str());
103103

104-
if impact_input.action == "add" || impact_input.action == "update" {
105-
impact_router.insert(impact_input.rule.clone());
106-
trace_unique_router.insert(impact_input.rule.clone());
107-
}
108-
109-
let impacts = ImpactOutput::compute_impacts(
110-
&impact_router,
111-
&trace_unique_router,
112-
impact_input.rule.examples,
104+
ImpactOutput::compute_impacts(
105+
&mut impact_router,
106+
&mut trace_unique_router,
107+
impact_input.rule.examples.clone(),
113108
impact_input.with_redirection_loop,
114109
impact_input.max_hops,
115-
);
116-
117-
ImpactOutput { impacts }
110+
impact_input.action.as_str(),
111+
impact_input.rule,
112+
)
118113
}
119114

120115
pub fn create_result(impact_input: ImpactInput) -> ImpactOutput {
@@ -132,33 +127,35 @@ impl ImpactOutput {
132127
router.insert(rule.clone());
133128
}
134129

135-
if impact_input.action == "add" || impact_input.action == "update" {
136-
router.insert(impact_input.rule.clone());
137-
trace_unique_router.insert(impact_input.rule.clone());
138-
}
139-
140-
let impacts = ImpactOutput::compute_impacts(
141-
&router,
142-
&trace_unique_router,
143-
impact_input.rule.examples,
130+
ImpactOutput::compute_impacts(
131+
&mut router,
132+
&mut trace_unique_router,
133+
impact_input.rule.examples.clone(),
144134
impact_input.with_redirection_loop,
145135
impact_input.max_hops,
146-
);
147-
148-
ImpactOutput { impacts }
136+
impact_input.action.as_str(),
137+
impact_input.rule,
138+
)
149139
}
150140

151141
fn compute_impacts(
152-
router: &Router<Rule>,
153-
trace_unique_router: &Router<Rule>,
142+
router: &mut Router<Rule>,
143+
trace_unique_router: &mut Router<Rule>,
154144
examples: Option<Vec<Example>>,
155145
with_redirection_loop: bool,
156146
max_hops: u8,
157-
) -> Vec<Impact> {
147+
action: &str,
148+
rule: Rule,
149+
) -> ImpactOutput {
150+
if action == "add" || action == "update" {
151+
router.insert(rule.clone());
152+
trace_unique_router.insert(rule);
153+
}
154+
158155
let mut impacts = Vec::new();
159156

160157
if examples.is_none() {
161-
return impacts;
158+
return ImpactOutput { impacts };
162159
}
163160

164161
for example in examples.unwrap() {
@@ -226,7 +223,8 @@ impl ImpactOutput {
226223
should_log_request,
227224
});
228225
}
229-
impacts
226+
227+
ImpactOutput { impacts }
230228
}
231229

232230
fn compute_redirection_loop(router: &Router<Rule>, max_hops: u8, example: &Example) -> RedirectionLoop {

src/api/test_examples.rs

Lines changed: 15 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -65,110 +65,39 @@ pub struct ErroredExample {
6565
impl TestExamplesOutput {
6666
pub fn from_project(test_examples_input: TestExamplesProjectInput, existing_router: Arc<Router<Rule>>) -> TestExamplesOutput {
6767
let test_example_router = test_examples_input.change_set.update_existing_router(existing_router);
68-
let mut results = TestExamplesOutput::default();
69-
70-
for (id, route) in test_example_router.routes() {
71-
let examples = &route.handler().examples;
72-
73-
if examples.is_none() {
74-
continue;
75-
}
76-
77-
for example in examples.as_ref().unwrap().iter() {
78-
if example.unit_ids_applied.is_none() {
79-
continue;
80-
}
81-
82-
let request = match Request::from_example(&test_example_router.config, example) {
83-
Ok(request) => request,
84-
Err(e) => {
85-
results.add_errored_example(route.handler(), example.clone(), e.to_string());
86-
continue;
87-
}
88-
};
89-
let mut unit_trace = UnitTrace::default();
90-
91-
let routes = test_example_router.match_request(&request);
92-
let mut action = Action::from_routes_rule(routes, &request, Some(&mut unit_trace));
93-
94-
let action_status_code = action.get_status_code(0, Some(&mut unit_trace));
95-
let (final_status_code, backend_status_code) = if action_status_code != 0 {
96-
(action_status_code, action_status_code)
97-
} else {
98-
// We call the backend and get a response code
99-
let backend_status_code = example.response_status_code.unwrap_or(200);
100-
let final_status_code = action.get_status_code(backend_status_code, Some(&mut unit_trace));
101-
(final_status_code, backend_status_code)
102-
};
10368

104-
action.filter_headers(Vec::new(), backend_status_code, false, Some(&mut unit_trace));
105-
106-
if let Some(mut body_filter) = action.create_filter_body(backend_status_code, &[]) {
107-
let body = "<!DOCTYPE html>
108-
<html>
109-
<head>
110-
</head>
111-
<body>
112-
</body>
113-
</html>";
114-
115-
body_filter.filter(body.into(), Some(&mut unit_trace));
116-
body_filter.end(Some(&mut unit_trace));
117-
}
118-
119-
action.should_log_request(true, final_status_code, Some(&mut unit_trace));
120-
121-
unit_trace.squash_with_target_unit_traces();
122-
123-
let unit_ids_not_applied_anymore = unit_trace.diff(example.unit_ids_applied.clone().unwrap());
124-
125-
// If it should match but not unit are applied anymore
126-
// If it should match but the rule is not applied
127-
// If it should not matche but the rule is applied
128-
if example.must_match && (!unit_ids_not_applied_anymore.is_empty() || !unit_trace.rule_ids_contains(id.as_str()))
129-
|| !example.must_match && unit_trace.rule_ids_contains(id.as_str())
130-
{
131-
results.add_failed_example(
132-
route.handler(),
133-
example.clone(),
134-
unit_trace.get_rule_ids_applied(),
135-
unit_trace.get_unit_ids_applied(),
136-
unit_ids_not_applied_anymore,
137-
);
138-
}
139-
140-
results.increment_example_count();
141-
}
142-
}
143-
144-
results
69+
Self::create_result(&test_example_router)
14570
}
14671

147-
pub fn create_result(test_examples_input: TestExamplesInput) -> TestExamplesOutput {
72+
pub fn create_result_without_project(test_examples_input: TestExamplesInput) -> TestExamplesOutput {
14873
let mut router = Router::<Rule>::from_config(test_examples_input.router_config.clone());
14974

15075
for rule in test_examples_input.rules.iter() {
15176
router.insert(rule.clone());
15277
}
15378

154-
router.cache(10000);
79+
Self::create_result(&router)
80+
}
15581

82+
fn create_result(router: &Router<Rule>) -> TestExamplesOutput {
15683
let mut results = TestExamplesOutput::default();
15784

158-
for rule in test_examples_input.rules.iter() {
159-
if rule.examples.is_none() {
85+
for (id, route) in router.routes() {
86+
let examples = &route.handler().examples;
87+
88+
if examples.is_none() {
16089
continue;
16190
}
16291

163-
for example in rule.examples.as_ref().unwrap().iter() {
92+
for example in examples.as_ref().unwrap().iter() {
16493
if example.unit_ids_applied.is_none() {
16594
continue;
16695
}
16796

168-
let request = match Request::from_example(&test_examples_input.router_config, example) {
97+
let request = match Request::from_example(&router.config, example) {
16998
Ok(request) => request,
17099
Err(e) => {
171-
results.add_errored_example(rule, example.clone(), e.to_string());
100+
results.add_errored_example(route.handler(), example.clone(), e.to_string());
172101
continue;
173102
}
174103
};
@@ -211,11 +140,11 @@ impl TestExamplesOutput {
211140
// If it should match but not unit are applied anymore
212141
// If it should match but the rule is not applied
213142
// If it should not matche but the rule is applied
214-
if example.must_match && (!unit_ids_not_applied_anymore.is_empty() || !unit_trace.rule_ids_contains(rule.id.as_str()))
215-
|| !example.must_match && unit_trace.rule_ids_contains(rule.id.as_str())
143+
if example.must_match && (!unit_ids_not_applied_anymore.is_empty() || !unit_trace.rule_ids_contains(id.as_str()))
144+
|| !example.must_match && unit_trace.rule_ids_contains(id.as_str())
216145
{
217146
results.add_failed_example(
218-
rule,
147+
route.handler(),
219148
example.clone(),
220149
unit_trace.get_rule_ids_applied(),
221150
unit_trace.get_unit_ids_applied(),

src/api/unit_ids.rs

Lines changed: 19 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -39,92 +39,41 @@ impl UnitIdsOutput {
3939
#[cfg(feature = "router")]
4040
pub fn create_result_from_project(unit_ids_input: UnitIdsProjectInput, existing_router: Arc<Router<Rule>>) -> UnitIdsOutput {
4141
let unit_ids_router = unit_ids_input.change_set.update_existing_router(existing_router);
42-
let mut rules = HashMap::new();
43-
44-
for (id, route) in unit_ids_router.routes() {
45-
let examples = &route.handler().examples;
46-
47-
if examples.is_none() {
48-
continue;
49-
}
50-
51-
let mut examples_output = Vec::new();
5242

53-
for example in examples.as_ref().unwrap() {
54-
let request = match Request::from_example(&unit_ids_router.config, example) {
55-
Ok(request) => request,
56-
Err(_) => {
57-
examples_output.push(example.clone());
58-
continue;
59-
}
60-
};
61-
62-
let mut unit_trace = UnitTrace::default();
63-
64-
let routes = unit_ids_router.match_request(&request);
65-
let mut action = Action::from_routes_rule(routes, &request, Some(&mut unit_trace));
66-
67-
let action_status_code = action.get_status_code(0, Some(&mut unit_trace));
68-
let (_, backend_status_code) = if action_status_code != 0 {
69-
(action_status_code, action_status_code)
70-
} else {
71-
// We call the backend and get a response code
72-
let backend_status_code = example.response_status_code.unwrap_or(200);
73-
let final_status_code = action.get_status_code(backend_status_code, Some(&mut unit_trace));
74-
(final_status_code, backend_status_code)
75-
};
76-
77-
action.filter_headers(Vec::new(), backend_status_code, false, Some(&mut unit_trace));
78-
79-
let body = "<!DOCTYPE html>
80-
<html>
81-
<head>
82-
</head>
83-
<body>
84-
</body>
85-
</html>";
86-
if let Some(mut body_filter) = action.create_filter_body(backend_status_code, &[]) {
87-
body_filter.filter(body.into(), Some(&mut unit_trace));
88-
body_filter.end(Some(&mut unit_trace));
89-
}
90-
91-
unit_trace.squash_with_target_unit_traces();
92-
93-
let mut final_example = example.clone();
94-
final_example.unit_ids_applied = Some(unit_trace.get_unit_ids_applied().into_iter().collect());
95-
examples_output.push(final_example);
96-
}
97-
98-
rules.insert(id.clone(), RuleOutput { examples: examples_output });
99-
}
100-
101-
UnitIdsOutput { rules }
43+
Self::create_result(&unit_ids_router)
10244
}
10345

10446
#[cfg(feature = "router")]
105-
pub fn create_result(unit_ids_input: UnitIdsInput) -> UnitIdsOutput {
47+
pub fn create_result_without_project(unit_ids_input: UnitIdsInput) -> UnitIdsOutput {
10648
let mut router = Router::<Rule>::from_config(unit_ids_input.router_config.clone());
107-
let router_config = router.config.clone();
10849

10950
for rule in unit_ids_input.rules.iter() {
11051
router.insert(rule.clone());
11152
}
11253

11354
router.cache(10000);
11455

56+
Self::create_result(&router)
57+
}
58+
59+
#[cfg(feature = "router")]
60+
fn create_result(router: &Router<Rule>) -> UnitIdsOutput {
11561
let mut rules = HashMap::new();
11662

117-
for rule in unit_ids_input.rules {
118-
if rule.examples.is_none() {
63+
for (id, route) in router.routes() {
64+
let examples = &route.handler().examples;
65+
66+
if examples.is_none() {
11967
continue;
12068
}
121-
let mut examples = Vec::new();
12269

123-
for example in rule.examples.unwrap() {
124-
let request = match Request::from_example(&router_config, &example) {
70+
let mut examples_output = Vec::new();
71+
72+
for example in examples.as_ref().unwrap() {
73+
let request = match Request::from_example(&router.config, example) {
12574
Ok(request) => request,
12675
Err(_) => {
127-
examples.push(example.clone());
76+
examples_output.push(example.clone());
12877
continue;
12978
}
13079
};
@@ -162,11 +111,12 @@ impl UnitIdsOutput {
162111

163112
let mut final_example = example.clone();
164113
final_example.unit_ids_applied = Some(unit_trace.get_unit_ids_applied().into_iter().collect());
165-
examples.push(final_example);
114+
examples_output.push(final_example);
166115
}
167116

168-
rules.insert(rule.id.clone(), RuleOutput { examples });
117+
rules.insert(id.clone(), RuleOutput { examples: examples_output });
169118
}
119+
170120
UnitIdsOutput { rules }
171121
}
172122
}

0 commit comments

Comments
 (0)