Skip to main content

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 description of the field
10    #[serde(skip)]
11    pub description: String,
12
13    /// The type of the field (e.g. `Type::BigInt`)
14    pub ty: Type,
15}
16
17impl Field {
18    /// Creates a new `Field` with the given name and type.
19    /// `description` is set to `""`.
20    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    /// Creates a new `Field` with the given name, type and description.
29    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/// `EnumVariant` is a struct that represents the variants of an enum.
43#[derive(Clone, Debug, Hash, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
44pub struct EnumVariant {
45    /// The name of the variant (e.g. `UniSwap`)
46    pub name: String,
47
48    /// A description added by `new_with_description` method
49    pub description: String,
50
51    /// The value of the variant (e.g. 1)
52    pub value: i64,
53}
54
55impl EnumVariant {
56    /// Creates a new `EnumVariant` with the given name and value.
57    /// `description` is set to `""`.
58    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    /// Creates a new `EnumVariant` with the given name, value and description.
67    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/// `Type` is an enum that represents the types of the fields in an endpoint schema.
81#[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    IpAddr,
92    Struct {
93        name: String,
94        fields: Vec<Field>,
95    },
96    StructRef(String),
97    Object,
98    // DataTable {
99    //     name: String,
100    //     fields: Vec<Field>,
101    // },
102    StructTable {
103        struct_ref: String,
104    },
105    Vec(Box<Type>),
106    Unit,
107    Optional(Box<Type>),
108    Enum {
109        name: String,
110        variants: Vec<EnumVariant>,
111    },
112    EnumRef {
113        name: String,
114        #[serde(default, skip_serializing)]
115        prefixed_name: bool,
116    },
117    TimeStampMs,
118    BlockchainDecimal,
119    BlockchainAddress,
120    BlockchainTransactionHash,
121}
122
123impl Type {
124    /// Creates a new `Type::Struct` with the given name and fields.
125    pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
126        Self::Struct {
127            name: name.into(),
128            fields,
129        }
130    }
131
132    /// Creates a new `Type::StructRef` with the given name.
133    pub fn struct_ref(name: impl Into<String>) -> Self {
134        Self::StructRef(name.into())
135    }
136
137    // /// Creates a new `Type::DataTable` with the given name and fields.
138    // pub fn datatable(name: impl Into<String>, fields: Vec<Field>) -> Self {
139    //     Self::DataTable {
140    //         name: name.into(),
141    //         fields,
142    //     }
143    // }
144
145    /// Creates a new `Type::StructTable` with the given struct reference.
146    pub fn struct_table(struct_ref: impl Into<String>) -> Self {
147        Self::StructTable {
148            struct_ref: struct_ref.into(),
149        }
150    }
151
152    /// Creates a new `Type::Vec` with the given type.
153    pub fn vec(ty: Type) -> Self {
154        Self::Vec(Box::new(ty))
155    }
156
157    /// Creates a new `Type::Optional` with the given type.
158    pub fn optional(ty: Type) -> Self {
159        Self::Optional(Box::new(ty))
160    }
161
162    /// Creates a new `Type::EnumRef` with the given name.
163    pub fn enum_ref(name: impl Into<String>, prefixed_name: bool) -> Self {
164        Self::EnumRef {
165            name: name.into(),
166            prefixed_name,
167        }
168    }
169
170    /// Creates a new `Type::Enum` with the given name and fields/variants.
171    pub fn enum_(name: impl Into<String>, fields: Vec<EnumVariant>) -> Self {
172        Self::Enum {
173            name: name.into(),
174            variants: fields,
175        }
176    }
177    pub fn try_unwrap(self) -> Option<Self> {
178        match self {
179            Self::Vec(v) => Some(*v),
180            // Self::DataTable { .. } => None,
181            Self::StructTable { .. } => None,
182            _ => Some(self),
183        }
184    }
185
186    pub fn add_default_enum_derives(input: String) -> String {
187        format!(
188            r#"#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromPrimitive, PartialEq, Eq, PartialOrd, Ord, EnumString, Display, Hash)]{input}"#
189        )
190    }
191
192    pub fn add_default_struct_derives(input: String) -> String {
193        format!(
194            r#" #[derive(Serialize, Deserialize, Debug, Clone)]
195                #[serde(rename_all = "camelCase")]
196                {input}
197            "#
198        )
199    }
200}