Skip to content

Commit

Permalink
Tidy 'routes!' and 'catchers!' proc-macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Sep 17, 2018
1 parent 8e77961 commit 7926ffd
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 97 deletions.
1 change: 0 additions & 1 deletion core/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ const DEBUG_ENV_VAR: &str = "ROCKET_CODEGEN_DEBUG";

const PARAM_PREFIX: &str = "rocket_param_";
const ROUTE_STRUCT_PREFIX: &str = "static_rocket_route_info_for_";
const CATCH_STRUCT_PREFIX: &str = "static_rocket_catch_info_for_";
const ROUTE_FN_PREFIX: &str = "rocket_route_fn_";
const URI_INFO_MACRO_PREFIX: &str = "rocket_uri_for_";

Expand Down
1 change: 0 additions & 1 deletion core/codegen_next/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ rev = "87ad56ba"

[dev-dependencies]
rocket = { version = "0.4.0-dev", path = "../lib" }
rocket_codegen = { version = "0.4.0-dev", path = "../codegen" }
compiletest_rs = "0.3.14"
48 changes: 48 additions & 0 deletions core/codegen_next/src/bang/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;

use derive_utils::{syn, Spanned, Result};
use self::syn::{Path, punctuated::Punctuated, parse::Parser, token::Comma};
use syn_ext::{IdentExt, syn_to_diag};

