Skip to content

Commit f6b9eca

Browse files
authored
Merge pull request #38 from Nemo157/cleanup
Cleanup
2 parents b29d75b + 12f48e9 commit f6b9eca

File tree

4 files changed

+346
-333
lines changed

4 files changed

+346
-333
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ matrix:
2020
- rustup component add clippy
2121
script:
2222
- cargo clippy --lib --tests -- -Dwarnings
23+
24+
- name: cargo +nightly bench
25+
rust: nightly
26+
script:
27+
- cargo bench

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ description = "Recognizes URL patterns with support for dynamic and glob segment
44
license = "MIT"
55
repository = "https://github.com/rustasync/route-recognizer"
66
keywords = ["router", "url"]
7+
edition = "2018"
78

89
version = "0.1.13"
910
authors = ["wycats", "rustasync"]

src/lib.rs

Lines changed: 128 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use nfa::CharacterClass;
2-
use nfa::NFA;
3-
use std::cmp::Ordering;
4-
use std::collections::btree_map;
5-
use std::collections::BTreeMap;
6-
use std::ops::Index;
1+
use std::{
2+
cmp::Ordering,
3+
collections::{btree_map, BTreeMap},
4+
ops::Index,
5+
};
6+
7+
use crate::nfa::{CharacterClass, NFA};
78

89
pub mod nfa;
910

@@ -16,8 +17,8 @@ struct Metadata {
1617
}
1718

