1use std::path::Path;
7
8use forge_core::schema::{
9 EnumDef, EnumVariant, FieldDef, FunctionArg, FunctionDef, FunctionKind, RustType,
10 SchemaRegistry, TableDef,
11};
12use forge_core::util::to_snake_case;
13use quote::ToTokens;
14use syn::{Attribute, Expr, Fields, FnArg, Lit, Meta, Pat, ReturnType};
15use walkdir::WalkDir;
16
17use crate::Error;
18
19pub fn parse_project(src_dir: &Path) -> Result<SchemaRegistry, Error> {
21 let registry = SchemaRegistry::new();
22
23 let mut files: Vec<_> = WalkDir::new(src_dir)
24 .into_iter()
25 .filter_map(|e| e.ok())
26 .filter(|e| e.path().extension().map(|ext| ext == "rs").unwrap_or(false))
27 .collect();
28 files.sort_by(|a, b| a.path().cmp(b.path()));
29
30 for entry in files {
31 let content = std::fs::read_to_string(entry.path())?;
32 if let Err(e) = parse_file(&content, ®istry) {
33 tracing::debug!(file = ?entry.path(), error = %e, "Failed to parse file");
34 }
35 }
36
37 Ok(registry)
38}
39
40fn parse_file(content: &str, registry: &SchemaRegistry) -> Result<(), Error> {
42 let file = syn::parse_file(content).map_err(|e| Error::Template(e.to_string()))?;
43
44 for item in file.items {
45 match item {
46 syn::Item::Struct(item_struct) => {
47 if has_forge_model_attr(&item_struct.attrs) {
48 if let Some(table) = parse_model(&item_struct) {
49 registry.register_table(table);
50 }
51 } else if has_serde_derive(&item_struct.attrs) {
52 if let Some(table) = parse_dto_struct(&item_struct) {
54 registry.register_table(table);
55 }
56 }
57 }
58 syn::Item::Enum(item_enum) => {
59 if has_forge_enum_attr(&item_enum.attrs) {
60 if let Some(enum_def) = parse_enum(&item_enum) {
61 registry.register_enum(enum_def);
62 }
63 } else if has_serde_derive(&item_enum.attrs) {
64 if let Some(enum_def) = parse_enum(&item_enum) {
66 registry.register_enum(enum_def);
67 }
68 }
69 }
70 syn::Item::Fn(item_fn) => {
71 if let Some(func) = parse_function(&item_fn) {
72 registry.register_function(func);
73 }
74 }
75 _ => {}
76 }
77 }
78
79 Ok(())
80}
81
82fn has_forge_model_attr(attrs: &[Attribute]) -> bool {
84 attrs.iter().any(|attr| {
85 let path = attr.path();
86 path.is_ident("model")
87 || path.segments.len() == 2
88 && path.segments[0].ident == "forge"
89 && path.segments[1].ident == "model"
90 })
91}
92
93fn has_forge_enum_attr(attrs: &[Attribute]) -> bool {
95 attrs.iter().any(|attr| {
96 let path = attr.path();
97 path.is_ident("forge_enum")
98 || path.is_ident("enum_type")
99 || path.segments.len() == 2
100 && path.segments[0].ident == "forge"
101 && (path.segments[1].ident == "enum_type" || path.segments[1].ident == "forge_enum")
102 })
103}
104
105fn has_serde_derive(attrs: &[Attribute]) -> bool {
107 attrs.iter().any(|attr| {
108 if !attr.path().is_ident("derive") {
109 return false;
110 }
111 let tokens = attr.meta.to_token_stream().to_string();
112 tokens.contains("Serialize") || tokens.contains("Deserialize")
113 })
114}
115
116fn parse_dto_struct(item: &syn::ItemStruct) -> Option<TableDef> {
118 let struct_name = item.ident.to_string();
119
120 let mut table = TableDef::new(&struct_name, &struct_name);
122
123 table.is_dto = true;
125
126 table.doc = get_doc_comment(&item.attrs);
128
129 if let Fields::Named(fields) = &item.fields {
131 for field in &fields.named {
132 if let Some(field_name) = &field.ident {
133 let field_def = parse_field(field_name.to_string(), &field.ty, &field.attrs);
134 table.fields.push(field_def);
135 }
136 }
137 }
138
139 Some(table)
140}
141
142fn parse_model(item: &syn::ItemStruct) -> Option<TableDef> {
144 let struct_name = item.ident.to_string();
145 let table_name = get_table_name_from_attrs(&item.attrs).unwrap_or_else(|| {
146 let snake = to_snake_case(&struct_name);
147 pluralize(&snake)
148 });
149
150 let mut table = TableDef::new(&table_name, &struct_name);
151
152 table.doc = get_doc_comment(&item.attrs);
154
155 if let Fields::Named(fields) = &item.fields {
157 for field in &fields.named {
158 if let Some(field_name) = &field.ident {
159 let field_def = parse_field(field_name.to_string(), &field.ty, &field.attrs);
160 table.fields.push(field_def);
161 }
162 }
163 }
164
165 Some(table)
166}
167
168fn parse_field(name: String, ty: &syn::Type, attrs: &[Attribute]) -> FieldDef {
170 let rust_type = type_to_rust_type(ty);
171 let mut field = FieldDef::new(&name, rust_type);
172 field.column_name = to_snake_case(&name);
173 field.doc = get_doc_comment(attrs);
174 field
175}
176
177fn parse_enum(item: &syn::ItemEnum) -> Option<EnumDef> {
179 let enum_name = item.ident.to_string();
180 let mut enum_def = EnumDef::new(&enum_name);
181 enum_def.doc = get_doc_comment(&item.attrs);
182
183 for variant in &item.variants {
184 let variant_name = variant.ident.to_string();
185 let mut enum_variant = EnumVariant::new(&variant_name);
186 enum_variant.doc = get_doc_comment(&variant.attrs);
187
188 if let Some((_, Expr::Lit(lit))) = &variant.discriminant
190 && let Lit::Int(int_lit) = &lit.lit
191 && let Ok(value) = int_lit.base10_parse::<i32>()
192 {
193 enum_variant.int_value = Some(value);
194 }
195
196 enum_def.variants.push(enum_variant);
197 }
198
199 Some(enum_def)
200}
201
202fn parse_function(item: &syn::ItemFn) -> Option<FunctionDef> {
204 let kind = get_function_kind(&item.attrs)?;
205 let func_name = item.sig.ident.to_string();
206
207 let return_type = match &item.sig.output {
209 ReturnType::Default => RustType::Custom("()".to_string()),
210 ReturnType::Type(_, ty) => extract_result_type(ty),
211 };
212
213 let mut func = FunctionDef::new(&func_name, kind, return_type);
214 func.doc = get_doc_comment(&item.attrs);
215 func.is_async = item.sig.asyncness.is_some();
216
217 let mut skip_first = true;
219 for arg in &item.sig.inputs {
220 if let FnArg::Typed(pat_type) = arg {
221 if skip_first {
223 skip_first = false;
224 let type_str = quote::quote!(#pat_type.ty).to_string();
226 if type_str.contains("Context")
227 || type_str.contains("QueryContext")
228 || type_str.contains("MutationContext")
229 {
230 continue;
231 }
232 }
233
234 if let Pat::Ident(pat_ident) = &*pat_type.pat {
236 let arg_name = pat_ident.ident.to_string();
237 let arg_type = type_to_rust_type(&pat_type.ty);
238 func.args.push(FunctionArg::new(arg_name, arg_type));
239 }
240 }
241 }
242
243 Some(func)
244}
245
246fn get_function_kind(attrs: &[Attribute]) -> Option<FunctionKind> {
248 for attr in attrs {
249 let path = attr.path();
250 let segments: Vec<_> = path.segments.iter().map(|s| s.ident.to_string()).collect();
251
252 let kind_str = if segments.len() == 2 && segments[0] == "forge" {
254 Some(segments[1].as_str())
255 } else if segments.len() == 1 {
256 Some(segments[0].as_str())
257 } else {
258 None
259 };
260
261 if let Some(kind) = kind_str {
262 match kind {
263 "query" => return Some(FunctionKind::Query),
264 "mutation" => return Some(FunctionKind::Mutation),
265 "job" => return Some(FunctionKind::Job),
266 "cron" => return Some(FunctionKind::Cron),
267 "workflow" => return Some(FunctionKind::Workflow),
268 _ => {}
269 }
270 }
271 }
272 None
273}
274
275fn extract_result_type(ty: &syn::Type) -> RustType {
277 let type_str = quote::quote!(#ty).to_string().replace(' ', "");
278
279 if let Some(rest) = type_str.strip_prefix("Result<") {
281 let mut depth = 0;
283 let mut end_idx = 0;
284 for (i, c) in rest.chars().enumerate() {
285 match c {
286 '<' => depth += 1,
287 '>' => {
288 if depth == 0 {
289 end_idx = i;
290 break;
291 }
292 depth -= 1;
293 }
294 ',' if depth == 0 => {
295 end_idx = i;
296 break;
297 }
298 _ => {}
299 }
300 }
301 let inner = &rest[..end_idx];
302 return type_to_rust_type(
303 &syn::parse_str(inner)
304 .unwrap_or_else(|_| syn::parse_str::<syn::Type>("String").unwrap()),
305 );
306 }
307
308 type_to_rust_type(ty)
309}
310
311fn type_to_rust_type(ty: &syn::Type) -> RustType {
313 let type_str = quote::quote!(#ty).to_string().replace(' ', "");
314
315 match type_str.as_str() {
317 "String" | "&str" => RustType::String,
318 "i32" => RustType::I32,
319 "i64" => RustType::I64,
320 "f32" => RustType::F32,
321 "f64" => RustType::F64,
322 "bool" => RustType::Bool,
323 "Uuid" | "uuid::Uuid" => RustType::Uuid,
324 "DateTime<Utc>" | "chrono::DateTime<Utc>" | "chrono::DateTime<chrono::Utc>" => {
325 RustType::DateTime
326 }
327 "NaiveDate" | "chrono::NaiveDate" => RustType::Date,
328 "NaiveTime" | "chrono::NaiveTime" => RustType::Custom("NaiveTime".to_string()),
329 "serde_json::Value" | "Value" => RustType::Json,
330 "Vec<u8>" => RustType::Bytes,
331 _ => {
332 if let Some(inner) = type_str
334 .strip_prefix("Option<")
335 .and_then(|s| s.strip_suffix('>'))
336 {
337 let inner_ty: syn::Type =
338 syn::parse_str(inner).unwrap_or_else(|_| syn::parse_str("String").unwrap());
339 return RustType::Option(Box::new(type_to_rust_type(&inner_ty)));
340 }
341
342 if let Some(inner) = type_str
344 .strip_prefix("Vec<")
345 .and_then(|s| s.strip_suffix('>'))
346 {
347 if inner == "u8" {
348 return RustType::Bytes;
349 }
350 let inner_ty: syn::Type =
351 syn::parse_str(inner).unwrap_or_else(|_| syn::parse_str("String").unwrap());
352 return RustType::Vec(Box::new(type_to_rust_type(&inner_ty)));
353 }
354
355 RustType::Custom(type_str)
357 }
358 }
359}
360
361fn get_table_name_from_attrs(attrs: &[Attribute]) -> Option<String> {
363 for attr in attrs {
364 if attr.path().is_ident("table")
365 && let Meta::List(list) = &attr.meta
366 {
367 let tokens = list.tokens.to_string();
368 if let Some(value) = extract_name_value(&tokens) {
369 return Some(value);
370 }
371 }
372 }
373 None
374}
375
376fn get_attribute_string_value(attr: &Attribute) -> Option<String> {
378 if let Meta::NameValue(nv) = &attr.meta
379 && let Expr::Lit(lit) = &nv.value
380 && let Lit::Str(s) = &lit.lit
381 {
382 return Some(s.value());
383 }
384 None
385}
386
387fn get_doc_comment(attrs: &[Attribute]) -> Option<String> {
389 let docs: Vec<String> = attrs
390 .iter()
391 .filter_map(|attr| {
392 if attr.path().is_ident("doc") {
393 get_attribute_string_value(attr)
394 } else {
395 None
396 }
397 })
398 .collect();
399
400 if docs.is_empty() {
401 None
402 } else {
403 Some(
404 docs.into_iter()
405 .map(|s| s.trim().to_string())
406 .collect::<Vec<_>>()
407 .join("\n"),
408 )
409 }
410}
411
412fn extract_name_value(s: &str) -> Option<String> {
414 let parts: Vec<&str> = s.splitn(2, '=').collect();
415 if parts.len() == 2 {
416 let value = parts[1].trim();
417 if let Some(stripped) = value.strip_prefix('"').and_then(|s| s.strip_suffix('"')) {
418 return Some(stripped.to_string());
419 }
420 }
421 None
422}
423
424fn pluralize(s: &str) -> String {
426 if s.ends_with('s')
427 || s.ends_with("sh")
428 || s.ends_with("ch")
429 || s.ends_with('x')
430 || s.ends_with('z')
431 {
432 format!("{}es", s)
433 } else if let Some(stem) = s.strip_suffix('y') {
434 if !s.ends_with("ay") && !s.ends_with("ey") && !s.ends_with("oy") && !s.ends_with("uy") {
435 format!("{}ies", stem)
436 } else {
437 format!("{}s", s)
438 }
439 } else {
440 format!("{}s", s)
441 }
442}
443
444#[cfg(test)]
445mod tests {
446 use super::*;
447
448 #[test]
449 fn test_parse_model_source() {
450 let source = r#"
451 #[model]
452 struct User {
453 #[id]
454 id: Uuid,
455 email: String,
456 name: Option<String>,
457 #[indexed]
458 created_at: DateTime<Utc>,
459 }
460 "#;
461
462 let registry = SchemaRegistry::new();
463 parse_file(source, ®istry).unwrap();
464
465 let table = registry.get_table("users").unwrap();
466 assert_eq!(table.struct_name, "User");
467 assert_eq!(table.fields.len(), 4);
468 }
469
470 #[test]
471 fn test_parse_enum_source() {
472 let source = r#"
473 #[forge_enum]
474 enum ProjectStatus {
475 Draft,
476 Active,
477 Completed,
478 }
479 "#;
480
481 let registry = SchemaRegistry::new();
482 parse_file(source, ®istry).unwrap();
483
484 let enum_def = registry.get_enum("ProjectStatus").unwrap();
485 assert_eq!(enum_def.variants.len(), 3);
486 }
487
488 #[test]
489 fn test_to_snake_case() {
490 assert_eq!(to_snake_case("UserProfile"), "user_profile");
491 assert_eq!(to_snake_case("ID"), "i_d");
492 assert_eq!(to_snake_case("createdAt"), "created_at");
493 }
494
495 #[test]
496 fn test_pluralize() {
497 assert_eq!(pluralize("user"), "users");
498 assert_eq!(pluralize("category"), "categories");
499 assert_eq!(pluralize("box"), "boxes");
500 assert_eq!(pluralize("address"), "addresses");
501 }
502
503 #[test]
504 fn test_parse_query_function() {
505 let source = r#"
506 #[query]
507 async fn get_user(ctx: QueryContext, id: Uuid) -> Result<User> {
508 todo!()
509 }
510 "#;
511
512 let registry = SchemaRegistry::new();
513 parse_file(source, ®istry).unwrap();
514
515 let func = registry.get_function("get_user").unwrap();
516 assert_eq!(func.name, "get_user");
517 assert_eq!(func.kind, FunctionKind::Query);
518 assert!(func.is_async);
519 }
520
521 #[test]
522 fn test_parse_mutation_function() {
523 let source = r#"
524 #[mutation]
525 async fn create_user(ctx: MutationContext, name: String, email: String) -> Result<User> {
526 todo!()
527 }
528 "#;
529
530 let registry = SchemaRegistry::new();
531 parse_file(source, ®istry).unwrap();
532
533 let func = registry.get_function("create_user").unwrap();
534 assert_eq!(func.name, "create_user");
535 assert_eq!(func.kind, FunctionKind::Mutation);
536 assert_eq!(func.args.len(), 2);
537 assert_eq!(func.args[0].name, "name");
538 assert_eq!(func.args[1].name, "email");
539 }
540}