Skip to content

Commit bd111f6

Browse files
committed
Rename specificity to priority
1 parent 78714e7 commit bd111f6

File tree

7 files changed

+28
-36
lines changed

7 files changed

+28
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
### Fixed
3030

3131
- Improved detection of structural conflicts.
32-
- Correcting routing specificity for inline parameters.
32+
- Correcting routing priority for inline parameters.
3333

3434
## [0.8.1] - 2025-01-07
3535

src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,12 @@
119119
//!
120120
//! Templates are matched using a hierarchical priority system.
121121
//!
122-
//! ### 1. Kind
123-
//!
124122
//! From highest priority to lowest, we walk the current nodes children in this order:
125123
//! - statics
126124
//! - dynamics
127125
//! - wildcards
128126
//! - end wildcards
129127
//!
130-
//! In the event of multiple children of the same type, we walk them in alphabetical order, to ensure order remains predictable.
131-
//!
132-
//! ### 2. Structure
133-
//!
134-
//! When comparing templates at the same node level and of the same kind, we prefer the "more specific" template.
135-
//!
136128
//! ## Display
137129
//!
138130
//! The router can be printed as a tree, via a [`Display`](std::fmt::Display) implementation.
@@ -156,5 +148,5 @@ mod parser;
156148
mod router;
157149
pub use router::{Match, Router};
158150

159-
mod specificity;
151+
mod priority;
160152
mod state;

src/node.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use alloc::{boxed::Box, string::String, vec::Vec};
22
use core::cmp::Ordering;
33

44
use crate::{
5-
specificity::Specificity,
5+
priority::Priority,
66
state::{DynamicState, EndWildcardState, StaticState, WildcardState},
77
};
88

@@ -22,8 +22,8 @@ pub struct NodeData {
2222
/// This nodes template.
2323
pub template: String,
2424

25-
/// The specificity of the template.
26-
pub specificity: Specificity,
25+
/// The priority of the template.
26+
pub priority: Priority,
2727
}
2828

2929
/// Represents a node in the tree structure.
@@ -46,7 +46,7 @@ pub struct Node<S> {
4646
pub wildcard_segment_only: bool,
4747

4848
/// Flag indicating whether this node need optimization.
49-
/// During optimization, the shortcut flags are updated, specificity calculated, and nodes sorted.
49+
/// During optimization, the shortcut flags are updated, priority calculated, and nodes sorted.
5050
pub needs_optimization: bool,
5151
}
5252

src/node/optimize.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
use crate::{node::Node, specificity::Specificity};
1+
use crate::{node::Node, priority::Priority};
22

33
impl<S> Node<S> {
44
/// Optimizes the tree structure.
55
pub(crate) fn optimize(&mut self) {
6-
self.optimize_inner(Specificity::default());
6+
self.optimize_inner(Priority::default());
77
}
88

99
/// Recursively optimizes nodes from root to leaf.
1010
/// We can skip optimization if the current node hasn't changed.
11-
fn optimize_inner(&mut self, parent: Specificity) {
11+
fn optimize_inner(&mut self, parent: Priority) {
1212
if !self.needs_optimization {
1313
return;
1414
}
1515

1616
if let Some(data) = &mut self.data {
17-
data.specificity = parent.clone();
17+
data.priority = parent.clone();
1818
}
1919

2020
for child in &mut self.static_children {
21-
let child_specificity = parent.clone().with_static(child.state.prefix.len());
22-
child.optimize_inner(child_specificity);
21+
let child_priority = parent.clone().with_static(child.state.prefix.len());
22+
child.optimize_inner(child_priority);
2323
}
2424

2525
for child in &mut self.dynamic_children {
26-
let child_specificity = parent.clone().with_dynamic();
27-
child.optimize_inner(child_specificity);
26+
let child_priority = parent.clone().with_dynamic();
27+
child.optimize_inner(child_priority);
2828
}
2929

3030
for child in &mut self.wildcard_children {
31-
let child_specificity = parent.clone().with_wildcard();
32-
child.optimize_inner(child_specificity);
31+
let child_priority = parent.clone().with_wildcard();
32+
child.optimize_inner(child_priority);
3333
}
3434

3535
if let Some(child) = &mut self.end_wildcard {
36-
let child_specificity = parent.with_wildcard();
37-
child.optimize_inner(child_specificity);
36+
let child_priority = parent.with_wildcard();
37+
child.optimize_inner(child_priority);
3838
}
3939

4040
self.static_children.sort();

src/node/search.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<S> Node<S> {
9999

100100
/// Slower dynamic path for complex templates like `/<name>.<extension>`.
101101
/// Must try each byte to consider all possible permutations.
102-
/// Prefers the most specific match.
102+
/// Prefers the most specific match, via backtracking.
103103
fn search_dynamic_inline<'r, 'p>(
104104
&'r self,
105105
path: &'p [u8],
@@ -127,7 +127,7 @@ impl<S> Node<S> {
127127
continue;
128128
};
129129

130-
if best_match.is_none_or(|best| data.specificity >= best.specificity) {
130+
if best_match.is_none_or(|best| data.priority >= best.priority) {
131131
best_match = Some(data);
132132
best_match_parameters = current_parameters;
133133
}
@@ -199,7 +199,7 @@ impl<S> Node<S> {
199199

200200
/// Slower wildcard path for complex templates like `/<*name>.txt`.
201201
/// Must try each byte, since the wildcard ends mid-segment.
202-
/// Prefers the most specific match.
202+
/// Prefers the most specific match, via backtracking.
203203
fn search_wildcard_inline<'r, 'p>(
204204
&'r self,
205205
path: &'p [u8],
@@ -223,7 +223,7 @@ impl<S> Node<S> {
223223
continue;
224224
};
225225

226-
if best_match.is_none_or(|best| data.specificity >= best.specificity) {
226+
if best_match.is_none_or(|best| data.priority >= best.priority) {
227227
best_match = Some(data);
228228
best_match_parameters = current_parameters;
229229
}

src/specificity.rs renamed to src/priority.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use core::cmp::Ordering;
77
/// 2. Dynamic parameter count: less dynamics, more specific
88
/// 3. Wildcard parameter count: less wildcards, more specific
99
#[derive(Clone, Eq, PartialEq, Debug, Default)]
10-
pub struct Specificity {
10+
pub struct Priority {
1111
pub static_length: usize,
1212
pub dynamics_count: usize,
1313
pub wildcards_count: usize,
1414
}
1515

16-
impl Specificity {
16+
impl Priority {
1717
pub const fn with_static(mut self, length: usize) -> Self {
1818
self.static_length += length;
1919
self
@@ -30,13 +30,13 @@ impl Specificity {
3030
}
3131
}
3232

33-
impl PartialOrd for Specificity {
33+
impl PartialOrd for Priority {
3434
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
3535
Some(self.cmp(other))
3636
}
3737
}
3838

39-
impl Ord for Specificity {
39+
impl Ord for Priority {
4040
fn cmp(&self, other: &Self) -> Ordering {
4141
self.static_length
4242
.cmp(&other.static_length)

src/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
errors::{DeleteError, InsertError},
99
node::{Node, NodeData},
1010
parser::Template,
11-
specificity::Specificity,
11+
priority::Priority,
1212
state::RootState,
1313
};
1414

@@ -94,7 +94,7 @@ impl<T> Router<T> {
9494
NodeData {
9595
key,
9696
template: template.to_owned(),
97-
specificity: Specificity::default(),
97+
priority: Priority::default(),
9898
},
9999
);
100100

0 commit comments

Comments
 (0)