fn _prefixed_vec(prefix: &str, input: TokenStream, ty: &TokenStream2) -> Result<TokenStream2> {
// Parse a comma-separated list of paths.
let mut paths = <Punctuated<Path, Comma>>::parse_terminated
.parse(input)
.map_err(syn_to_diag)?;

// Prefix the last segment in each path with `prefix`.
for path in paths.iter_mut() {
let mut last_seg = path.segments.last_mut().expect("last path segment");
last_seg.value_mut().ident = last_seg.value().ident.prepend(prefix);
}

// Return a `vec!` of the prefixed, mapped paths.
let prefixed_mapped_paths = paths.iter()
.map(|path| quote_spanned!(path.span().into() => #ty::from(&#path)));

Ok(quote!(vec![#(#prefixed_mapped_paths),*]))
}

fn prefixed_vec(prefix: &str, input: TokenStream, ty: TokenStream2) -> TokenStream {
let vec = _prefixed_vec(prefix, input, &ty)
.map_err(|diag| diag.emit())
.unwrap_or_else(|_| quote!(vec![]));

quote!({
let __vector: Vec<#ty> = #vec;
__vector
}).into()
}

pub static ROUTE_STRUCT_PREFIX: &str = "static_rocket_route_info_for_";

pub fn routes_macro(input: TokenStream) -> TokenStream {
prefixed_vec(ROUTE_STRUCT_PREFIX, input, quote!(::rocket::Route))
}

pub static CATCH_STRUCT_PREFIX: &str = "static_rocket_catch_info_for_";

pub fn catchers_macro(input: TokenStream) -> TokenStream {
prefixed_vec(CATCH_STRUCT_PREFIX, input, quote!(::rocket::Catcher))
}
24 changes: 9 additions & 15 deletions core/codegen_next/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ extern crate rocket_http;

mod derive;
mod attribute;
mod bang;
mod http_codegen;
mod syn_ext;
mod prefixing_vec;

crate use derive_utils::proc_macro2;

Expand All @@ -32,23 +32,17 @@ pub fn derive_responder(input: TokenStream) -> TokenStream {
derive::responder::derive_responder(input)
}

const ROUTE_STRUCT_PREFIX: &'static str = "static_rocket_route_info_for_";
#[proc_macro]
pub fn rocket_routes_internal(input: TokenStream) -> TokenStream {
prefixing_vec::prefixing_vec_macro(ROUTE_STRUCT_PREFIX, |path| {
quote!(::rocket::Route::from(&#path))
}, input)
#[proc_macro_attribute]
pub fn catch(args: TokenStream, input: TokenStream) -> TokenStream {
attribute::catch::catch_attribute(args, input)
}

const CATCH_STRUCT_PREFIX: &'static str = "static_rocket_catch_info_for_";
#[proc_macro]
pub fn rocket_catchers_internal(input: TokenStream) -> TokenStream {
prefixing_vec::prefixing_vec_macro(CATCH_STRUCT_PREFIX, |path| {
quote!(::rocket::Catcher::from(&#path))
}, input)
pub fn routes(input: TokenStream) -> TokenStream {
bang::routes_macro(input)
}

#[proc_macro_attribute]
pub fn catch(args: TokenStream, input: TokenStream) -> TokenStream {
attribute::catch::catch_attribute(args, input)
#[proc_macro]
pub fn catchers(input: TokenStream) -> TokenStream {
bang::catchers_macro(input)
}
44 changes: 0 additions & 44 deletions core/codegen_next/src/prefixing_vec.rs

This file was deleted.

1 change: 0 additions & 1 deletion core/codegen_next/src/syn_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ impl ReturnTypeExt for syn::ReturnType {
}
}
}

8 changes: 0 additions & 8 deletions core/codegen_next/tests/compile-fail/catchers.rs

This file was deleted.

11 changes: 0 additions & 11 deletions core/codegen_next/tests/route.rs

This file was deleted.

10 changes: 10 additions & 0 deletions core/codegen_next/tests/ui-fail/catchers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(proc_macro_non_items)]

#[macro_use] extern crate rocket;

fn main() {
let _ = catchers![a b]; //~ ERROR expected
let _ = catchers![];
let _ = catchers![a::, ]; //~ ERROR expected identifier
let _ = catchers![a::]; //~ ERROR expected identifier
}
20 changes: 20 additions & 0 deletions core/codegen_next/tests/ui-fail/catchers.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: expected `,`
--> $DIR/catchers.rs:6:25
|
6 | let _ = catchers![a b]; //~ ERROR expected
| ^

error: expected identifier
--> $DIR/catchers.rs:8:26
|
8 | let _ = catchers![a::, ]; //~ ERROR expected identifier
| ^

error: unexpected end of input, expected identifier
--> $DIR/catchers.rs:9:13
|
9 | let _ = catchers![a::]; //~ ERROR expected identifier
| ^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

8 changes: 5 additions & 3 deletions core/codegen_next/tests/ui-fail/routes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![feature(plugin, decl_macro, proc_macro_non_items)]
#![feature(proc_macro_non_items)]

#[macro_use] extern crate rocket;

fn main() {
let _ = routes![a b];
//~^ ERROR expected
let _ = routes![a b]; //~ ERROR expected `,`
let _ = routes![];
let _ = routes![a::, ]; //~ ERROR expected identifier
let _ = routes![a::]; //~ ERROR expected identifier
}
18 changes: 15 additions & 3 deletions core/codegen_next/tests/ui-fail/routes.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
error: expected `,` or `::` or end of macro invocation
error: expected `,`
--> $DIR/routes.rs:6:23
|
6 | let _ = routes![a b];
6 | let _ = routes![a b]; //~ ERROR expected `,`
| ^

error: aborting due to previous error
error: expected identifier
--> $DIR/routes.rs:8:24
|
8 | let _ = routes![a::, ]; //~ ERROR expected identifier
| ^

error: unexpected end of input, expected identifier
--> $DIR/routes.rs:9:13
|
9 | let _ = routes![a::]; //~ ERROR expected identifier
| ^^^^^^^^^^^^

error: aborting due to 3 previous errors

10 changes: 0 additions & 10 deletions core/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@
#[allow(unused_imports)] #[macro_use] extern crate rocket_codegen_next;
#[doc(hidden)] pub use rocket_codegen_next::*;

#[macro_export]
macro_rules! routes {
($($input:tt)*) => { $crate::rocket_routes_internal![$($input)*] };
}

#[macro_export]
macro_rules! catchers {
($($input:tt)*) => { $crate::rocket_catchers_internal![$($input)*] };
}

extern crate rocket_http;
#[macro_use] extern crate log;
#[macro_use] extern crate pear;
Expand Down

0 comments on commit 7926ffd

Please sign in to comment.