1use serde::*;
2
3#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
5pub struct Field {
6 pub name: String,
8
9 #[serde(skip)]
11 pub description: String,
12
13 pub ty: Type,
15}
16
17impl Field {
18 pub fn new(name: impl Into<String>, ty: Type) -> Self {
21 Self {
22 name: name.into(),
23 description: "".into(),
24 ty,
25 }
26 }
27
28 pub fn new_with_description(
30 name: impl Into<String>,
31 description: impl Into<String>,
32 ty: Type,
33 ) -> Self {
34 Self {
35 name: name.into(),
36 description: description.into(),
37 ty,
38 }
39 }
40}
41
42#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
44pub struct EnumVariant {
45 pub name: String,
47
48 pub description: String,
50
51 pub value: i64,
53}
54
55impl EnumVariant {
56 pub fn new(name: impl Into<String>, value: i64) -> Self {
59 Self {
60 name: name.into(),
61 description: "".into(),
62 value,
63 }
64 }
65
66 pub fn new_with_description(
68 name: impl Into<String>,
69 description: impl Into<String>,
70 value: i64,
71 ) -> Self {
72 Self {
73 name: name.into(),
74 description: description.into(),
75 value,
76 }
77 }
78}
79
80#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, PartialOrd, Eq, Ord)]
82pub enum Type {
83 UInt32,
84 Int32,
85 Int64,
86 Float64,
87 Boolean,
88 String,
89 Bytea,
90 UUID,
91 IpAddr,
92 Struct {
93 name: String,
94 fields: Vec<Field>,
95 },
96 StructRef(String),
97 Object,
98 StructTable {
103 struct_ref: String,
104 },
105 Vec(Box<Type>),
106 Unit,
107 Optional(Box<Type>),
108 Enum {
109 name: String,
110 variants: Vec<EnumVariant>,
111 },
112 EnumRef {
113 name: String,
114 #[serde(default, skip_serializing)]
115 prefixed_name: bool,
116 },
117 TimeStampMs,
118 BlockchainDecimal,
119 BlockchainAddress,
120 BlockchainTransactionHash,
121}
122
123impl Type {
124 pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
126 Self::Struct {
127 name: name.into(),
128 fields,
129 }
130 }
131
132 pub fn struct_ref(name: impl Into<String>) -> Self {
134 Self::StructRef(name.into())
135 }
136
137 pub fn struct_table(struct_ref: impl Into<String>) -> Self {
147 Self::StructTable {
148 struct_ref: struct_ref.into(),
149 }
150 }
151
152 pub fn vec(ty: Type) -> Self {
154 Self::Vec(Box::new(ty))
155 }
156
157 pub fn optional(ty: Type) -> Self {
159 Self::Optional(Box::new(ty))
160 }
161
162 pub fn enum_ref(name: impl Into<String>, prefixed_name: bool) -> Self {
164 Self::EnumRef {
165 name: name.into(),
166 prefixed_name,
167 }
168 }
169
170 pub fn enum_(name: impl Into<String>, fields: Vec<EnumVariant>) -> Self {
172 Self::Enum {
173 name: name.into(),
174 variants: fields,
175 }
176 }
177 pub fn try_unwrap(self) -> Option<Self> {
178 match self {
179 Self::Vec(v) => Some(*v),
180 Self::StructTable { .. } => None,
182 _ => Some(self),
183 }
184 }
185
186 pub fn add_default_enum_derives(input: String) -> String {
187 format!(
188 r#"#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromPrimitive, PartialEq, Eq, PartialOrd, Ord, EnumString, Display, Hash)]{input}"#
189 )
190 }
191
192 pub fn add_default_struct_derives(input: String) -> String {
193 format!(
194 r#" #[derive(Serialize, Deserialize, Debug, Clone)]
195 #[serde(rename_all = "camelCase")]
196 {input}
197 "#
198 )
199 }
200}