instruct_macros_types/
lib.rs

1use serde::{Deserialize, Serialize};
2
3pub trait InstructMacro {
4    fn get_info() -> InstructMacroResult;
5    fn validate(&self) -> Result<(), String>;
6}
7
8#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
9pub enum InstructMacroResult {
10    Struct(StructInfo),
11    Enum(EnumInfo),
12}
13
14impl InstructMacroResult {
15    pub fn wrap_info(self, new_name: String) -> Parameter {
16        match self {
17            InstructMacroResult::Struct(struct_info) => struct_info.wrap_info(new_name),
18            InstructMacroResult::Enum(enum_info) => enum_info.wrap_info(new_name),
19        }
20    }
21
22    pub fn override_description(self, new_description: String) -> InstructMacroResult {
23        match self {
24            InstructMacroResult::Struct(struct_info) => {
25                InstructMacroResult::Struct(struct_info.override_description(new_description))
26            }
27            InstructMacroResult::Enum(enum_info) => {
28                InstructMacroResult::Enum(enum_info.override_description(new_description))
29            }
30        }
31    }
32
33    pub fn set_optional(self, is_optional: bool) -> InstructMacroResult {
34        match self {
35            InstructMacroResult::Struct(struct_info) => {
36                InstructMacroResult::Struct(struct_info.set_optional(is_optional))
37            }
38            InstructMacroResult::Enum(enum_info) => {
39                InstructMacroResult::Enum(enum_info.set_optional(is_optional))
40            }
41        }
42    }
43
44    pub fn set_list(self, is_list: bool) -> InstructMacroResult {
45        match self {
46            InstructMacroResult::Struct(struct_info) => {
47                InstructMacroResult::Struct(struct_info.set_list(is_list))
48            }
49            InstructMacroResult::Enum(enum_info) => {
50                InstructMacroResult::Enum(enum_info.set_list(is_list))
51            }
52        }
53    }
54}
55
56#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
57pub struct StructInfo {
58    pub name: String,
59    pub description: String,
60    pub parameters: Vec<Parameter>,
61    pub is_optional: bool,
62    pub is_list: bool,
63}
64
65impl StructInfo {
66    pub fn wrap_info(mut self, new_name: String) -> Parameter {
67        self.name = new_name;
68        Parameter::Struct(self)
69    }
70
71    pub fn override_description(mut self, new_description: String) -> StructInfo {
72        if new_description.len() > 0 {
73            self.description = new_description;
74        }
75        self
76    }
77
78    pub fn set_optional(mut self, is_optional: bool) -> StructInfo {
79        self.is_optional = is_optional;
80        self
81    }
82
83    pub fn set_list(mut self, is_list: bool) -> StructInfo {
84        self.is_list = is_list;
85        self
86    }
87}
88
89#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
90pub enum Parameter {
91    Struct(StructInfo),
92    Field(ParameterInfo),
93    Enum(EnumInfo),
94}
95
96#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
97pub struct ParameterInfo {
98    pub name: String,
99    pub r#type: String,
100    pub comment: String,
101    pub is_optional: bool,
102    pub is_list: bool,
103}
104
105#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
106pub struct EnumInfo {
107    pub title: String,
108    pub r#enum: Vec<String>,
109    pub r#type: String,
110    pub description: String,
111    pub is_optional: bool,
112    pub is_list: bool,
113}
114
115impl EnumInfo {
116    pub fn wrap_info(mut self, new_name: String) -> Parameter {
117        self.title = new_name;
118        Parameter::Enum(self)
119    }
120
121    pub fn override_description(mut self, new_description: String) -> EnumInfo {
122        if new_description.len() > 0 {
123            self.description = new_description;
124        }
125        self
126    }
127
128    pub fn set_optional(mut self, is_optional: bool) -> EnumInfo {
129        self.is_optional = is_optional;
130        self
131    }
132
133    pub fn set_list(mut self, is_list: bool) -> EnumInfo {
134        self.is_list = is_list;
135        self
136    }
137}
138
139pub struct FieldInfo {
140    pub name: String,
141    pub description: String,
142    pub r#type: String,
143    pub is_complex: bool,
144    pub is_optional: bool,
145    pub is_list: bool,
146}