fvm-abi 1.0.0

describe the contract struct
Documentation
use scale_info::{Registry, PortableRegistry, TypeDef};
use crate::{ABIType, PrimitiveType, TypeEnum};
use scale_info::form::PortableForm;
use crate::TypeInfo as ABITypeInfo;

pub fn generate_abi_type(reg: Registry) -> Vec<ABIType<PortableForm>> {
    let reg_portable: PortableRegistry = reg.into();
    reg_portable
        .types()
        .into_iter()
        .map(|tp| {
            // construct a ABIType
            match tp.ty().type_def() {
                TypeDef::Composite(comp) => {
                    let f = comp
                        .fields()
                        .into_iter()
                        .map(|field| {
                            ABITypeInfo {
                                type_id: *field.ty(),
                            }
                        })
                        .collect::<Vec<_>>();
                    ABIType {
                        type_id: tp.id(),
                        tp: TypeEnum::Struct,
                        fields: Some(f),
                        primitive: None,
                        array_len: None,
                        variants: None,
                    }
                }
                TypeDef::Primitive(pri) => {
                    ABIType {
                        type_id: tp.id(),
                        tp: TypeEnum::Primitive,
                        fields: None,
                        primitive: Some(PrimitiveType::to_primitive_type(pri).to_string()),
                        array_len: None,
                        variants: None,
                    }
                }
                TypeDef::Variant(items) => {
                    if items.variants().len() > 256 {
                        panic!("items of Enum have exceed 256");
                    }
                    let variant_types = items.variants().into_iter().map(|x| {
                        if x.name() != "None" && x.fields().len() == 0 {
                            panic!("Enum should point out item type");
                        }
                        x.fields().into_iter().map(|x| {
                            ABITypeInfo {
                                type_id: *x.ty()
                            }
                        }).collect::<Vec<_>>()
                    }).collect::<Vec<_>>();
                    ABIType {
                        type_id: tp.id(),
                        tp: TypeEnum::Enum,
                        fields: None,
                        primitive: None,
                        array_len: None,
                        variants: Some(variant_types),
                    }
                }
                TypeDef::Sequence(seq) => {
                    let other_info = ABITypeInfo {
                        type_id: *seq.type_param(),
                    };
                    ABIType {
                        type_id: tp.id(),
                        tp: TypeEnum::Vec,
                        fields: Some(vec![other_info]),
                        primitive: None,
                        array_len: None,
                        variants: None,
                    }
                }
                TypeDef::Array(arr) => {
                    let other_info = ABITypeInfo {
                        type_id: *arr.type_param(),
                    };
                    ABIType {
                        type_id: tp.id(),
                        tp: TypeEnum::Array,
                        fields: Some(vec![other_info]),
                        primitive: None,
                        array_len: Some(arr.len()),
                        variants: None,
                    }
                }
                TypeDef::Tuple(items) => {
                    let f = items.fields()
                        .into_iter()
                        .map(|field| {
                            ABITypeInfo {
                                type_id: *field,
                            }
                        })
                        .collect();
                    ABIType {
                        type_id: tp.id(),
                        tp: TypeEnum::Tuple,
                        fields: Some(f),
                        primitive: None,
                        array_len: None,
                        variants: None,
                    }
                }
                TypeDef::Compact(_) => {
                    panic!("not support compact type for ABI now")
                }
                TypeDef::BitSequence(_) => {
                    panic!("not support BitSequence type for ABI now")
                }
            }
        })
        .collect::<Vec<_>>()
}