mirror of https://github.com/rwf2/Rocket.git
Allow any 'T: Into<Vec<Route>>' to be mounted.
This commit is contained in:
parent
29c9cffdbe
commit
4010a967a6
|
@ -38,24 +38,13 @@ pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>;
|
||||||
/// Such a handler might be written and used as follows:
|
/// Such a handler might be written and used as follows:
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # #[derive(Copy, Clone)]
|
/// # #[derive(Copy, Clone)] enum Kind { Simple, Intermediate, Complex, }
|
||||||
/// # enum Kind {
|
|
||||||
/// # Simple,
|
|
||||||
/// # Intermediate,
|
|
||||||
/// # Complex,
|
|
||||||
/// # }
|
|
||||||
/// use rocket::{Request, Data, Route, http::Method};
|
/// use rocket::{Request, Data, Route, http::Method};
|
||||||
/// use rocket::handler::{self, Handler, Outcome};
|
/// use rocket::handler::{self, Handler, Outcome};
|
||||||
///
|
///
|
||||||
/// #[derive(Clone)]
|
/// #[derive(Clone)]
|
||||||
/// struct CustomHandler(Kind);
|
/// struct CustomHandler(Kind);
|
||||||
///
|
///
|
||||||
/// impl CustomHandler {
|
|
||||||
/// pub fn new(kind: Kind) -> Vec<Route> {
|
|
||||||
/// vec![Route::new(Method::Get, "/", CustomHandler(kind))]
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// impl Handler for CustomHandler {
|
/// impl Handler for CustomHandler {
|
||||||
/// fn handle<'r>(&self, req: &'r Request, data: Data) -> Outcome<'r> {
|
/// fn handle<'r>(&self, req: &'r Request, data: Data) -> Outcome<'r> {
|
||||||
/// match self.0 {
|
/// match self.0 {
|
||||||
|
@ -66,10 +55,16 @@ pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>;
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
/// impl Into<Vec<Route>> for CustomHandler {
|
||||||
|
/// fn into(self) -> Vec<Route> {
|
||||||
|
/// vec![Route::new(Method::Get, "/", self)]
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// # if false {
|
/// # if false {
|
||||||
/// rocket::ignite()
|
/// rocket::ignite()
|
||||||
/// .mount("/", CustomHandler::new(Kind::Simple))
|
/// .mount("/", CustomHandler(Kind::Simple))
|
||||||
/// .launch();
|
/// .launch();
|
||||||
/// # }
|
/// # }
|
||||||
/// }
|
/// }
|
||||||
|
@ -81,8 +76,8 @@ pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>;
|
||||||
/// `CustomHandler` implements `Cloneable` automatically. The `Cloneable`
|
/// `CustomHandler` implements `Cloneable` automatically. The `Cloneable`
|
||||||
/// trait serves no other purpose but to ensure that every `Handler` can be
|
/// trait serves no other purpose but to ensure that every `Handler` can be
|
||||||
/// cloned, allowing `Route`s to be cloned.
|
/// cloned, allowing `Route`s to be cloned.
|
||||||
/// 2. The `CustomHandler::new()` method returns a vector of routes so that
|
/// 2. `CustomHandler` implements `Into<Vec<Route>>`, allowing an instance to
|
||||||
/// the user can trivially mount the handler.
|
/// be used directly as the second parameter to `rocket.mount()`.
|
||||||
/// 3. Unlike static-function-based handlers, this custom handler can make use
|
/// 3. Unlike static-function-based handlers, this custom handler can make use
|
||||||
/// of any internal state.
|
/// of any internal state.
|
||||||
///
|
///
|
||||||
|
|
|
@ -492,7 +492,7 @@ impl Rocket {
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn mount(mut self, base: &str, routes: Vec<Route>) -> Self {
|
pub fn mount<R: Into<Vec<Route>>>(mut self, base: &str, routes: R) -> Self {
|
||||||
info!("{}{} '{}':",
|
info!("{}{} '{}':",
|
||||||
Paint::masked("🛰 "),
|
Paint::masked("🛰 "),
|
||||||
Paint::purple("Mounting"),
|
Paint::purple("Mounting"),
|
||||||
|
@ -520,7 +520,7 @@ impl Rocket {
|
||||||
panic!("Invalid mount point.");
|
panic!("Invalid mount point.");
|
||||||
}
|
}
|
||||||
|
|
||||||
for mut route in routes {
|
for mut route in routes.into() {
|
||||||
let complete_uri = format!("{}/{}", base_uri, route.uri);
|
let complete_uri = format!("{}/{}", base_uri, route.uri);
|
||||||
let uri = Origin::parse_route(&complete_uri)
|
let uri = Origin::parse_route(&complete_uri)
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
|
|
Loading…
Reference in New Issue