endpoint_libs/model/
types.rs1use serde::*;
2
3#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
5pub struct Field {
6 pub name: String,
8
9 pub ty: Type,
11}
12
13impl Field {
14 pub fn new(name: impl Into<String>, ty: Type) -> Self {
16 Self {
17 name: name.into(),
18 ty,
19 }
20 }
21}
22
23#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
25pub struct EnumVariant {
26 pub name: String,
28
29 pub value: i64,
31
32 pub comment: String,
34}
35
36impl EnumVariant {
37 pub fn new(name: impl Into<String>, value: i64) -> Self {
40 Self {
41 name: name.into(),
42 value,
43 comment: "".to_owned(),
44 }
45 }
46
47 pub fn new_with_comment(
49 name: impl Into<String>,
50 value: i64,
51 comment: impl Into<String>,
52 ) -> Self {
53 Self {
54 name: name.into(),
55 value,
56 comment: comment.into(),
57 }
58 }
59}
60
61#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, PartialOrd, Eq, Ord)]
63pub enum Type {
64 Date,
65 Int,
66 BigInt,
67 Numeric,
68 Boolean,
69 String,
70 Bytea,
71 UUID,
72 Inet,
73 Struct {
74 name: String,
75 fields: Vec<Field>,
76 },
77 StructRef(String),
78 Object,
79 DataTable {
80 name: String,
81 fields: Vec<Field>,
82 },
83 Vec(Box<Type>),
84 Unit,
85 Optional(Box<Type>),
86 Enum {
87 name: String,
88 variants: Vec<EnumVariant>,
89 },
90 EnumRef(String),
91 TimeStampMs,
92 BlockchainDecimal,
93 BlockchainAddress,
94 BlockchainTransactionHash,
95}
96
97impl Type {
98 pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
100 Self::Struct {
101 name: name.into(),
102 fields,
103 }
104 }
105
106 pub fn struct_ref(name: impl Into<String>) -> Self {
108 Self::StructRef(name.into())
109 }
110
111 pub fn datatable(name: impl Into<String>, fields: Vec<Field>) -> Self {
113 Self::DataTable {
114 name: name.into(),
115 fields,
116 }
117 }
118
119 pub fn vec(ty: Type) -> Self {
121 Self::Vec(Box::new(ty))
122 }
123
124 pub fn optional(ty: Type) -> Self {
126 Self::Optional(Box::new(ty))
127 }
128
129 pub fn enum_ref(name: impl Into<String>) -> Self {
131 Self::EnumRef(name.into())
132 }
133
134 pub fn enum_(name: impl Into<String>, fields: Vec<EnumVariant>) -> Self {
136 Self::Enum {
137 name: name.into(),
138 variants: fields,
139 }
140 }
141 pub fn try_unwrap(self) -> Option<Self> {
142 match self {
143 Self::Vec(v) => Some(*v),
144 Self::DataTable { .. } => None,
145 _ => Some(self),
146 }
147 }
148}