forked from rwf2/Rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy 'routes!' and 'catchers!' proc-macros.
- Loading branch information
1 parent
8e77961
commit 7926ffd
Showing
13 changed files
with
107 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,3 @@ impl ReturnTypeExt for syn::ReturnType { | |
} | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters