endpoint_libs/model/
types.rs

1use serde::*;
2
3/// `Field` is a struct that represents the parameters and returns in an endpoint schema.
4#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
5pub struct Field {
6    /// The name of the field (e.g. `user_id`)
7    pub name: String,
8
9    /// The type of the field (e.g. `Type::BigInt`)
10    pub ty: Type,
11}
12
13impl Field {
14    /// Creates a new `Field` with the given name and type.
15    pub fn new(name: impl Into<String>, ty: Type) -> Self {
16        Self {
17            name: name.into(),
18            ty,
19        }
20    }
21}
22
23/// `EnumVariant` is a struct that represents the variants of an enum.
24#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
25pub struct EnumVariant {
26    /// The name of the variant (e.g. `UniSwap`)
27    pub name: String,
28
29    /// The value of the variant (e.g. 1)
30    pub value: i64,
31
32    /// A comment added by `new_with_comment` method
33    pub comment: String,
34}
35
36impl EnumVariant {
37    /// Creates a new `EnumVariant` with the given name and value.
38    /// `comment` is set to `""`.
39    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    /// Creates a new `EnumVariant` with the given name, value and comment.
48    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/// `Type` is an enum that represents the types of the fields in an endpoint schema.
62#[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    /// Creates a new `Type::Struct` with the given name and fields.
99    pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
100        Self::Struct {
101            name: name.into(),
102            fields,
103        }
104    }
105
106    /// Creates a new `Type::StructRef` with the given name.
107    pub fn struct_ref(name: impl Into<String>) -> Self {
108        Self::StructRef(name.into())
109    }
110
111    /// Creates a new `Type::DataTable` with the given name and fields.
112    pub fn datatable(name: impl Into<String>, fields: Vec<Field>) -> Self {
113        Self::DataTable {
114            name: name.into(),
115            fields,
116        }
117    }
118
119    /// Creates a new `Type::Vec` with the given type.
120    pub fn vec(ty: Type) -> Self {
121        Self::Vec(Box::new(ty))
122    }
123
124    /// Creates a new `Type::Optional` with the given type.
125    pub fn optional(ty: Type) -> Self {
126        Self::Optional(Box::new(ty))
127    }
128
129    /// Creates a new `Type::EnumRef` with the given name.
130    pub fn enum_ref(name: impl Into<String>) -> Self {
131        Self::EnumRef(name.into())
132    }
133
134    /// Creates a new `Type::Enum` with the given name and fields/variants.
135    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}