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 { name: name.into(), ty }
17    }
18}
19
20/// `EnumVariant` is a struct that represents the variants of an enum.
21#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
22pub struct EnumVariant {
23    /// The name of the variant (e.g. `UniSwap`)
24    pub name: String,
25
26    /// The value of the variant (e.g. 1)
27    pub value: i64,
28
29    /// A comment added by `new_with_comment` method
30    pub comment: String,
31}
32
33impl EnumVariant {
34    /// Creates a new `EnumVariant` with the given name and value.
35    /// `comment` is set to `""`.
36    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    /// Creates a new `EnumVariant` with the given name, value and comment.
45    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/// `Type` is an enum that represents the types of the fields in an endpoint schema.
55#[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    /// Creates a new `Type::Struct` with the given name and fields.
83    pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
84        Self::Struct {
85            name: name.into(),
86            fields,
87        }
88    }
89
90    /// Creates a new `Type::StructRef` with the given name.
91    pub fn struct_ref(name: impl Into<String>) -> Self {
92        Self::StructRef(name.into())
93    }
94
95    /// Creates a new `Type::DataTable` with the given name and fields.
96    pub fn datatable(name: impl Into<String>, fields: Vec<Field>) -> Self {
97        Self::DataTable {
98            name: name.into(),
99            fields,
100        }
101    }
102
103    /// Creates a new `Type::Vec` with the given type.
104    pub fn vec(ty: Type) -> Self {
105        Self::Vec(Box::new(ty))
106    }
107
108    /// Creates a new `Type::Optional` with the given type.
109    pub fn optional(ty: Type) -> Self {
110        Self::Optional(Box::new(ty))
111    }
112
113    /// Creates a new `Type::EnumRef` with the given name.
114    pub fn enum_ref(name: impl Into<String>) -> Self {
115        Self::EnumRef(name.into())
116    }
117
118    /// Creates a new `Type::Enum` with the given name and fields/variants.
119    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}