Skip to content

Commit 165c37b

Browse files
committed
simplify Router::remove
1 parent 32370d8 commit 165c37b

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

src/tree.rs

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -290,56 +290,60 @@ impl<T> Node<T> {
290290

291291
let mut current = self;
292292
'walk: loop {
293-
// The path is longer than this node's prefix, search deeper.
294-
if remaining.len() > current.prefix.len() {
295-
let (prefix, rest) = remaining.split_at(current.prefix.len());
296-
297-
// The prefix matches.
298-
if prefix == current.prefix.unescaped() {
299-
let first = rest[0];
300-
remaining = rest;
301-
302-
// If there is a single child node, we can continue searching in the child.
303-
if current.children.len() == 1 {
304-
// The route matches, remove the node.
305-
if current.children[0].prefix.unescaped() == remaining {
306-
return current.remove_child(0, &remapping);
307-
}
293+
// Could not find a match.
294+
if remaining.len() <= current.prefix.len() {
295+
return None;
296+
}
308297

309-
// Otherwise, continue searching.
310-
current = &mut current.children[0];
311-
continue 'walk;
312-
}
298+
// Otherwise, the path is longer than this node's prefix, search deeper.
299+
let (prefix, rest) = remaining.split_at(current.prefix.len());
313300

314-
// Find a child node that matches the next character in the route.
315-
if let Some(i) = current.indices.iter().position(|&c| c == first) {
316-
// The route matches, remove the node.
317-
if current.children[i].prefix.unescaped() == remaining {
318-
return current.remove_child(i, &remapping);
319-
}
301+
// The prefix does not match.
302+
if prefix != current.prefix.unescaped() {
303+
return None;
304+
}
320305

321-
// Otherwise, continue searching.
322-
current = &mut current.children[i];
323-
continue 'walk;
324-
}
306+
let first = rest[0];
307+
remaining = rest;
325308

326-
// If the node has a matching wildcard child, continue searching in the child.
327-
if current.wild_child
328-
&& remaining.first().zip(remaining.get(2)) == Some((&b'{', &b'}'))
329-
{
330-
// The route matches, remove the node.
331-
if current.children.last_mut().unwrap().prefix.unescaped() == remaining {
332-
return current.remove_child(current.children.len() - 1, &remapping);
333-
}
309+
// If there is a single child node, we can continue searching in the child.
310+
if current.children.len() == 1 {
311+
// The route matches, remove the node.
312+
if current.children[0].prefix.unescaped() == remaining {
313+
return current.remove_child(0, &remapping);
314+
}
334315

335-
current = current.children.last_mut().unwrap();
336-
continue 'walk;
337-
}
316+
// Otherwise, continue searching.
317+
current = &mut current.children[0];
318+
continue 'walk;
319+
}
320+
321+
// Find a child node that matches the next character in the route.
322+
if let Some(i) = current.indices.iter().position(|&c| c == first) {
323+
// The route matches, remove the node.
324+
if current.children[i].prefix.unescaped() == remaining {
325+
return current.remove_child(i, &remapping);
338326
}
327+
328+
// Otherwise, continue searching.
329+
current = &mut current.children[i];
330+
continue 'walk;
339331
}
340332

341-
// Could not find a match.
342-
return None;
333+
// If there is no matching wildcard child, there is no matching route.
334+
if !current.wild_child
335+
|| remaining.first().zip(remaining.get(2)) != Some((&b'{', &b'}'))
336+
{
337+
return None;
338+
}
339+
340+
// If the route does match, remove the node.
341+
if current.children.last_mut().unwrap().prefix.unescaped() == remaining {
342+
return current.remove_child(current.children.len() - 1, &remapping);
343+
}
344+
345+
// Otherwise, keep searching deeper.
346+
current = current.children.last_mut().unwrap();
343347
}
344348
}
345349

0 commit comments

Comments
 (0)