Rocket

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 328bf4b32e105815bffc4f3eda7ea7d31191f3f1
parent 4ef179cc59dde4cadd37b7977cb5fe1f4a4e4ad6
Author: jeb <jeb@jebrosen.com>
Date:   Tue, 30 Oct 2018 02:43:04 -0700

Support 'uri' macro in 2018 edition crates.

Diffstat:
Mcore/codegen/src/attribute/route.rs | 8+++++---
Mcore/codegen/src/bang/uri.rs | 13+++++++------
Mexamples/hello_2018/src/main.rs | 4++--
Mexamples/hello_2018/src/tests.rs | 41+++++++++++++++++++++++++++++++++++++++++
4 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/core/codegen/src/attribute/route.rs b/core/codegen/src/attribute/route.rs @@ -319,9 +319,11 @@ fn generate_internal_uri_macro(route: &Route) -> TokenStream2 { let route_uri = route.attribute.path.origin.0.to_string(); quote! { - pub macro #generated_macro_name($($token:tt)*) { - rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*) - } + pub macro #generated_macro_name($($token:tt)*) {{ + extern crate std; + extern crate rocket; + rocket::rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*) + }} } } diff --git a/core/codegen/src/bang/uri.rs b/core/codegen/src/bang/uri.rs @@ -114,12 +114,12 @@ fn explode<'a, I>(route_str: &str, items: I) -> TokenStream2 let_bindings.push(quote_spanned!(span => 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 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 query = Optional(origin.query().map(|q| explode(q, arguments))); - Ok(quote!({ - ::rocket::http::uri::Origin::new::< - ::std::borrow::Cow<'static, str>, - ::std::borrow::Cow<'static, str>, + let span = internal.uri_params.route_path.span(); + Ok(quote_spanned!(span => { + rocket::http::uri::Origin::new::< + std::borrow::Cow<'static, str>, + std::borrow::Cow<'static, str>, >(#path, #query) }).into()) } diff --git a/examples/hello_2018/src/main.rs b/examples/hello_2018/src/main.rs @@ -1,9 +1,9 @@ #![feature(proc_macro_hygiene, decl_macro)] -#[macro_use] extern crate rocket; - #[cfg(test)] mod tests; +use rocket::{get, routes}; + #[get("/")] fn hello() -> &'static str { "Hello, Rust 2018!" diff --git a/examples/hello_2018/src/tests.rs b/examples/hello_2018/src/tests.rs @@ -7,3 +7,44 @@ fn hello_world() { let mut response = client.get("/").dispatch(); 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."); + } +}