Rocket/core/codegen/src/bang/mod.rs

80 lines
2.4 KiB
Rust
Raw Normal View History

2019-09-21 20:36:57 +00:00
use devise::Result;
2019-09-21 20:36:57 +00:00
use crate::syn_ext::IdentExt;
use crate::syn::{Path, punctuated::Punctuated, parse::Parser, Token};
use crate::syn::spanned::Spanned;
use crate::proc_macro2::TokenStream;
2019-06-13 01:59:25 +00:00
use crate::{ROUTE_STRUCT_PREFIX, CATCH_STRUCT_PREFIX};
2018-09-20 04:47:58 +00:00
mod uri;
mod uri_parsing;
mod test_guide;
2018-09-20 04:47:58 +00:00
pub fn prefix_last_segment(path: &mut Path, prefix: &str) {
let mut last_seg = path.segments.last_mut().expect("syn::Path has segments");
last_seg.ident = last_seg.ident.prepend(prefix);
}
fn _prefixed_vec(
prefix: &str,
2019-09-21 20:36:57 +00:00
input: proc_macro::TokenStream,
ty: &TokenStream
) -> Result<TokenStream> {
// Parse a comma-separated list of paths.
2019-09-21 20:36:57 +00:00
let mut paths = <Punctuated<Path, Token![,]>>::parse_terminated.parse(input)?;
// Prefix the last segment in each path with `prefix`.
paths.iter_mut().for_each(|p| prefix_last_segment(p, 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),*]))
}
2019-09-21 20:36:57 +00:00
fn prefixed_vec(
prefix: &str,
input: proc_macro::TokenStream,
ty: TokenStream
) -> TokenStream {
define_vars_and_mods!(_Vec);
_prefixed_vec(prefix, input, &ty)
.map(|vec| quote!({
let __vector: #_Vec<#ty> = #vec;
__vector
}))
.unwrap_or_else(|diag| {
let diag_tokens = diag.emit_as_expr_tokens();
2019-09-21 20:36:57 +00:00
quote!({
#diag_tokens
let __vec: #_Vec<#ty> = vec![];
__vec
})
})
}
2019-09-21 20:36:57 +00:00
pub fn routes_macro(input: proc_macro::TokenStream) -> TokenStream {
prefixed_vec(ROUTE_STRUCT_PREFIX, input, quote!(::rocket::Route))
}
2019-09-21 20:36:57 +00:00
pub fn catchers_macro(input: proc_macro::TokenStream) -> TokenStream {
prefixed_vec(CATCH_STRUCT_PREFIX, input, quote!(::rocket::Catcher))
}
2018-09-20 04:47:58 +00:00
2019-09-21 20:36:57 +00:00
pub fn uri_macro(input: proc_macro::TokenStream) -> TokenStream {
uri::_uri_macro(input.into())
.unwrap_or_else(|diag| diag.emit_as_expr_tokens())
2018-09-20 04:47:58 +00:00
}
2019-09-21 20:36:57 +00:00
pub fn uri_internal_macro(input: proc_macro::TokenStream) -> TokenStream {
let tokens = uri::_uri_internal_macro(input.into())
.unwrap_or_else(|diag| diag.emit_as_expr_tokens());
tokens
2018-09-20 04:47:58 +00:00
}
2019-09-21 20:36:57 +00:00
pub fn guide_tests_internal(input: proc_macro::TokenStream) -> TokenStream {
test_guide::_macro(input)
.unwrap_or_else(|diag| diag.emit_as_item_tokens())
}