riptc_bridge_macros/
lib.rs

1use proc_macro::TokenStream;
2
3/// Indicates to riptc that a type should be generated and shared despite not being
4/// a direct dependency of a route that we have detected.
5///
6/// This macro in and of itself doesn't do anything, it simply regurgitates your code back out.
7#[proc_macro_derive(Riptc)]
8pub fn riptc_marker(input: TokenStream) -> TokenStream {
9    // we just do this by hand here because we are simply implementing a marker
10    // trait and we don't want to bring in syn / quote for no reason and bloat compile times
11    let mut tokens = input.into_iter().peekable();
12    let mut found_struct_or_enum = false;
13    let type_name = loop {
14        if let Some(token) = tokens.next() {
15            if let proc_macro::TokenTree::Ident(ident) = &token {
16                let ident_str = ident.to_string();
17                if ident_str == "struct" || ident_str == "enum" {
18                    found_struct_or_enum = true;
19                } else if found_struct_or_enum {
20                    break ident.to_string();
21                }
22            }
23        } else {
24            panic!("Could not find type name");
25        }
26    };
27
28    let output = format!(
29        "#[automatically_derived] impl ::riptc_bridge::Riptc for {} {{}}",
30        type_name
31    );
32
33    output.parse().unwrap()
34}