rocket_codegen/
exports.rs

1use proc_macro2::{Span, TokenStream};
2use quote::{ToTokens, TokenStreamExt};
3
4#[derive(Debug, Copy, Clone)]
5pub struct StaticPath(pub Option<Span>, pub &'static str);
6
7#[derive(Debug, Copy, Clone)]
8pub struct StaticTokens(pub fn() -> TokenStream);
9
10macro_rules! quote_static {
11    ($($token:tt)*) => {
12        $crate::exports::StaticTokens(|| quote!($($token)*))
13    }
14}
15
16impl ToTokens for StaticTokens {
17    fn to_tokens(&self, tokens: &mut TokenStream) {
18        tokens.append_all((self.0)());
19    }
20}
21
22impl StaticPath {
23    pub fn respanned(mut self, span: Span) -> Self {
24        self.0 = Some(span);
25        self
26    }
27}
28
29impl ToTokens for StaticPath {
30    fn to_tokens(&self, tokens: &mut TokenStream) {
31        let path: syn::Path = syn::parse_str(self.1).unwrap();
32        if let Some(span) = self.0 {
33            let new_tokens = path.into_token_stream()
34                .into_iter()
35                .map(|mut t| { t.set_span(span); t });
36
37            tokens.append_all(new_tokens);
38        } else {
39            path.to_tokens(tokens)
40        }
41    }
42}
43
44macro_rules! define_exported_paths {
45    ($($name:ident => $path:path),* $(,)?) => {
46        $(
47            #[allow(dead_code)]
48            #[allow(non_upper_case_globals)]
49            pub const $name: StaticPath = $crate::exports::StaticPath(None, stringify!($path));
50        )*
51
52        macro_rules! define {
53            // Note: the `i` is to capture the input's span.
54            $(($span:expr => $i:ident $name) => {
55                #[allow(non_snake_case)]
56                let $i = $crate::exports::StaticPath(Some($span), stringify!($path));
57            };)*
58        }
59    };
60}
61
62define_exported_paths! {
63    __req => __req,
64    __status => __status,
65    __catcher => __catcher,
66    __data => __data,
67    __error => __error,
68    __trail => __trail,
69    _request => ::rocket::request,
70    _response => ::rocket::response,
71    _route => ::rocket::route,
72    _catcher => ::rocket::catcher,
73    _sentinel => ::rocket::sentinel,
74    _log => ::rocket::log,
75    _form => ::rocket::form::prelude,
76    _http => ::rocket::http,
77    _uri => ::rocket::http::uri,
78    _fmt => ::rocket::http::uri::fmt,
79    _Option => ::std::option::Option,
80    _Result => ::std::result::Result,
81    _Some => ::std::option::Option::Some,
82    _None => ::std::option::Option::None,
83    _Ok => ::std::result::Result::Ok,
84    _Err => ::std::result::Result::Err,
85    _Box => ::std::boxed::Box,
86    _Vec => ::std::vec::Vec,
87    _Cow => ::std::borrow::Cow,
88    BorrowMut => ::std::borrow::BorrowMut,
89    Outcome => ::rocket::outcome::Outcome,
90    FromForm => ::rocket::form::FromForm,
91    FromRequest => ::rocket::request::FromRequest,
92    FromData => ::rocket::data::FromData,
93    FromSegments => ::rocket::request::FromSegments,
94    FromParam => ::rocket::request::FromParam,
95    Request => ::rocket::request::Request,
96    Response => ::rocket::response::Response,
97    Data => ::rocket::data::Data,
98    StaticRouteInfo => ::rocket::StaticRouteInfo,
99    StaticCatcherInfo => ::rocket::StaticCatcherInfo,
100    Route => ::rocket::Route,
101    Catcher => ::rocket::Catcher,
102    SmallVec => ::rocket::http::private::SmallVec,
103    Status => ::rocket::http::Status,
104}
105
106macro_rules! define_spanned_export {
107    ($span:expr => $($name:ident),*) => ($(define!($span => $name $name);)*)
108}
109
110/// Convenience: returns a "mixed site" span located at `span`.
111#[inline(always)]
112pub fn mixed(span: Span) -> Span {
113    Span::mixed_site().located_at(span)
114}