massbit_sol/
schema.rs

1use crate::generator::helper::replace_invalid_identifier_chars;
2use inflector::Inflector;
3use std::collections::BTreeMap;
4pub type PositiveInteger = i64;
5pub type PositiveIntegerDefault0 = serde_json::Value;
6pub type SchemaArray = Vec<Schema>;
7pub type VariantArray = Vec<Variant>;
8pub type PropertyArray = Vec<Property>;
9#[derive(Clone, Debug, Deserialize, Serialize)]
10pub struct Variant {
11    pub name: String,
12    pub value: Option<i32>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    #[serde(rename = "innerName")]
15    pub inner_name: Option<String>,
16    #[serde(rename = "innerType")]
17    pub inner_type: Option<String>,
18    #[serde(rename = "innerScope")]
19    pub inner_scope: Option<String>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub description: Option<String>,
22    //For unpacking
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub offset: Option<u16>,
25    #[serde(rename = "variantTag")]
26    pub variant_tag: u32,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub accounts: Option<Vec<AccountInfo>>,
29}
30
31impl Variant {
32    pub fn get_size(&self) -> Option<usize> {
33        self.inner_type.as_ref().and_then(|typ| match typ.as_str() {
34            "i8" | "u8" => Some(1),
35            "i16" | "u16" => Some(2),
36            "i32" | "u32" => Some(4),
37            "i64" | "u64" => Some(8),
38            "i128" | "u128" => Some(16),
39            _type_name => None,
40        })
41    }
42}
43#[derive(Clone, Debug, Deserialize, Serialize)]
44pub struct AccountInfo {
45    pub index: usize,
46    pub name: String,
47}
48#[derive(Clone, Debug, Deserialize, Serialize)]
49pub struct Property {
50    pub name: String,
51    #[serde(rename = "dataType")]
52    pub data_type: String,
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub length: Option<usize>,
55    #[serde(rename = "arrayLength")]
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub array_length: Option<usize>,
58    pub required: bool,
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub description: Option<String>,
61}
62impl Property {
63    pub fn get_size(&self, definitions: &BTreeMap<String, Schema>) -> usize {
64        match self.length {
65            None => match self.data_type.as_str() {
66                "u8" | "i8" => 1_usize,
67                "u16" | "i16" => 2_usize,
68                "u32" | "i32" => 4_usize,
69                "u64" | "i64" => 8_usize,
70                "u128" | "i128" => 16_usize,
71                type_name => definitions
72                    .get(type_name)
73                    .and_then(|def_schema| def_schema.get_size(definitions))
74                    .unwrap_or(1usize),
75            },
76            Some(len) => len,
77        }
78    }
79}
80#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
81#[serde(rename = "variantTag")]
82pub enum VariantTag {
83    #[serde(rename = "u8")]
84    U8(u8),
85    #[serde(rename = "u16")]
86    U16(u16),
87    #[serde(rename = "u32")]
88    U32(u32),
89    #[serde(rename = "u64")]
90    U64(u64),
91}
92
93#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
94#[serde(rename = "simpleTypes")]
95pub enum SimpleTypes {
96    #[serde(rename = "array")]
97    Array,
98    #[serde(rename = "boolean")]
99    Boolean,
100    #[serde(rename = "integer")]
101    Integer,
102    #[serde(rename = "null")]
103    Null,
104    #[serde(rename = "number")]
105    Number,
106    #[serde(rename = "object")]
107    Object,
108    #[serde(rename = "string")]
109    String,
110}
111pub type StringArray = Vec<String>;
112#[derive(Clone, Debug, Deserialize, Serialize, Default)]
113pub struct Schema {
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub name: Option<String>,
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub variants: Option<VariantArray>,
118    #[serde(skip_serializing_if = "Option::is_none")]
119    #[serde(default)]
120    pub properties: Option<PropertyArray>,
121    #[serde(default)]
122    pub definitions: ::std::collections::BTreeMap<String, Schema>,
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub description: Option<String>,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub offset: Option<usize>,
127    #[serde(skip_serializing_if = "Option::is_none", rename = "variantTagLength")]
128    pub variant_tag_length: Option<usize>,
129}
130
131impl Schema {
132    pub fn get_pascal_name(&self, name: &String) -> String {
133        replace_invalid_identifier_chars(&name.as_str().to_pascal_case())
134    }
135    //Get size of struct type
136    pub fn get_size(&self, definitions: &BTreeMap<String, Schema>) -> Option<usize> {
137        self.properties.as_ref().and_then(|properties| {
138            let mut size = 0usize;
139            for property in properties {
140                size = size + property.get_size(definitions);
141            }
142            Some(size)
143        })
144    }
145}