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 NanoId {
92 len: usize,
93 },
94 IpAddr,
95 Struct {
96 name: String,
97 fields: Vec<Field>,
98 },
99 StructRef(String),
100 Object,
101 StructTable {
106 struct_ref: String,
107 },
108 Vec(Box<Type>),
109 Unit,
110 Optional(Box<Type>),
111 Enum {
112 name: String,
113 variants: Vec<EnumVariant>,
114 },
115 EnumRef {
116 name: String,
117 #[serde(default, skip_serializing)]
118 prefixed_name: bool,
119 },
120 TimeStampMs,
121 BlockchainDecimal,
122 BlockchainAddress,
123 BlockchainTransactionHash,
124}
125
126impl Type {
127 pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
129 Self::Struct {
130 name: name.into(),
131 fields,
132 }
133 }
134
135 pub fn struct_ref(name: impl Into<String>) -> Self {
137 Self::StructRef(name.into())
138 }
139
140 pub fn struct_table(struct_ref: impl Into<String>) -> Self {
150 Self::StructTable {
151 struct_ref: struct_ref.into(),
152 }
153 }
154
155 pub fn vec(ty: Type) -> Self {
157 Self::Vec(Box::new(ty))
158 }
159
160 pub fn optional(ty: Type) -> Self {
162 Self::Optional(Box::new(ty))
163 }
164
165 pub fn enum_ref(name: impl Into<String>, prefixed_name: bool) -> Self {
167 Self::EnumRef {
168 name: name.into(),
169 prefixed_name,
170 }
171 }
172
173 pub fn enum_(name: impl Into<String>, fields: Vec<EnumVariant>) -> Self {
175 Self::Enum {
176 name: name.into(),
177 variants: fields,
178 }
179 }
180 pub fn try_unwrap(self) -> Option<Self> {
181 match self {
182 Self::Vec(v) => Some(*v),
183 Self::StructTable { .. } => None,
185 _ => Some(self),
186 }
187 }
188
189 pub fn add_default_enum_derives(input: String) -> String {
190 format!(
191 r#"#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromPrimitive, PartialEq, Eq, PartialOrd, Ord, EnumString, Display, Hash)]{input}"#
192 )
193 }
194
195 pub fn add_default_struct_derives(input: String) -> String {
196 format!(
197 r#" #[derive(Serialize, Deserialize, Debug, Clone)]
198 #[serde(rename_all = "camelCase")]
199 {input}
200 "#
201 )
202 }
203}