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