Skip to content

Commit 1864261

Browse files
committed
explicit inner/outer control on fangs application & fix fmt and cfg
1 parent 1a786df commit 1864261

3 files changed

Lines changed: 41 additions & 76 deletions

File tree

ohkami/src/ohkami/mod.rs

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,11 +1268,12 @@ mod test {
12681268
}
12691269

12701270
#[cfg(test)]
1271+
#[cfg(feature = "__rt_native__")]
12711272
mod nested_fang_regression_test {
1272-
use crate::{Ohkami, Request, Response, Route};
12731273
use crate::claw::status;
1274-
use crate::fang::{FangAction, Context};
1274+
use crate::fang::{Context, FangAction};
12751275
use crate::testing::{Status, TestRequest, Tester};
1276+
use crate::{Ohkami, Request, Response, Route};
12761277

12771278
#[derive(Clone, Debug, PartialEq, Eq)]
12781279
struct Principal(&'static str);
@@ -1336,15 +1337,9 @@ mod nested_fang_regression_test {
13361337
"/accounting/reconciliation".GET(accounting_reconciliation_handler),
13371338
));
13381339

1339-
let protected_routes = Ohkami::new((
1340-
ParentAuthFang,
1341-
"/ops".By(ops_routes),
1342-
));
1340+
let protected_routes = Ohkami::new((ParentAuthFang, "/ops".By(ops_routes)));
13431341

1344-
let app = Ohkami::new((
1345-
Context::new(()),
1346-
"/api".By(protected_routes),
1347-
));
1342+
let app = Ohkami::new((Context::new(()), "/api".By(protected_routes)));
13481343

13491344
let tester = app.test();
13501345

@@ -1390,42 +1385,21 @@ mod nested_fang_regression_test {
13901385
});
13911386
}
13921387

1393-
13941388
#[test]
13951389
fn parent_context_auth_is_visible_to_nested_local_route_fangs_in_realistic_order() {
13961390
crate::__rt__::testing::block_on(async {
13971391
let ops_routes = Ohkami::new((
1398-
"/routing/health".GET((
1399-
OpsAuthorizationFang,
1400-
routing_health_handler,
1401-
)),
1402-
"/routing/override".POST((
1403-
OpsAuthorizationFang,
1404-
routing_override_set_handler,
1405-
)),
1406-
"/routing/override".DELETE((
1407-
OpsAuthorizationFang,
1408-
routing_override_clear_handler,
1409-
)),
1410-
"/metrics".GET((
1411-
OpsAuthorizationFang,
1412-
metrics_handler,
1413-
)),
1414-
"/accounting/reconciliation".GET((
1415-
OpsAuthorizationFang,
1416-
accounting_reconciliation_handler,
1417-
)),
1392+
"/routing/health".GET((OpsAuthorizationFang, routing_health_handler)),
1393+
"/routing/override".POST((OpsAuthorizationFang, routing_override_set_handler)),
1394+
"/routing/override".DELETE((OpsAuthorizationFang, routing_override_clear_handler)),
1395+
"/metrics".GET((OpsAuthorizationFang, metrics_handler)),
1396+
"/accounting/reconciliation"
1397+
.GET((OpsAuthorizationFang, accounting_reconciliation_handler)),
14181398
));
14191399

1420-
let protected_routes = Ohkami::new((
1421-
ParentAuthFang,
1422-
"/ops".By(ops_routes),
1423-
));
1400+
let protected_routes = Ohkami::new((ParentAuthFang, "/ops".By(ops_routes)));
14241401

1425-
let app = Ohkami::new((
1426-
Context::new(()),
1427-
"/api".By(protected_routes),
1428-
));
1402+
let app = Ohkami::new((Context::new(()), "/api".By(protected_routes)));
14291403

14301404
let tester = app.test();
14311405

@@ -1470,4 +1444,4 @@ mod nested_fang_regression_test {
14701444
assert_eq!(accounting_res.status(), Status::OK);
14711445
});
14721446
}
1473-
}
1447+
}

ohkami/src/router/base.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,44 +77,42 @@ impl FangsList {
7777
Self(Vec::new())
7878
}
7979

