Support 'uri' macro in 2018 edition crates.

This commit is contained in:
jeb 2018-10-30 02:43:04 -07:00 committed by Sergio Benitez
parent 4ef179cc59
commit 328bf4b32e
4 changed files with 55 additions and 11 deletions

View File

@ -319,9 +319,11 @@ fn generate_internal_uri_macro(route: &Route) -> TokenStream2 {
let route_uri = route.attribute.path.origin.0.to_string(); let route_uri = route.attribute.path.origin.0.to_string();
quote! { quote! {
pub macro #generated_macro_name($($token:tt)*) { pub macro #generated_macro_name($($token:tt)*) {{
rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*) extern crate std;
} extern crate rocket;
rocket::rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*)
}}
} }
} }

View File

@ -114,12 +114,12 @@ fn explode<'a, I>(route_str: &str, items: I) -> TokenStream2
let_bindings.push(quote_spanned!(span => let_bindings.push(quote_spanned!(span =>
let #ident_tmp = #expr; let #ident_tmp = #expr;
let #ident = <#ty as ::rocket::http::uri::FromUriParam<_>>::from_uri_param(#ident_tmp); let #ident = <#ty as rocket::http::uri::FromUriParam<_>>::from_uri_param(#ident_tmp);
)); ));
// generating: arg tokens for format string // generating: arg tokens for format string
fmt_exprs.push(quote_spanned! { span => fmt_exprs.push(quote_spanned! { span =>
&#ident as &::rocket::http::uri::UriDisplay &#ident as &rocket::http::uri::UriDisplay
}); });
} }
@ -158,10 +158,11 @@ crate fn _uri_internal_macro(input: TokenStream) -> Result<TokenStream> {
let path = explode(origin.path(), arguments.by_ref().take(path_param_count)); let path = explode(origin.path(), arguments.by_ref().take(path_param_count));
let query = Optional(origin.query().map(|q| explode(q, arguments))); let query = Optional(origin.query().map(|q| explode(q, arguments)));
Ok(quote!({ let span = internal.uri_params.route_path.span();
::rocket::http::uri::Origin::new::< Ok(quote_spanned!(span => {
::std::borrow::Cow<'static, str>, rocket::http::uri::Origin::new::<
::std::borrow::Cow<'static, str>, std::borrow::Cow<'static, str>,
std::borrow::Cow<'static, str>,
>(#path, #query) >(#path, #query)
}).into()) }).into())
} }

View File

@ -1,9 +1,9 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[cfg(test)] mod tests; #[cfg(test)] mod tests;
use rocket::{get, routes};
#[get("/")] #[get("/")]
fn hello() -> &'static str { fn hello() -> &'static str {
"Hello, Rust 2018!" "Hello, Rust 2018!"

View File

@ -7,3 +7,44 @@ fn hello_world() {
let mut response = client.get("/").dispatch(); let mut response = client.get("/").dispatch();
assert_eq!(response.body_string(), Some("Hello, Rust 2018!".into())); assert_eq!(response.body_string(), Some("Hello, Rust 2018!".into()));
} }
// Tests unrelated to the example.
mod scoped_uri_tests {
use rocket::{get, routes};
mod inner {
use rocket::uri;
#[rocket::get("/")]
pub fn hello() -> String {
format!("Hello! Try {}.", uri!(super::hello_name: "Rust 2018"))
}
}
#[get("/<name>")]
fn hello_name(name: String) -> String {
format!("Hello, {}! This is {}.", name, rocket::uri!(hello_name: &name))
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![hello_name])
.mount("/", rocket::routes![inner::hello])
}
use rocket::local::Client;
#[test]
fn test_inner_hello() {
let client = Client::new(rocket()).unwrap();
let mut response = client.get("/").dispatch();
assert_eq!(response.body_string(), Some("Hello! Try /Rust%202018.".into()));
}
#[test]
fn test_hello_name() {
let client = Client::new(rocket()).unwrap();
let mut response = client.get("/Rust%202018").dispatch();
assert_eq!(response.body_string().unwrap(), "Hello, Rust 2018! This is /Rust%202018.");
}
}