|
3 | 3 | use std::{cell::RefCell, rc::Rc};
|
4 | 4 |
|
5 | 5 | use quote::ToTokens;
|
| 6 | +use syn::{ItemEnum, ItemStruct}; |
6 | 7 | use uuid::Uuid;
|
7 | 8 |
|
8 | 9 | use crate::{location, Codebase, OpenState};
|
9 | 10 |
|
10 | 11 | use super::{
|
| 12 | + contract::Contract, |
| 13 | + custom_type::{CustomType, EnumType, StructType, Type}, |
11 | 14 | expression::{
|
12 | 15 | Array, Assign, BinEx, Binary, Break, Cast, ConstBlock, EBlock, Expression, FunctionCall,
|
13 | 16 | Identifier, MemberAccess, MethodCall, Reference,
|
14 | 17 | },
|
15 |
| - inner_type::Type, |
16 | 18 | statement::{Block, Statement},
|
17 | 19 | };
|
18 | 20 |
|
| 21 | +pub(crate) fn build_contract(struct_item: &ItemStruct) -> Rc<Contract> { |
| 22 | + let mut fields = Vec::new(); |
| 23 | + for field in &struct_item.fields { |
| 24 | + let field_name = match &field.ident { |
| 25 | + Some(ident) => ident.to_string(), |
| 26 | + None => "unnamed".to_string(), |
| 27 | + }; |
| 28 | + let field_type = Type::T(field.ty.to_token_stream().to_string()); |
| 29 | + fields.push((field_name, field_type)); |
| 30 | + } |
| 31 | + Rc::new(Contract { |
| 32 | + id: Uuid::new_v4().as_u128(), |
| 33 | + location: location!(struct_item), |
| 34 | + name: Contract::contract_name_from_syn_item(struct_item), |
| 35 | + fields, |
| 36 | + children: RefCell::new(Vec::new()), |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +pub(crate) fn build_struct_custom_type(struct_item: &ItemStruct) -> CustomType { |
| 41 | + let mut fields = Vec::new(); |
| 42 | + for field in &struct_item.fields { |
| 43 | + let field_name = match &field.ident { |
| 44 | + Some(ident) => ident.to_string(), |
| 45 | + None => "unnamed".to_string(), |
| 46 | + }; |
| 47 | + let field_type = Type::T(field.ty.to_token_stream().to_string()); |
| 48 | + fields.push((field_name, field_type)); |
| 49 | + } |
| 50 | + CustomType::Struct(Rc::new(StructType { |
| 51 | + id: Uuid::new_v4().as_u128(), |
| 52 | + location: location!(struct_item), |
| 53 | + name: struct_item.ident.to_string(), |
| 54 | + fields, |
| 55 | + children: RefCell::new(Vec::new()), |
| 56 | + })) |
| 57 | +} |
| 58 | + |
| 59 | +pub(crate) fn build_enum_custom_type(enum_item: &ItemEnum) -> CustomType { |
| 60 | + let mut variants = Vec::new(); |
| 61 | + for variant in &enum_item.variants { |
| 62 | + let variant_name = variant.ident.to_string(); |
| 63 | + variants.push(variant_name); |
| 64 | + } |
| 65 | + CustomType::Enum(Rc::new(EnumType { |
| 66 | + id: Uuid::new_v4().as_u128(), |
| 67 | + location: location!(enum_item), |
| 68 | + name: enum_item.ident.to_string(), |
| 69 | + variants, |
| 70 | + children: RefCell::new(Vec::new()), |
| 71 | + })) |
| 72 | +} |
| 73 | + |
19 | 74 | pub(crate) fn build_array_expression(
|
20 | 75 | codebase: &mut Codebase<OpenState>,
|
21 | 76 | array_expr: &syn::ExprArray,
|
|
0 commit comments