1819
impl Metadata {
19-
pub fn new() -> Metadata {
20-
Metadata {
20+
pub fn new() -> Self {
21+
Self {
2122
statics: 0,
2223
dynamics: 0,
2324
stars: 0,
@@ -27,7 +28,7 @@ impl Metadata {
2728
}
2829

2930
impl Ord for Metadata {
30-
fn cmp(&self, other: &Metadata) -> Ordering {
31+
fn cmp(&self, other: &Self) -> Ordering {
3132
if self.statics > other.statics {
3233
Ordering::Greater
3334
} else if self.statics < other.statics {
@@ -47,13 +48,13 @@ impl Ord for Metadata {
4748
}
4849

4950
impl PartialOrd for Metadata {
50-
fn partial_cmp(&self, other: &Metadata) -> Option<Ordering> {
51+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
5152
Some(self.cmp(other))
5253
}
5354
}
5455

5556
impl PartialEq for Metadata {
56-
fn eq(&self, other: &Metadata) -> bool {
57+
fn eq(&self, other: &Self) -> bool {
5758
self.statics == other.statics
5859
&& self.dynamics == other.dynamics
5960
&& self.stars == other.stars
@@ -68,8 +69,8 @@ pub struct Params {
6869
}
6970

7071
impl Params {
71-
pub fn new() -> Params {
72-
Params {
72+
pub fn new() -> Self {
73+
Self {
7374
map: BTreeMap::new(),
7475
}
7576
}
@@ -87,9 +88,9 @@ impl Params {
8788
}
8889
}
8990

90-
impl<'a> Index<&'a str> for Params {
91+
impl Index<&str> for Params {
9192
type Output = String;
92-
fn index(&self, index: &'a str) -> &String {
93+
fn index(&self, index: &str) -> &String {
9394
match self.map.get(index) {
9495
None => panic!(format!("params[{}] did not exist", index)),
9596
Some(s) => s,
@@ -127,8 +128,8 @@ pub struct Match<T> {
127128
}
128129

129130
impl<T> Match<T> {
130-
pub fn new(handler: T, params: Params) -> Match<T> {
131-
Match { handler, params }
131+
pub fn new(handler: T, params: Params) -> Self {
132+
Self { handler, params }
132133
}
133134
}
134135

@@ -139,8 +140,8 @@ pub struct Router<T> {
139140
}
140141

141142
impl<T> Router<T> {
142-
pub fn new() -> Router<T> {
143-
Router {
143+
pub fn new() -> Self {
144+
Self {
144145
nfa: NFA::new(),
145146
handlers: BTreeMap::new(),
146147
}
@@ -179,7 +180,7 @@ impl<T> Router<T> {
179180
self.handlers.insert(state, dest);
180181
}
181182

182-
pub fn recognize<'a>(&'a self, mut path: &str) -> Result<Match<&'a T>, String> {
183+
pub fn recognize(&self, mut path: &str) -> Result<Match<&T>, String> {
183184
if !path.is_empty() && path.as_bytes()[0] == b'/' {
184185
path = &path[1..];
185186
}
@@ -240,139 +241,142 @@ fn process_star_state<T>(nfa: &mut NFA<T>, mut state: usize) -> usize {
240241
state
241242
}
242243

243-
#[test]
244-
fn basic_router() {
245-
let mut router = Router::new();
244+
#[cfg(test)]
245+
mod tests {
246+
use super::{Params, Router};
246247

247-
router.add("/thomas", "Thomas".to_string());
248-
router.add("/tom", "Tom".to_string());
249-
router.add("/wycats", "Yehuda".to_string());
248+
#[test]
249+
fn basic_router() {
250+
let mut router = Router::new();
250251

251-
let m = router.recognize("/thomas").unwrap();
252+
router.add("/thomas", "Thomas".to_string());
253+
router.add("/tom", "Tom".to_string());
254+
router.add("/wycats", "Yehuda".to_string());
252255

253-
assert_eq!(*m.handler, "Thomas".to_string());
254-
assert_eq!(m.params, Params::new());
255-
}
256+
let m = router.recognize("/thomas").unwrap();
256257

257-
#[test]
258-
fn root_router() {
259-
let mut router = Router::new();
260-
router.add("/", 10);
261-
assert_eq!(*router.recognize("/").unwrap().handler, 10)
262-
}
258+
assert_eq!(*m.handler, "Thomas".to_string());
259+
assert_eq!(m.params, Params::new());
260+
}
263261

264-
#[test]
265-
fn empty_path() {
266-
let mut router = Router::new();
267-
router.add("/", 12);
268-
assert_eq!(*router.recognize("").unwrap().handler, 12)
269-
}
262+
#[test]
263+
fn root_router() {
264+
let mut router = Router::new();
265+
router.add("/", 10);
266+
assert_eq!(*router.recognize("/").unwrap().handler, 10)
267+
}
270268

271-
#[test]
272-
fn empty_route() {
273-
let mut router = Router::new();
274-
router.add("", 12);
275-
assert_eq!(*router.recognize("/").unwrap().handler, 12)
276-
}
269+
#[test]
270+
fn empty_path() {
271+
let mut router = Router::new();
272+
router.add("/", 12);
273+
assert_eq!(*router.recognize("").unwrap().handler, 12)
274+
}
277275

278-
#[test]
279-
fn ambiguous_router() {
280-
let mut router = Router::new();
276+
#[test]
277+
fn empty_route() {
278+
let mut router = Router::new();
279+
router.add("", 12);
280+
assert_eq!(*router.recognize("/").unwrap().handler, 12)
281+
}
281282

282-
router.add("/posts/new", "new".to_string());
283-
router.add("/posts/:id", "id".to_string());
283+
#[test]
284+
fn ambiguous_router() {
285+
let mut router = Router::new();
284286

285-
let id = router.recognize("/posts/1").unwrap();
287+
router.add("/posts/new", "new".to_string());
288+
router.add("/posts/:id", "id".to_string());
286289

287-
assert_eq!(*id.handler, "id".to_string());
288-
assert_eq!(id.params, params("id", "1"));
290+
let id = router.recognize("/posts/1").unwrap();
289291

290-
let new = router.recognize("/posts/new").unwrap();
291-
assert_eq!(*new.handler, "new".to_string());
292-
assert_eq!(new.params, Params::new());
293-
}
292+
assert_eq!(*id.handler, "id".to_string());
293+
assert_eq!(id.params, params("id", "1"));
294294

295-
#[test]
296-
fn ambiguous_router_b() {
297-
let mut router = Router::new();
295+
let new = router.recognize("/posts/new").unwrap();
296+
assert_eq!(*new.handler, "new".to_string());
297+
assert_eq!(new.params, Params::new());
298+
}
298299

299-
router.add("/posts/:id", "id".to_string());
300-
router.add("/posts/new", "new".to_string());
300+
#[test]
301+
fn ambiguous_router_b() {
302+
let mut router = Router::new();
301303

302-
let id = router.recognize("/posts/1").unwrap();
304+
router.add("/posts/:id", "id".to_string());
305+
router.add("/posts/new", "new".to_string());
303306

304-
assert_eq!(*id.handler, "id".to_string());
305-
assert_eq!(id.params, params("id", "1"));
307+
let id = router.recognize("/posts/1").unwrap();
306308

307-
let new = router.recognize("/posts/new").unwrap();
308-
assert_eq!(*new.handler, "new".to_string());
309-
assert_eq!(new.params, Params::new());
310-
}
309+
assert_eq!(*id.handler, "id".to_string());
310+
assert_eq!(id.params, params("id", "1"));
311311

312-
#[test]
313-
fn multiple_params() {
314-
let mut router = Router::new();
312+
let new = router.recognize("/posts/new").unwrap();
313+
assert_eq!(*new.handler, "new".to_string());
314+
assert_eq!(new.params, Params::new());
315+
}
315316

316-
router.add("/posts/:post_id/comments/:id", "comment".to_string());
317-
router.add("/posts/:post_id/comments", "comments".to_string());
317+
#[test]
318+
fn multiple_params() {
319+
let mut router = Router::new();
318320

319-
let com = router.recognize("/posts/12/comments/100").unwrap();
320-
let coms = router.recognize("/posts/12/comments").unwrap();
321+
router.add("/posts/:post_id/comments/:id", "comment".to_string());
322+
router.add("/posts/:post_id/comments", "comments".to_string());
321323

322-
assert_eq!(*com.handler, "comment".to_string());
323-
assert_eq!(com.params, two_params("post_id", "12", "id", "100"));
324+
let com = router.recognize("/posts/12/comments/100").unwrap();
325+
let coms = router.recognize("/posts/12/comments").unwrap();
324326

325-
assert_eq!(*coms.handler, "comments".to_string());
326-
assert_eq!(coms.params, params("post_id", "12"));
327-
assert_eq!(coms.params["post_id"], "12".to_string());
328-
}
327+
assert_eq!(*com.handler, "comment".to_string());
328+
assert_eq!(com.params, two_params("post_id", "12", "id", "100"));
329329

330-
#[test]
331-
fn star() {
332-
let mut router = Router::new();
330+
assert_eq!(*coms.handler, "comments".to_string());
331+
assert_eq!(coms.params, params("post_id", "12"));
332+
assert_eq!(coms.params["post_id"], "12".to_string());
333+
}
333334

334-
router.add("*foo", "test".to_string());
335-
router.add("/bar/*foo", "test2".to_string());
335+
#[test]
336+
fn star() {
337+
let mut router = Router::new();
336338

337-
let m = router.recognize("/test").unwrap();
338-
assert_eq!(*m.handler, "test".to_string());
339-
assert_eq!(m.params, params("foo", "test"));
339+
router.add("*foo", "test".to_string());
340+
router.add("/bar/*foo", "test2".to_string());
340341

341-
let m = router.recognize("/foo/bar").unwrap();
342-
assert_eq!(*m.handler, "test".to_string());
343-
assert_eq!(m.params, params("foo", "foo/bar"));
342+
let m = router.recognize("/test").unwrap();
343+
assert_eq!(*m.handler, "test".to_string());
344+
assert_eq!(m.params, params("foo", "test"));
344345

345-
let m = router.recognize("/bar/foo").unwrap();
346-
assert_eq!(*m.handler, "test2".to_string());
347-
assert_eq!(m.params, params("foo", "foo"));
348-
}
346+
let m = router.recognize("/foo/bar").unwrap();
347+
assert_eq!(*m.handler, "test".to_string());
348+
assert_eq!(m.params, params("foo", "foo/bar"));
349349

350-
#[test]
351-
fn unnamed_parameters() {
352-
let mut router = Router::new();
350+
let m = router.recognize("/bar/foo").unwrap();
351+
assert_eq!(*m.handler, "test2".to_string());
352+
assert_eq!(m.params, params("foo", "foo"));
353+
}
353354

354-
router.add("/foo/:/bar", "test".to_string());
355-
router.add("/foo/:bar/*", "test2".to_string());
356-
let m = router.recognize("/foo/test/bar").unwrap();
357-
assert_eq!(*m.handler, "test");
358-
assert_eq!(m.params, Params::new());
355+
#[test]
356+
fn unnamed_parameters() {
357+
let mut router = Router::new();
359358

360-
let m = router.recognize("/foo/test/blah").unwrap();
361-
assert_eq!(*m.handler, "test2");
362-
assert_eq!(m.params, params("bar", "test"));
363-
}
359+
router.add("/foo/:/bar", "test".to_string());
360+
router.add("/foo/:bar/*", "test2".to_string());
361+
let m = router.recognize("/foo/test/bar").unwrap();
362+
assert_eq!(*m.handler, "test");
363+
assert_eq!(m.params, Params::new());
364364

365-
#[allow(dead_code)]
366-
fn params(key: &str, val: &str) -> Params {
367-
let mut map = Params::new();
368-
map.insert(key.to_string(), val.to_string());
369-
map
370-
}
365+
let m = router.recognize("/foo/test/blah").unwrap();
366+
assert_eq!(*m.handler, "test2");
367+
assert_eq!(m.params, params("bar", "test"));
368+
}
371369

372-
#[allow(dead_code)]
373-
fn two_params(k1: &str, v1: &str, k2: &str, v2: &str) -> Params {
374-
let mut map = Params::new();
375-
map.insert(k1.to_string(), v1.to_string());
376-
map.insert(k2.to_string(), v2.to_string());
377-
map
370+
fn params(key: &str, val: &str) -> Params {
371+
let mut map = Params::new();
372+
map.insert(key.to_string(), val.to_string());
373+
map
374+
}
375+
376+
fn two_params(k1: &str, v1: &str, k2: &str, v2: &str) -> Params {
377+
let mut map = Params::new();
378+
map.insert(k1.to_string(), v1.to_string());
379+
map.insert(k2.to_string(), v2.to_string());
380+
map
381+
}
378382
}

0 commit comments

Comments
 (0)