Skip to content

Commit faae153

Browse files
committed
Bump to 0.6.0
1 parent 17a13b8 commit faae153

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22

33
name = "conduit-router"
4-
version = "0.5.4"
4+
version = "0.6.0"
55
authors = ["[email protected]"]
66
license = "MIT"
77
description = "Router middleware for conduit based on route-recognizer"
88
repository = "https://github.com/conduit-rust/conduit-router"
99

1010
[dependencies]
11-
conduit = "0.5.0"
11+
conduit = "0.6"
1212
route-recognizer = "0.1.0"

src/lib.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate "route-recognizer" as router;
22
extern crate conduit;
33

44
use std::collections::hash_map::{HashMap, Entry};
5-
use std::fmt::Show;
5+
use std::error::Error;
66

77
use router::{Router, Match};
88
use conduit::{Method, Handler, Request, Response};
@@ -11,28 +11,31 @@ pub struct RouteBuilder {
1111
routers: HashMap<Method, Router<Box<Handler + Send + Sync>>>
1212
}
1313

14+
pub struct RouterError(String);
15+
1416
impl RouteBuilder {
1517
pub fn new() -> RouteBuilder {
1618
RouteBuilder { routers: HashMap::new() }
1719
}
1820

1921
pub fn recognize<'a>(&'a self, method: &Method, path: &str)
20-
-> Result<Match<&'a Box<Handler + Send + Sync>>, String>
22+
-> Result<Match<&'a Box<Handler + Send + Sync>>,
23+
RouterError>
2124
{
2225
match self.routers.get(method) {
23-
None => Err(format!("No router found for {}", method)),
24-
Some(router) => router.recognize(path)
25-
}
26+
Some(router) => router.recognize(path),
27+
None => Err(format!("No router found for {:?}", method)),
28+
}.map_err(RouterError)
2629
}
2730

2831
pub fn map<'a, H: Handler>(&'a mut self, method: Method, pattern: &str,
2932
handler: H) -> &'a mut RouteBuilder {
3033
{
31-
let router = match self.routers.entry(&method) {
34+
let router = match self.routers.entry(method) {
3235
Entry::Occupied(e) => e.into_mut(),
3336
Entry::Vacant(e) => e.insert(Router::new()),
3437
};
35-
router.add(pattern, box handler as Box<Handler + Send + Sync>);
38+
router.add(pattern, Box::new(handler));
3639
}
3740
self
3841
}
@@ -64,14 +67,14 @@ impl RouteBuilder {
6467
}
6568

6669
impl conduit::Handler for RouteBuilder {
67-
fn call(&self, request: &mut Request) -> Result<Response, Box<Show + 'static>> {
70+
fn call(&self, request: &mut Request) -> Result<Response, Box<Error>> {
6871
let m = {
6972
let method = request.method();
7073
let path = request.path();
7174

7275
match self.recognize(&method, path) {
7376
Ok(m) => m,
74-
Err(e) => return Err(box e as Box<Show>)
77+
Err(e) => return Err(Box::new(e) as Box<Error>)
7578
}
7679
};
7780

@@ -84,6 +87,10 @@ impl conduit::Handler for RouteBuilder {
8487
}
8588
}
8689

90+
impl Error for RouterError {
91+
fn description(&self) -> &str { self.0.as_slice() }
92+
}
93+
8794
pub trait RequestParams<'a> {
8895
fn params(self) -> &'a router::Params;
8996
}
@@ -102,9 +109,10 @@ impl<'a> RequestParams<'a> for &'a (Request + 'a) {
102109
#[cfg(test)]
103110
mod tests {
104111
extern crate semver;
105-
use std::io::net::ip::IpAddr;
106112
use std::collections::HashMap;
113+
use std::error::Error;
107114
use std::io::MemReader;
115+
use std::io::net::ip::IpAddr;
108116

109117
use {RouteBuilder, RequestParams};
110118

@@ -139,7 +147,7 @@ mod tests {
139147
}
140148
fn query_string<'a>(&'a self) -> Option<&'a str> { unimplemented!() }
141149
fn remote_ip(&self) -> IpAddr { unimplemented!() }
142-
fn content_length(&self) -> Option<uint> { unimplemented!() }
150+
fn content_length(&self) -> Option<u64> { unimplemented!() }
143151
fn headers<'a>(&'a self) -> &'a Headers { unimplemented!() }
144152
fn body<'a>(&'a mut self) -> &'a mut Reader { unimplemented!() }
145153
fn extensions<'a>(&'a self) -> &'a Extensions {
@@ -184,15 +192,16 @@ mod tests {
184192
router
185193
}
186194

187-
fn test_handler(req: &mut conduit::Request) -> Result<conduit::Response, ()> {
195+
fn test_handler(req: &mut conduit::Request)
196+
-> Result<conduit::Response, Box<Error>> {
188197
let mut res = vec!();
189198
res.push(req.params()["id"].clone());
190-
res.push(format!("{}", req.method()));
199+
res.push(format!("{:?}", req.method()));
191200

192201
Ok(conduit::Response {
193202
status: (200, "OK"),
194203
headers: HashMap::new(),
195-
body: box MemReader::new(res.connect(", ").into_bytes())
204+
body: Box::new(MemReader::new(res.connect(", ").into_bytes()))
196205
})
197206
}
198207
}

0 commit comments

Comments
 (0)