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