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 { name: name.into(), ty }
17 }
18}
19
20#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
22pub struct EnumVariant {
23 pub name: String,
25
26 pub value: i64,
28
29 pub comment: String,
31}
32
33impl EnumVariant {
34 pub fn new(name: impl Into<String>, value: i64) -> Self {
37 Self {
38 name: name.into(),
39 value,
40 comment: "".to_owned(),
41 }
42 }
43
44 pub fn new_with_comment(name: impl Into<String>, value: i64, comment: impl Into<String>) -> Self {
46 Self {
47 name: name.into(),
48 value,
49 comment: comment.into(),
50 }
51 }
52}
53
54#[derive(Clone, Debug, Serialize, Deserialize, Hash, PartialEq, PartialOrd, Eq, Ord)]
56pub enum Type {
57 Date,
58 Int,
59 BigInt,
60 Numeric,
61 Boolean,
62 String,
63 Bytea,
64 UUID,
65 Inet,
66 Struct { name: String, fields: Vec<Field> },
67 StructRef(String),
68 Object,
69 DataTable { name: String, fields: Vec<Field> },
70 Vec(Box<Type>),
71 Unit,
72 Optional(Box<Type>),
73 Enum { name: String, variants: Vec<EnumVariant> },
74 EnumRef(String),
75 TimeStampMs,
76 BlockchainDecimal,
77 BlockchainAddress,
78 BlockchainTransactionHash,
79}
80
81impl Type {
82 pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
84 Self::Struct {
85 name: name.into(),
86 fields,
87 }
88 }
89
90 pub fn struct_ref(name: impl Into<String>) -> Self {
92 Self::StructRef(name.into())
93 }
94
95 pub fn datatable(name: impl Into<String>, fields: Vec<Field>) -> Self {
97 Self::DataTable {
98 name: name.into(),
99 fields,
100 }
101 }
102
103 pub fn vec(ty: Type) -> Self {
105 Self::Vec(Box::new(ty))
106 }
107
108 pub fn optional(ty: Type) -> Self {
110 Self::Optional(Box::new(ty))
111 }
112
113 pub fn enum_ref(name: impl Into<String>) -> Self {
115 Self::EnumRef(name.into())
116 }
117
118 pub fn enum_(name: impl Into<String>, fields: Vec<EnumVariant>) -> Self {
120 Self::Enum {
121 name: name.into(),
122 variants: fields,
123 }
124 }
125 pub fn try_unwrap(self) -> Option<Self> {
126 match self {
127 Self::Vec(v) => Some(*v),
128 Self::DataTable { .. } => None,
129 _ => Some(self),
130 }
131 }
132}