80-
fn add(&mut self, id: ID, fangs: Arc<dyn Fangs>) {
80+
fn add_inner(&mut self, id: ID, fangs: Arc<dyn Fangs>) {
8181
if self.0.iter().all(|(_id, _)| *_id != id) {
8282
self.0.push((id, fangs));
8383
}
8484
}
85-
pub(super) fn append(&mut self, another: Self) {
86-
for (id, fangs) in another.0.into_iter() {
87-
self.add(id, fangs)
85+
fn add_outer(&mut self, id: ID, fangs: Arc<dyn Fangs>) {
86+
if self.0.iter().all(|(_id, _)| *_id != id) {
87+
self.0.insert(0, (id, fangs));
8888
}
8989
}
90-
91-
/// yield from most inner fangs
92-
fn into_iter(self) -> impl Iterator<Item = Arc<dyn Fangs>> {
93-
self.0.into_iter().map(|(_, fangs)| fangs)
90+
pub(super) fn append_inner(&mut self, another: Self) {
91+
for (id, fangs) in another.0.into_iter() {
92+
self.add_inner(id, fangs);
93+
}
9494
}
9595

9696
pub(super) fn into_proc_with(self, h: Handler) -> IntoProcWith {
97-
let mut iter = self.into_iter();
98-
9997
#[cfg(not(feature = "openapi"))]
100-
match iter.next() {
101-
None => h.proc,
102-
Some(most_inner) => {
103-
iter.fold(most_inner.build(h.proc), |proc, fangs| fangs.build(proc))
104-
}
98+
{
99+
self.0
100+
.into_iter()
101+
.rfold(h.proc, |proc, (_, most_inner_fangs)| {
102+
most_inner_fangs.build(proc)
103+
})
105104
}
106105
#[cfg(feature = "openapi")]
107-
match iter.next() {
108-
None => (h.proc, h.openapi_operation),
109-
Some(most_inner) => iter.fold(
110-
(
111-
most_inner.build(h.proc),
112-
most_inner.openapi_map_operation(h.openapi_operation),
113-
),
114-
|(proc, operation), fangs| {
115-
(fangs.build(proc), fangs.openapi_map_operation(operation))
106+
{
107+
self.0.into_iter().rfold(
108+
(h.proc, h.openapi_operation),
109+
|(proc, op), (_, most_inner_fangs)| {
110+
(
111+
most_inner_fangs.build(proc),
112+
most_inner_fangs.openapi_map_operation(operation),
113+
)
116114
},
117-
),
115+
)
118116
}
119117
}
120118
}
@@ -362,10 +360,6 @@ impl Node {
362360
}
363361
}
364362

365-
fn append_fangs(&mut self, fangs: FangsList) {
366-
self.fangses.append(fangs);
367-
}
368-
369363
fn set_handler(&mut self, new_handler: Handler, allow_override: bool) -> Result<(), String> {
370364
if self.handler.is_some() && !allow_override {
371365
return Err(format!("Conflicting handler registering"));
@@ -414,7 +408,7 @@ impl Node {
414408
panic!("Unexpectedly called `Node::merge_here` where `another_root` is not root node")
415409
};
416410

417-
self.append_fangs(another_root_fangses);
411+
self.fangses.append_inner(another_root_fangses);
418412

419413
if let Some(h) = another_root_handler {
420414
self.set_handler(h, allow_override_handler)?;
@@ -432,10 +426,10 @@ impl Node {
432426
for child in &mut self.children {
433427
child.apply_fangs(id, fangs.clone())
434428
}
435-
436429
// Add even when `self.handler.is_none()`. They are used later
437430
// for applying to `Handler::default_notfound`s in `finalize`.
438-
self.fangses.add(id, fangs);
431+
// This `fangses` must be added by `_outer` to *wrap* existing fangs.
432+
self.fangses.add_outer(id, fangs);
439433
}
440434
}
441435

ohkami/src/router/final.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,7 @@ const _: (/* conversions */) = {
287287
let child = base.children.pop().unwrap(/* base.children.len() == 1 */);
288288
base.children = child.children;
289289
base.handler = child.handler;
290-
// Preserve outer/inner order: child fangs should be inner, base fangs outer.
291-
let mut merged_fangses = child.fangses;
292-
merged_fangses.append(base.fangses);
293-
base.fangses = merged_fangses;
290+
base.fangses.append_inner(child.fangses);
294291
base.pattern = Some(match base.pattern {
295292
None => child.pattern.unwrap(/* not root */),
296293
Some(p) => p.merge_statics(child.pattern.unwrap(/* not root */)).unwrap(/* both are Pattern::Static */)

0 commit comments

Comments
 (0)