-
Notifications
You must be signed in to change notification settings - Fork 12
/
parser.rs
317 lines (281 loc) · 12.4 KB
/
parser.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use inflector::Inflector;
use syn::Ident;
use syn::Item::Macro;
use crate::{code, GenerationConfig};
pub const FILE_SIGNATURE: &str = "/* This file is generated and managed by dsync */";
// TODO: handle postgres array types
// TODO: handle postgres tuple/record types
#[derive(Debug, Clone)]
pub struct ParsedColumnMacro {
pub ty: String,
pub name: Ident,
pub is_nullable: bool,
}
#[derive(Debug, Clone)]
pub struct ParsedTableMacro {
pub name: Ident,
pub struct_name: String,
pub columns: Vec<ParsedColumnMacro>,
pub primary_key_columns: Vec<Ident>,
pub foreign_keys: Vec<(
ForeignTableName,
JoinColumn, /* this is the column from this table which maps to the foreign table's primary key*/
)>,
pub generated_code: String,
}
type ForeignTableName = Ident;
type JoinColumn = String;
#[derive(Debug, Clone)]
pub struct ParsedJoinMacro {
pub table1: Ident,
pub table2: Ident,
pub table1_columns: String,
}
pub fn parse_and_generate_code(
schema_file_contents: String,
config: &GenerationConfig,
) -> anyhow::Result<Vec<ParsedTableMacro>> {
let schema_file = syn::parse_file(&schema_file_contents).unwrap();
let mut tables: Vec<ParsedTableMacro> = vec![];
for item in schema_file.items {
if let Macro(macro_item) = item {
let macro_identifier = macro_item
.mac
.path
.segments
.last()
.expect("could not read identifier for macro")
.ident
.to_string();
match macro_identifier.as_str() {
"table" => {
let parsed_table = handle_table_macro(macro_item, config);
// make sure the table isn't ignored
let table_options = config.table(parsed_table.name.to_string().as_str());
if !table_options.get_ignore() {
tables.push(parsed_table);
}
}
"joinable" => {
let parsed_join = handle_joinable_macro(macro_item);
for table in tables.iter_mut() {
if parsed_join
.table1
.to_string()
.eq(table.name.to_string().as_str())
{
table.foreign_keys.push((
parsed_join.table2.clone(),
parsed_join.table1_columns.clone(),
));
break;
}
}
}
_ => {}
};
}
}
for table in tables.iter_mut() {
table.generated_code = code::generate_table(table.clone(), config);
}
Ok(tables)
}
fn handle_joinable_macro(macro_item: syn::ItemMacro) -> ParsedJoinMacro {
// println!("joinable! macro: {:#?}", macro_item);
let mut table1_name: Option<Ident> = None;
let mut table2_name: Option<Ident> = None;
let mut table2_join_column: Option<String> = None;
for item in macro_item.mac.tokens.into_iter() {
match &item {
proc_macro2::TokenTree::Ident(ident) => {
if table1_name.is_none() {
table1_name = Some(ident.clone());
} else if table2_name.is_none() {
table2_name = Some(ident.clone());
}
}
proc_macro2::TokenTree::Group(group) => {
if table1_name.is_none() || table2_name.is_none() {
panic!("Unsupported schema format! (encountered join column group too early)");
} else {
table2_join_column = Some(group.stream().to_string());
}
}
_ => {}
}
}
ParsedJoinMacro {
table1: table1_name
.expect("Unsupported schema format! (could not determine first join table name)"),
table2: table2_name
.expect("Unsupported schema format! (could not determine second join table name)"),
table1_columns: table2_join_column
.expect("Unsupported schema format! (could not determine join column name)"),
}
}
fn handle_table_macro(macro_item: syn::ItemMacro, _config: &GenerationConfig) -> ParsedTableMacro {
let mut table_name_ident: Option<Ident> = None;
let mut table_primary_key_idents: Vec<Ident> = vec![];
let mut table_columns: Vec<ParsedColumnMacro> = vec![];
for item in macro_item.mac.tokens.into_iter() {
match &item {
proc_macro2::TokenTree::Ident(ident) => {
table_name_ident = Some(ident.clone());
}
proc_macro2::TokenTree::Group(group) => {
if group.delimiter() == proc_macro2::Delimiter::Parenthesis {
// primary keys group
// println!("GROUP-keys {:#?}", group);
for key_token in group.stream().into_iter() {
if let proc_macro2::TokenTree::Ident(ident) = key_token {
table_primary_key_idents.push(ident)
}
}
} else if group.delimiter() == proc_macro2::Delimiter::Brace {
// columns group
// println!("GROUP-cols {:#?}", group);
let mut column_name: Option<Ident> = None;
let mut column_type: Option<Ident> = None;
let mut column_nullable: bool = false;
for column_tokens in group.stream().into_iter() {
match column_tokens {
proc_macro2::TokenTree::Ident(ident) => {
if column_name.is_none() {
column_name = Some(ident.clone());
} else if ident.to_string().eq("Nullable") {
column_nullable = true;
} else {
column_type = Some(ident.clone());
}
}
proc_macro2::TokenTree::Punct(punct) => {
let char = punct.as_char();
if char == '-' || char == '>' {
// nothing for arrow
continue;
} else if char == ',' && column_name.is_some() && column_type.is_some() {
// end of column def!
// add the column
table_columns.push(ParsedColumnMacro {
name: column_name.expect("Unsupported schema format! (Invalid column name syntax)"),
ty: schema_type_to_rust_type(column_type.expect("Unsupported schema format! (Invalid column type syntax)").to_string()),
is_nullable: column_nullable,
});
// reset the properties
column_name = None;
column_type = None;
column_nullable = false;
}
}
_ => panic!("Unsupported schema format! (Invalid column definition token in diesel table macro)")
}
}
if column_name.is_some() || column_type.is_some() || column_nullable {
// looks like a column was in the middle of being parsed, let's panic!
panic!(
"Unsupported schema format! (It seems a column was partially defined)"
);
}
} else {
panic!("Unsupported schema format! (Invalid delimiter in diesel table macro group)")
}
}
_ => {
panic!("Unsupported schema format! (Invalid token tree item in diesel table macro)")
}
}
}
ParsedTableMacro {
name: table_name_ident
.clone()
.expect("Unsupported schema format! (Could not extract table name from schema file)"),
struct_name: table_name_ident
.unwrap()
.to_string()
.to_pascal_case()
.to_singular(),
columns: table_columns,
primary_key_columns: table_primary_key_idents,
foreign_keys: vec![],
generated_code: format!(
"{FILE_SIGNATURE}\n\nFATAL ERROR: nothing was generated; this shouldn't be possible."
),
}
}
// A function to translate diesel schema types into rust types
//
// reference: https://github.com/diesel-rs/diesel/blob/master/diesel/src/sql_types/mod.rs
// exact reference; https://github.com/diesel-rs/diesel/blob/292ac5c0ed6474f96734ba2e99b95b442064f69c/diesel/src/mysql/types/mod.rs
//
// The docs page for sql_types is comprehensive but it hides some alias types like Int4, Float8, etc.:
// https://docs.rs/diesel/latest/diesel/sql_types/index.html
fn schema_type_to_rust_type(schema_type: String) -> String {
match schema_type.to_lowercase().as_str() {
"unsigned" => panic!("Unsigned types are not yet supported, please open an issue if you need this feature!"), // TODO: deal with this later
"inet" => panic!("Unsigned types are not yet supported, please open an issue if you need this feature!"), // TODO: deal with this later
"cidr" => panic!("Unsigned types are not yet supported, please open an issue if you need this feature!"), // TODO: deal with this later
// boolean
"bool" => "bool",
// numbers
"tinyint" => "i8",
"smallint" => "i16",
"smallserial" => "i16",
"int2" => "i16",
"int4" => "i32",
"int4range" => "(std::collections::Bound<i32>, std::collections::Bound<i32>)",
"integer" => "i32",
"serial" => "i32",
"bigint" => "i64",
"bigserial" => "i64",
"int8" => "i64",
"int8range" => "(std::collections::Bound<i64>, std::collections::Bound<i64>)",
"float" => "f32",
"float4" => "f32",
"double" => "f64",
"float8" => "f64",
"numeric" => "bigdecimal::BigDecimal",
"numrange" => "(std::collections::Bound<bigdecimal::BigDecimal>, std::collections::Bound<bigdecimal::BigDecimal>)",
"decimal" => "bigdecimal::BigDecimal",
// string
"text" => "String",
"varchar" => "String",
"bpchar" => "String",
"char" => "String",
"tinytext" => "String",
"mediumtext" => "String",
"longtext" => "String",
// bytes
"binary" => "Vec<u8>",
"bytea" => "Vec<u8>",
"tinyblob" => "Vec<u8>",
"blob" => "Vec<u8>",
"mediumblob" => "Vec<u8>",
"longblob" => "Vec<u8>",
"varbinary" => "Vec<u8>",
"bit" => "Vec<u8>",
// date & time
"date" => "chrono::NaiveDate",
"daterange" => "(std::collections::Bound<chrono::NaiveDate>, std::collections::Bound<chrono::NaiveDate>)",
"datetime" => "chrono::NaiveDateTime",
"time" => "chrono::NaiveTime",
"timestamp" => "chrono::NaiveDateTime",
"tsrange" => "(std::collections::Bound<chrono::NaiveDateTime>, std::collections::Bound<chrono::NaiveDateTime>)",
"timestamptz" => "chrono::DateTime<chrono::Utc>",
"timestamptzsqlite" => "chrono::DateTime<chrono::Utc>",
"tstzrange" => "(std::collections::Bound<chrono::DateTime<chrono::Utc>>, std::collections::Bound<chrono::DateTime<chrono::Utc>>)",
// json
"json" => "serde::Value",
"jsonb" => "serde_json::Value",
// misc
"uuid" => "uuid::Uuid",
"interval" => "PgInterval",
"oid" => "u32",
"money" => "PgMoney",
"macaddr" => "[u8; 6]",
// "inet" => "either ipnetwork::IpNetwork or ipnet::IpNet (TODO)",
// "cidr" => "either ipnetwork::IpNetwork or ipnet::IpNet (TODO)",
// panic if no type is found (this means generation is broken for this particular schema)
_ => panic!("Unknown type found '{}', please report this!", schema_type)
}.to_string()
}