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            match &token {
16                proc_macro::TokenTree::Ident(ident) => {
17                    let ident_str = ident.to_string();
18                    if ident_str == "struct" || ident_str == "enum" {
19                        found_struct_or_enum = true;
20                    } else if found_struct_or_enum {
21                        break ident.to_string();
22                    }
23                }
24                _ => {}
25            }
26        } else {
27            panic!("Could not find type name");
28        }
29    };
30
31    let output = format!(
32        "#[automatically_derived] impl ::riptc_bridge::Riptc for {} {{}}",
33        type_name
34    );
35
36    output.parse().unwrap()
37}