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    NanoId { len: usize },
92    IpAddr,
93    Struct {
94        name: String,
95        fields: Vec<Field>,
96    },
97    StructRef(String),
98    Object,
99    // DataTable {
100    //     name: String,
101    //     fields: Vec<Field>,
102    // },
103    StructTable {
104        struct_ref: String,
105    },
106    Vec(Box<Type>),
107    Unit,
108    Optional(Box<Type>),
109    Enum {
110        name: String,
111        variants: Vec<EnumVariant>,
112    },
113    EnumRef {
114        name: String,
115        #[serde(default, skip_serializing)]
116        prefixed_name: bool,
117    },
118    TimeStampMs,
119    BlockchainDecimal,
120    BlockchainAddress,
121    BlockchainTransactionHash,
122}
123
124impl Type {
125    /// Creates a new `Type::Struct` with the given name and fields.
126    pub fn struct_(name: impl Into<String>, fields: Vec<Field>) -> Self {
127        Self::Struct {
128            name: name.into(),
129            fields,
130        }
131    }
132
133    /// Creates a new `Type::StructRef` with the given name.
134    pub fn struct_ref(name: impl Into<String>) -> Self {
135        Self::StructRef(name.into())
136    }
137
138    // /// Creates a new `Type::DataTable` with the given name and fields.
139    // pub fn datatable(name: impl Into<String>, fields: Vec<Field>) -> Self {
140    //     Self::DataTable {
141    //         name: name.into(),
142    //         fields,
143    //     }
144    // }
145
146    /// Creates a new `Type::StructTable` with the given struct reference.
147    pub fn struct_table(struct_ref: impl Into<String>) -> Self {
148        Self::StructTable {
149            struct_ref: struct_ref.into(),
150        }
151    }
152
153    /// Creates a new `Type::Vec` with the given type.
154    pub fn vec(ty: Type) -> Self {
155        Self::Vec(Box::new(ty))
156    }
157
158    /// Creates a new `Type::Optional` with the given type.
159    pub fn optional(ty: Type) -> Self {
160        Self::Optional(Box::new(ty))
161    }
162
163    /// Creates a new `Type::EnumRef` with the given name.
164    pub fn enum_ref(name: impl Into<String>, prefixed_name: bool) -> Self {
165        Self::EnumRef {
166            name: name.into(),
167            prefixed_name,
168        }
169    }
170
171    /// Creates a new `Type::Enum` with the given name and fields/variants.
172    pub fn enum_(name: impl Into<String>, fields: Vec<EnumVariant>) -> Self {
173        Self::Enum {
174            name: name.into(),
175            variants: fields,
176        }
177    }
178    pub fn try_unwrap(self) -> Option<Self> {
179        match self {
180            Self::Vec(v) => Some(*v),
181            // Self::DataTable { .. } => None,
182            Self::StructTable { .. } => None,
183            _ => Some(self),
184        }
185    }
186
187    pub fn add_default_enum_derives(input: String) -> String {
188        format!(
189            r#"#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromPrimitive, PartialEq, Eq, PartialOrd, Ord, EnumString, Display, Hash)]{input}"#
190        )
191    }
192
193    pub fn add_default_struct_derives(input: String) -> String {
194        format!(
195            r#" #[derive(Serialize, Deserialize, Debug, Clone)]
196                #[serde(rename_all = "camelCase")]
197                {input}
198            "#
199        )
200    }
201}