1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! # Pathmaker
//!
//! Generalized routing for any HTTP library. Not too fancy.
//!
//! ## Usage
//!
//! To use this library, add the following to your `Cargo.toml`:
//!
//! ```toml
//! pathmaker = "0.1.0"
//! ```
//!
//! If you want to use it with a specific HTTP library, e.g. `hyper`, enable that
//! feature:
//!
//! ```toml
//! hyper = "0.12"
//! pathmaker = { version = "0.1", features = ["hyper"] }
//! ```
//!
//! Then, in your code, construct a router:
//!
//! ```rust
//! extern crate hyper;
//! extern crate pathmaker;
//! extern crate failure;
//! extern crate futures;
//!
//! use hyper::{Request, Response, Method, Body, Server};
//! use hyper::service::make_service_fn;
//! use hyper::header::CONTENT_LENGTH;
//! use pathmaker::hyper::Router;
//! use failure::Error;
//! use futures::prelude::*;
//!
//! fn router() -> Router {
//! let mut build = Router::build();
//! build.get("/foo", |_, _| {
//! let body = "Hello, world!";
//! Box::new(futures::future::result(Response::builder()
//! .header(CONTENT_LENGTH, body.len() as u64)
//! .body(Body::from(body))
//! .map_err(Error::from)
//! ))
//! });
//! build.finish()
//! }
//!
//! fn main() {
//! let address = "0.0.0.0:8080".parse().unwrap();
//! let server = Server::bind(&address)
//! .serve(make_service_fn(|_| Ok::<_, hyper::Error>(router()))).map_err(|e| {
//! eprintln!("error: {:?}", e);
//! });
//! // hyper::rt::run(server)
//! }
//! ```
//!
//! ## Query Parameters
//!
//! Support for query parameters is allowed by using `{}` in the path:
//!
//! ```rust
//! // ...
//! # use pathmaker::hyper::Router;
//! # use futures::prelude::*;
//! # use hyper::{Request, Response, Body};
//! # use hyper::header::CONTENT_LENGTH;
//! # use failure::Error;
//! # fn handler(_: Request<Body>, _: Vec<String>) -> Box<dyn Future<Item = Response<Body>, Error = Error> + Send> {
//! # let body = "Hello, world!";
//! # Box::new(futures::future::result(Response::builder()
//! # .header(CONTENT_LENGTH, body.len() as u64).body(Body::from(body))
//! # .map_err(Error::from)))
//! # }
//! # fn hello_handler(a: Request<Body>, b: Vec<String>) -> Box<dyn Future<Item = Response<Body>, Error = Error> + Send> { handler(a, b) }
//! fn router() -> Router {
//! let mut build = Router::build();
//! build.get("/foo", handler)
//! .get("/hello/{}", hello_handler);
//! build.finish()
//! }
//! // ...
//! ```
//!
//! Then, in the handler, you can access the first element of the second argument
//! in order to get the result:
//!
//! ```rust
//! # use hyper::{Request, Response, Body};
//! # use failure::Error;
//! # use futures::prelude::*;
//! # use hyper::header::CONTENT_LENGTH;
//! //...
//! fn hello_handler(_: Request<Body>, params: Vec<String>) -> Box<dyn Future<Item = Response<Body>, Error = Error> + Send> {
//! let body = format!("Hello, {}!", params[0]);
//! Box::new(futures::future::result(
//! Response::builder()
//! .header(CONTENT_LENGTH, body.len() as u64)
//! .body(Body::from(body))
//! .map_err(Error::from)
//! ))
//! }
//! // ...
//! ```
//!
//! Query parameters can be filtered down by format:
//!
//! - `{}`, `{:string}` (the default): anything that isn't a `/` character is
//! matched.
//! - `{:int}`: a positive or negative number.
//! - `{:uint}`: just a number, no sign allowed.
//! - `{:uuid}`: a UUID, in 8-4-4-4-12 format.
//!
//! More can be added if requested.
//!
//! ## Route Evaluation
//!
//! Routes are evaluated from top to bottom. The first route that matches is used.
extern crate failure;
extern crate test;
pub use *;