Skip to content

Commit f2fa059

Browse files
committed
doc: comments
1 parent cac52f7 commit f2fa059

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

src/lib.rs

+48-36
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@
135135
#![forbid(unsafe_code)]
136136
#![warn(rust_2018_idioms, unreachable_pub)]
137137

138-
use std::{iter, marker::PhantomData, slice, str::from_utf8};
138+
use std::{
139+
iter::{Copied, FilterMap, Zip},
140+
marker::PhantomData,
141+
slice::Iter,
142+
str::from_utf8,
143+
};
139144

140145
use smallvec::SmallVec;
141146

@@ -145,6 +150,7 @@ mod parser;
145150
pub use node::{Node, NodeKind};
146151
pub use parser::{Kind, Parser, Piece, Position};
147152

153+
/// A path tree.
148154
#[derive(Debug)]
149155
pub struct PathTree<T> {
150156
id: usize,
@@ -159,6 +165,7 @@ impl<T> Default for PathTree<T> {
159165
}
160166

161167
impl<T> PathTree<T> {
168+
/// Creates a new [PathTree].
162169
pub fn new() -> Self {
163170
Self {
164171
id: 0,
@@ -167,6 +174,7 @@ impl<T> PathTree<T> {
167174
}
168175
}
169176

177+
/// Inserts a part path-value to the tree and returns the id.
170178
pub fn insert(&mut self, path: &str, value: T) -> usize {
171179
let mut node = &mut self.node;
172180

@@ -202,6 +210,7 @@ impl<T> PathTree<T> {
202210
}
203211
}
204212

213+
/// Returns the [Path] by the given path.
205214
pub fn find<'b>(&self, path: &'b str) -> Option<Path<'_, 'b, T>> {
206215
let bytes = path.as_bytes();
207216
self.node.find(bytes).and_then(|(id, ranges)| {
@@ -221,12 +230,13 @@ impl<T> PathTree<T> {
221230
})
222231
}
223232

233+
/// Gets the route by id.
224234
#[inline]
225235
pub fn get_route(&self, index: usize) -> Option<&(T, Vec<Piece>)> {
226236
self.routes.get(index)
227237
}
228238

229-
/// Generates URL
239+
/// Generates URL with the params.
230240
pub fn url_for(&self, index: usize, params: &[&str]) -> Option<String> {
231241
self.get_route(index).map(|(_, pieces)| {
232242
let mut bytes = Vec::new();
@@ -248,6 +258,7 @@ impl<T> PathTree<T> {
248258
}
249259
}
250260

261+
/// Matched route path infomation.
251262
#[derive(Debug, PartialEq, Eq)]
252263
pub struct Path<'a, 'b, T> {
253264
pub id: &'a usize,
@@ -257,53 +268,54 @@ pub struct Path<'a, 'b, T> {
257268
}
258269

259270
impl<'a, 'b, T> Path<'a, 'b, T> {
271+
/// Gets current path pattern.
260272
pub fn pattern(&self) -> String {
261273
let mut bytes = Vec::new();
262274

263-
for piece in self.pieces {
264-
match piece {
265-
Piece::String(s) => {
266-
if s == b":" || s == b"+" || s == b"?" {
267-
bytes.push(b'\\');
275+
self.pieces.iter().for_each(|piece| match piece {
276+
Piece::String(s) => {
277+
if s == b":" || s == b"+" || s == b"?" {
278+
bytes.push(b'\\');
279+
}
280+
bytes.extend_from_slice(s);
281+
}
282+
Piece::Parameter(p, k) => match p {
283+
Position::Index(_, _) => {
284+
if *k == Kind::OneOrMore {
285+
bytes.push(b'+');
286+
} else if *k == Kind::ZeroOrMore || *k == Kind::ZeroOrMoreSegment {
287+
bytes.push(b'*');
268288
}
269-
bytes.extend_from_slice(s);
270289
}
271-
Piece::Parameter(p, k) => match p {
272-
Position::Index(_, _) => {
273-
if *k == Kind::OneOrMore {
274-
bytes.push(b'+');
275-
} else if *k == Kind::ZeroOrMore || *k == Kind::ZeroOrMoreSegment {
276-
bytes.push(b'*');
290+
Position::Named(n) => match k {
291+
Kind::Normal | Kind::Optional | Kind::OptionalSegment => {
292+
bytes.push(b':');
293+
bytes.extend_from_slice(n);
294+
if *k == Kind::Optional || *k == Kind::OptionalSegment {
295+
bytes.push(b'?');
277296
}
278297
}
279-
Position::Named(n) => match k {
280-
Kind::Normal | Kind::Optional | Kind::OptionalSegment => {
281-
bytes.push(b':');
282-
bytes.extend_from_slice(n);
283-
if *k == Kind::Optional || *k == Kind::OptionalSegment {
284-
bytes.push(b'?');
285-
}
286-
}
287-
Kind::OneOrMore => {
288-
bytes.push(b'+');
289-
bytes.extend_from_slice(n);
290-
}
291-
Kind::ZeroOrMore | Kind::ZeroOrMoreSegment => {
292-
bytes.push(b'*');
293-
bytes.extend_from_slice(n);
294-
}
295-
},
298+
Kind::OneOrMore => {
299+
bytes.push(b'+');
300+
bytes.extend_from_slice(n);
301+
}
302+
Kind::ZeroOrMore | Kind::ZeroOrMoreSegment => {
303+
bytes.push(b'*');
304+
bytes.extend_from_slice(n);
305+
}
296306
},
297-
}
298-
}
307+
},
308+
});
299309

300310
String::from_utf8_lossy(&bytes).to_string()
301311
}
302312

313+
/// Returns the parameters of the current path.
303314
pub fn params(&self) -> Vec<(&'a str, &'b str)> {
304315
self.params_iter().collect()
305316
}
306317

318+
/// Returns the parameters iterator of the current path.
307319
pub fn params_iter<'p>(&'p self) -> ParamsIter<'p, 'a, 'b, T> {
308320
#[inline]
309321
fn piece_filter(piece: &Piece) -> Option<&'_ str> {
@@ -328,11 +340,11 @@ impl<'a, 'b, T> Path<'a, 'b, T> {
328340
}
329341
}
330342

331-
type FilterIter<'a> =
332-
iter::FilterMap<slice::Iter<'a, Piece>, fn(piece: &'a Piece) -> Option<&'a str>>;
343+
type FilterIter<'a> = FilterMap<Iter<'a, Piece>, fn(piece: &'a Piece) -> Option<&'a str>>;
333344

345+
/// A Parameters Iterator.
334346
pub struct ParamsIter<'p, 'a, 'b, T> {
335-
iter: iter::Zip<FilterIter<'a>, std::iter::Copied<slice::Iter<'p, &'b str>>>,
347+
iter: Zip<FilterIter<'a>, Copied<Iter<'p, &'b str>>>,
336348
_t: PhantomData<T>,
337349
}
338350

tests/tree.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use rand::seq::SliceRandom;
33

44
#[test]
55
fn statics() {
6-
const ROUTES: [&str; 13] = [
7-
"",
6+
const ROUTES: [&str; 12] = [
87
"/",
98
"/hi",
109
"/contact",

0 commit comments

Comments
 (0)