static_graph/codegen/
ty.rs

1use std::sync::Arc;
2
3use quote::{format_ident, quote, ToTokens};
4
5use crate::symbol::Ident;
6
7pub enum CodegenTy {
8    String,
9    Void,
10    U8,
11    Bool,
12    Bytes,
13    I8,
14    I16,
15    I32,
16    I64,
17    F32,
18    F64,
19    Vec(Arc<CodegenTy>),
20    Set(Arc<CodegenTy>),
21    Map(Arc<CodegenTy>, Arc<CodegenTy>),
22    ArcSwap(Arc<CodegenTy>),
23    Adt(Adt),
24}
25
26pub struct Adt {
27    pub segments: Arc<[Ident]>,
28}
29
30impl ToTokens for CodegenTy {
31    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
32        match self {
33            CodegenTy::String => tokens.extend(quote! { ::std::string::String }),
34            CodegenTy::Void => tokens.extend(quote! { () }),
35            CodegenTy::U8 => tokens.extend(quote! { u8 }),
36            CodegenTy::Bool => tokens.extend(quote! { bool }),
37            CodegenTy::Bytes => tokens.extend(quote! { ::bytes::Bytes }),
38            CodegenTy::I8 => tokens.extend(quote! { i8 }),
39            CodegenTy::I16 => tokens.extend(quote! { i16 }),
40            CodegenTy::I32 => tokens.extend(quote! { i32 }),
41            CodegenTy::I64 => tokens.extend(quote! { i64 }),
42            CodegenTy::F64 => tokens.extend(quote! { f64 }),
43            CodegenTy::F32 => tokens.extend(quote! { f32 }),
44            CodegenTy::Vec(ty) => {
45                let ty = &**ty;
46                tokens.extend(quote! { ::std::vec::Vec<#ty> });
47            }
48            CodegenTy::Set(ty) => {
49                let ty = &**ty;
50                tokens.extend(quote! { ::std::collections::HashSet<#ty> });
51            }
52            CodegenTy::Map(k, v) => {
53                let k = &**k;
54                let v = &**v;
55                tokens.extend(quote! { ::std::collections::HashMap<#k, #v> });
56            }
57            CodegenTy::ArcSwap(ty) => {
58                let ty = &**ty;
59                tokens.extend(quote! { ::static_graph::ArcSwap<#ty> });
60            }
61            CodegenTy::Adt(adt) => {
62                let adt: Vec<_> = adt
63                    .segments
64                    .iter()
65                    .map(|ident| format_ident!("{}", ident))
66                    .collect();
67
68                tokens.extend(quote! { #(#adt)::* });
69            }
70        }
71    }
72}