gull/definitions/
mod.rs

1pub mod builders;
2pub mod declarations;
3
4pub use declarations::Declarations;
5
6#[derive(Debug, Clone)]
7pub enum TPrimitive {
8    String,
9    Ti64,
10    Tf64,
11    Ti32,
12    Tu32,
13    Tusize,
14    Tbool,
15    TGeneric(TGeneric),
16    TReference(TReference),
17    THardcoded(&'static str),
18    TDifferentPerLanguage {
19        hack: Box<TPrimitive>,
20        rust: Box<TPrimitive>,
21        flow: Box<TPrimitive>,
22    },
23    TVec(Box<TVec>),
24    TMap(Box<TMap>),
25    TOption(Box<TOption>),
26}
27
28#[derive(Debug, Clone)]
29pub struct TReference {
30    // private field. must be returned by declarations
31    name: &'static str,
32    pub generic_params: Vec<TGeneric>,
33}
34
35impl TReference {
36    pub fn get_name(&self) -> &'static str {
37        self.name
38    }
39
40    pub fn primitive(&self) -> TPrimitive {
41        TPrimitive::TReference(self.clone())
42    }
43}
44
45#[derive(Debug, Clone)]
46pub enum TOption {
47    TMap(TMap),
48    TPrimitive(TPrimitive),
49    TSet(TSet),
50    TVec(TVec),
51    TTuple(TTuple),
52}
53
54#[derive(Debug, Clone)]
55pub enum TVec {
56    TPrimitive(TPrimitive),
57}
58
59#[derive(Debug, Clone)]
60pub enum TSet {
61    TPrimitive(TPrimitive),
62}
63
64#[derive(Debug, Clone, Copy)]
65pub enum TMapType {
66    Hash,
67    BTree,
68}
69
70#[derive(Debug, Clone)]
71pub struct TMap {
72    pub key: TPrimitive,
73    pub value: TMapValue,
74    pub t: TMapType,
75}
76
77#[derive(Debug, Clone)]
78pub enum TGeneric {
79    // Params that are defined and used within struct/type. no namespacing
80    // needed etc.
81    TDefinition {
82        name: &'static str,
83        bounds: Option<&'static str>,
84    },
85    // generic param that references ather thing. e.g. Vec<OtherTypeReference>
86    TReference(TReference),
87}
88
89#[derive(Debug, Clone)]
90pub enum TMapValue {
91    TPrimitive(TPrimitive),
92    TSet(TSet),
93}
94
95#[derive(Debug, Clone)]
96pub struct TTuple {
97    pub items: Vec<TupleItem>,
98}
99
100#[derive(Debug, Clone)]
101pub enum TupleItem {
102    TPrimitive(TPrimitive),
103    TOption(TOption),
104}
105
106#[derive(Debug, Clone)]
107pub struct TypeDeclaration {
108    pub name: &'static str,
109    pub docs: &'static str,
110    pub value: DeclarationValue,
111    pub config: Vec<TypeDeclarationConfig>,
112    pub generic_params: Vec<TGeneric>,
113}
114
115#[derive(Debug, Clone, Copy)]
116pub enum TypeDeclarationConfig {
117    RustAttribute(&'static str),
118}
119
120#[derive(Debug, Clone)]
121pub enum DeclarationValue {
122    TEnum(TEnum),
123    TSimpleEnum(TSimpleEnum),
124    TMap(Box<TMap>),
125    TVec(Box<TVec>),
126    TOption(Box<TOption>),
127    TPrimitive(TPrimitive),
128    TStruct(TStruct),
129    TTuple(TTuple),
130    Docs,
131    CodeBlock(CodeBlock),
132}
133
134#[derive(Debug, Clone)]
135pub enum CodeBlock {
136    Rust(Vec<&'static str>),
137    Hack(Vec<&'static str>),
138    Flow(Vec<&'static str>),
139}
140
141#[derive(Debug, Clone)]
142pub struct TStruct {
143    pub fields: Vec<StructField>,
144}
145
146#[derive(Debug, Clone)]
147pub struct StructField {
148    pub name: &'static str,
149    pub docs: &'static str,
150    pub field_type: StructFieldType,
151    pub config: Vec<StructFieldConfig>,
152}
153
154#[derive(Debug, Clone)]
155pub enum StructFieldConfig {
156    RustAttribute(&'static str),
157    RustOverride(&'static str),
158}
159
160#[derive(Debug, Clone)]
161pub enum StructFieldType {
162    TMap(TMap),
163    TSet(TSet),
164    TOption(TOption),
165    TPrimitive(TPrimitive),
166    TTuple(TTuple),
167    TVec(TVec),
168}
169
170#[derive(Debug, Clone)]
171pub struct TEnum {
172    pub variants: Vec<EnumVariant>,
173}
174
175#[derive(Debug, Clone)]
176pub struct EnumVariant {
177    pub name: &'static str,
178    pub docs: &'static str,
179    pub variant_type: EnumVariantType,
180}
181
182#[derive(Debug, Clone)]
183pub enum EnumVariantType {
184    // WARNING: This enum is pretty limited in what it can represent. Since hack
185    // and flow don't have enums, we need to be careful at how we represent the
186    // serialization of these enums on rust size. E.g.
187    // see https://serde.rs/enum-representations.html for details.
188    //
189    // Default representation puts current varariant as a key in a JSON object
190    // (e.g. {"MyVariant" => {"data": 1}})
191    //
192    // We can't have empty variants, since they would be represented as a single
193    // string in json by deault. e.g. `MyEnum::EmptyVariant` will serialize as
194    // just "EmtyVariant" string, which will mess up hack static typing. (it has
195    // no disjoint/distriminating unions)
196    //
197    // If we want to represent an empty enum on rust side, it HAS to have some
198    // data in it. so en Empty variant should be a primitive type with, e.g. a
199    // boolean type inside, so it serializes into `{"EmptyVariant": true}` and
200    // can be still represented as a shape/object on hack/js side.
201    TStruct(TStruct),
202    TPrimitive(TPrimitive),
203}
204
205/// This enum is an enum that has NO data associated with its variants.
206/// Basically like a C-like enum, where enum values are strings.
207/// It will serialize in a single string value (instead of the object/string
208/// stuff that we get with full featured enums). This easily maps to hack enums
209/// and flow string union types.
210#[derive(Debug, Clone)]
211pub struct TSimpleEnum {
212    pub variants: Vec<&'static str>,
213}