• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::syntax::qualified::QualifiedName;
2 use proc_macro2::{TokenStream, TokenTree};
3 use quote::{format_ident, quote, ToTokens};
4 use syn::ext::IdentExt;
5 
6 pub enum Crate {
7     Cxx,
8     DollarCrate(TokenTree),
9 }
10 
11 impl ToTokens for Crate {
to_tokens(&self, tokens: &mut TokenStream)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 
20 // "folly::File" => `(f, o, l, l, y, (), F, i, l, e)`
expand(krate: Crate, arg: QualifiedName) -> TokenStream21 pub 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 }
45