cxxbridge_macro/
type_id.rs1use crate::syntax::qualified::QualifiedName;
2use proc_macro2::{TokenStream, TokenTree};
3use quote::{format_ident, quote, ToTokens};
4use syn::ext::IdentExt;
5
6pub(crate) enum Crate {
7 Cxx,
8 DollarCrate(TokenTree),
9}
10
11impl ToTokens for Crate {
12 fn to_tokens(&self, tokens: &mut TokenStream) {
13 match self {
14 Crate::Cxx => tokens.extend(quote!(::cxx)),
15 Crate::DollarCrate(krate) => krate.to_tokens(tokens),
16 }
17 }
18}
19
20pub(crate) fn expand(krate: Crate, arg: QualifiedName) -> TokenStream {
22 let mut ids = Vec::new();
23
24 for word in arg.segments {
25 if !ids.is_empty() {
26 ids.push(quote!(()));
27 }
28 for ch in word.unraw().to_string().chars() {
29 ids.push(match ch {
30 'A'..='Z' | 'a'..='z' => {
31 let t = format_ident!("{}", ch);
32 quote!(#krate::#t)
33 }
34 '0'..='9' | '_' => {
35 let t = format_ident!("_{}", ch);
36 quote!(#krate::#t)
37 }
38 _ => quote!([(); #ch as _]),
39 });
40 }
41 }
42
43 quote! { (#(#ids,)*) }
44}