1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#![allow(unused_imports)]
#![allow(unused_variables)]
extern crate syn;

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::visit::{self, Visit};
use syn::{DataStruct, File, GenericArgument, ItemFn, ItemStruct, Token};

struct StructVisitor {
    pub struct_defs: Vec<StructDef>,
}

impl Default for StructVisitor {
    fn default() -> Self {
        StructVisitor {
            struct_defs: Vec::new(),
        }
    }
}

impl<'ast> Visit<'ast> for StructVisitor {
    fn visit_item_struct(&mut self, node: &'ast ItemStruct) {
        let struct_name = node.ident.to_owned();
        let mut struct_fields: Vec<StructField> = Vec::new();

        let fields = node.fields.clone();
        match fields {
            syn::Fields::Named(names) => {
                let names = names.named;
                for field in names.iter() {
                    let field_name = field.ident.to_owned().unwrap().to_string();
                    match &field.ty {
                        syn::Type::Path(path) => {
                            let path = path.path.clone();
                            let mut segs = path.segments.into_iter();
                            let field_ty = segs.next().unwrap();
                            let field_ty_ident = field_ty.ident.to_owned().to_string();

                            if field_ty_ident == "Vec" {
                                let inner_ty = field_ty.arguments.to_owned();
                                match inner_ty {
                                    syn::PathArguments::AngleBracketed(args) => {
                                        let generic = args.args.last().unwrap();
                                        if let GenericArgument::Type(ty) = generic {
                                            match ty {
                                                syn::Type::Path(path) => {
                                                    let path = path.path.to_owned();
                                                    let mut segs = path.segments.into_iter();
                                                    let inner_ty = segs.next().unwrap();
                                                    let inner_ty_ident =
                                                        inner_ty.ident.to_owned().to_string();

                                                    struct_fields.push(StructField {
                                                        name: field_name,
                                                        ty: Type::Vec(Box::new(Type::Atom(
                                                            inner_ty_ident,
                                                        ))),
                                                    })
                                                }
                                                _ => panic!("The generic type is illegal!"),
                                            }
                                        } else {
                                            panic!("The generic is not a type!")
                                        }
                                    }
                                    _ => panic!("Not providing the Vec generic!"),
                                }
                            } else {
                                struct_fields.push(StructField {
                                    name: field_name,
                                    ty: Type::Atom(field_ty_ident),
                                });
                            }
                        }
                        _ => panic!("The field type is illegal!"),
                    }
                }
            }
            _ => (),
        };

        self.struct_defs.push(StructDef {
            name: struct_name.to_string(),
            fields: struct_fields,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
    Vec(Box<Type>),
    Atom(String),
}

#[derive(Debug, PartialEq, Eq)]
pub struct StructField {
    pub name: String,
    pub ty: Type,
}

#[derive(Debug, PartialEq, Eq)]
pub struct StructDef {
    pub name: String,
    pub fields: Vec<StructField>,
}

pub fn get_struct_defs_from_file(file: &File) -> Vec<StructDef> {
    let mut visitor = StructVisitor::default();
    visitor.visit_file(file);
    visitor.struct_defs
}

pub fn get_struct_defs(token_stream: TokenStream) -> Vec<StructDef> {
    let file: File = syn::parse2(token_stream).unwrap();
    get_struct_defs_from_file(&file)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_struct_defs_one() {
        let code = quote! {
            struct Person {
                name: String,
                age: u8
            }
        };

        let struct_defs = get_struct_defs(code);
        let struct_def = struct_defs.first().unwrap();
        assert_eq!(
            *struct_def,
            StructDef {
                name: "Person".to_string(),
                fields: vec![
                    StructField {
                        name: "name".to_string(),
                        ty: Type::Atom("String".to_string()),
                    },
                    StructField {
                        name: "age".to_string(),
                        ty: Type::Atom("u8".to_string()),
                    }
                ]
            }
        )
    }

    #[test]
    fn get_struct_defs_multiple() {
        let code = quote! {
            struct Person {
                name: String,
                age: u8
            }

            struct Animal {
                alive: bool
            }
        };

        let struct_defs = get_struct_defs(code);

        assert_eq!(
            struct_defs,
            vec![
                StructDef {
                    name: "Person".to_string(),
                    fields: vec![
                        StructField {
                            name: "name".to_string(),
                            ty: Type::Atom("String".to_string()),
                        },
                        StructField {
                            name: "age".to_string(),
                            ty: Type::Atom("u8".to_string()),
                        }
                    ]
                },
                StructDef {
                    name: "Animal".to_string(),
                    fields: vec![StructField {
                        name: "alive".to_string(),
                        ty: Type::Atom("bool".to_string()),
                    }]
                }
            ]
        )
    }

    #[test]
    fn get_struct_def_vec() {
        let code = quote! {
            struct Student {
                marks: Vec<u32>
            }
        };

        let struct_defs = get_struct_defs(code);

        assert_eq!(
            struct_defs,
            vec![StructDef {
                name: "Student".to_string(),
                fields: vec![StructField {
                    name: "marks".to_string(),
                    ty: Type::Vec(Box::new(Type::Atom("u32".to_string())))
                }]
            }]
        )
    }
}