Skip to main content

forge_codegen/
parser.rs

1//! Rust source code parser for extracting FORGE schema definitions.
2//!
3//! This module parses Rust source files to extract model, enum, and function
4//! definitions without requiring compilation.
5
6use 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
19/// Parse all Rust source files in a directory and extract schema definitions.
20pub 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, &registry) {
33            tracing::debug!(file = ?entry.path(), error = %e, "Failed to parse file");
34        }
35    }
36
37    Ok(registry)
38}
39
40/// Parse a single Rust source file and extract schema definitions.
41fn 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                    // Parse DTO structs (those with Serialize/Deserialize)
53                    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                    // Parse enums with Serialize/Deserialize
65                    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
82/// Check if attributes contain #[forge::model] or #[model].
83fn 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
93/// Check if attributes contain #[forge_enum] or #[forge::enum_type].
94fn 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"
102    })
103}
104
105/// Check if attributes contain #[derive(...Serialize...)] or #[derive(...Deserialize...)].
106fn 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
116/// Parse a DTO struct (with Serialize/Deserialize) into a TableDef.
117fn parse_dto_struct(item: &syn::ItemStruct) -> Option<TableDef> {
118    let struct_name = item.ident.to_string();
119
120    // Use struct name as table name (DTOs don't have SQL tables)
121    let mut table = TableDef::new(&struct_name, &struct_name);
122
123    // Mark as DTO (not a database table)
124    table.is_dto = true;
125
126    // Extract documentation
127    table.doc = get_doc_comment(&item.attrs);
128
129    // Extract fields
130    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
142/// Parse a struct with #[model] attribute into a TableDef.
143fn 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    // Extract documentation
153    table.doc = get_doc_comment(&item.attrs);
154
155    // Extract fields
156    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
168/// Parse a field definition.
169fn 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
177/// Parse an enum with #[forge_enum] attribute into an EnumDef.
178fn 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        // Check for explicit value
189        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
202/// Parse a function with #[query] or #[mutation] attribute.
203fn 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    // Get return type
208    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    // Parse arguments (skip first arg which is usually context)
218    let mut skip_first = true;
219    for arg in &item.sig.inputs {
220        if let FnArg::Typed(pat_type) = arg {
221            // Skip context argument (usually first)
222            if skip_first {
223                skip_first = false;
224                // Check if it's a context type
225                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            // Extract argument name
235            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
246/// Get the function kind from attributes.
247fn 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        // Check for #[forge::X] or #[X] patterns
253        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
275/// Extract the inner type from Result<T, E>.
276fn extract_result_type(ty: &syn::Type) -> RustType {
277    let type_str = quote::quote!(#ty).to_string().replace(' ', "");
278
279    // Check for Result<T, _>
280    if let Some(rest) = type_str.strip_prefix("Result<") {
281        // Find the inner type before the comma or angle bracket
282        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
311/// Convert a syn::Type to RustType.
312fn type_to_rust_type(ty: &syn::Type) -> RustType {
313    let type_str = quote::quote!(#ty).to_string().replace(' ', "");
314
315    // Handle common types
316    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            // Handle Option<T>
333            if let Some(inner) = type_str
334                .strip_prefix("Option<")
335                .and_then(|s| s.strip_suffix('>'))
336            {
337                let inner_type = match inner {
338                    "String" => RustType::String,
339                    "i32" => RustType::I32,
340                    "i64" => RustType::I64,
341                    "f64" => RustType::F64,
342                    "bool" => RustType::Bool,
343                    "Uuid" => RustType::Uuid,
344                    _ => RustType::Custom(inner.to_string()),
345                };
346                return RustType::Option(Box::new(inner_type));
347            }
348
349            // Handle Vec<T>
350            if let Some(inner) = type_str
351                .strip_prefix("Vec<")
352                .and_then(|s| s.strip_suffix('>'))
353            {
354                let inner_type = match inner {
355                    "String" => RustType::String,
356                    "i32" => RustType::I32,
357                    "u8" => return RustType::Bytes,
358                    _ => RustType::Custom(inner.to_string()),
359                };
360                return RustType::Vec(Box::new(inner_type));
361            }
362
363            // Default to custom type
364            RustType::Custom(type_str)
365        }
366    }
367}
368
369/// Get #[table(name = "...")] value from attributes.
370fn get_table_name_from_attrs(attrs: &[Attribute]) -> Option<String> {
371    for attr in attrs {
372        if attr.path().is_ident("table")
373            && let Meta::List(list) = &attr.meta
374        {
375            let tokens = list.tokens.to_string();
376            if let Some(value) = extract_name_value(&tokens) {
377                return Some(value);
378            }
379        }
380    }
381    None
382}
383
384/// Get string value from attribute like #[attr = "value"].
385fn get_attribute_string_value(attr: &Attribute) -> Option<String> {
386    if let Meta::NameValue(nv) = &attr.meta
387        && let Expr::Lit(lit) = &nv.value
388        && let Lit::Str(s) = &lit.lit
389    {
390        return Some(s.value());
391    }
392    None
393}
394
395/// Get documentation comment from attributes.
396fn get_doc_comment(attrs: &[Attribute]) -> Option<String> {
397    let docs: Vec<String> = attrs
398        .iter()
399        .filter_map(|attr| {
400            if attr.path().is_ident("doc") {
401                get_attribute_string_value(attr)
402            } else {
403                None
404            }
405        })
406        .collect();
407
408    if docs.is_empty() {
409        None
410    } else {
411        Some(
412            docs.into_iter()
413                .map(|s| s.trim().to_string())
414                .collect::<Vec<_>>()
415                .join("\n"),
416        )
417    }
418}
419
420/// Extract name value from "name = \"value\"" format.
421fn extract_name_value(s: &str) -> Option<String> {
422    let parts: Vec<&str> = s.splitn(2, '=').collect();
423    if parts.len() == 2 {
424        let value = parts[1].trim();
425        if let Some(stripped) = value.strip_prefix('"').and_then(|s| s.strip_suffix('"')) {
426            return Some(stripped.to_string());
427        }
428    }
429    None
430}
431
432/// Simple English pluralization.
433fn pluralize(s: &str) -> String {
434    if s.ends_with('s')
435        || s.ends_with("sh")
436        || s.ends_with("ch")
437        || s.ends_with('x')
438        || s.ends_with('z')
439    {
440        format!("{}es", s)
441    } else if let Some(stem) = s.strip_suffix('y') {
442        if !s.ends_with("ay") && !s.ends_with("ey") && !s.ends_with("oy") && !s.ends_with("uy") {
443            format!("{}ies", stem)
444        } else {
445            format!("{}s", s)
446        }
447    } else {
448        format!("{}s", s)
449    }
450}
451
452#[cfg(test)]
453mod tests {
454    use super::*;
455
456    #[test]
457    fn test_parse_model_source() {
458        let source = r#"
459            #[model]
460            struct User {
461                #[id]
462                id: Uuid,
463                email: String,
464                name: Option<String>,
465                #[indexed]
466                created_at: DateTime<Utc>,
467            }
468        "#;
469
470        let registry = SchemaRegistry::new();
471        parse_file(source, &registry).unwrap();
472
473        let table = registry.get_table("users").unwrap();
474        assert_eq!(table.struct_name, "User");
475        assert_eq!(table.fields.len(), 4);
476    }
477
478    #[test]
479    fn test_parse_enum_source() {
480        let source = r#"
481            #[forge_enum]
482            enum ProjectStatus {
483                Draft,
484                Active,
485                Completed,
486            }
487        "#;
488
489        let registry = SchemaRegistry::new();
490        parse_file(source, &registry).unwrap();
491
492        let enum_def = registry.get_enum("ProjectStatus").unwrap();
493        assert_eq!(enum_def.variants.len(), 3);
494    }
495
496    #[test]
497    fn test_to_snake_case() {
498        assert_eq!(to_snake_case("UserProfile"), "user_profile");
499        assert_eq!(to_snake_case("ID"), "i_d");
500        assert_eq!(to_snake_case("createdAt"), "created_at");
501    }
502
503    #[test]
504    fn test_pluralize() {
505        assert_eq!(pluralize("user"), "users");
506        assert_eq!(pluralize("category"), "categories");
507        assert_eq!(pluralize("box"), "boxes");
508        assert_eq!(pluralize("address"), "addresses");
509    }
510
511    #[test]
512    fn test_parse_query_function() {
513        let source = r#"
514            #[query]
515            async fn get_user(ctx: QueryContext, id: Uuid) -> Result<User> {
516                todo!()
517            }
518        "#;
519
520        let registry = SchemaRegistry::new();
521        parse_file(source, &registry).unwrap();
522
523        let func = registry.get_function("get_user").unwrap();
524        assert_eq!(func.name, "get_user");
525        assert_eq!(func.kind, FunctionKind::Query);
526        assert!(func.is_async);
527    }
528
529    #[test]
530    fn test_parse_mutation_function() {
531        let source = r#"
532            #[mutation]
533            async fn create_user(ctx: MutationContext, name: String, email: String) -> Result<User> {
534                todo!()
535            }
536        "#;
537
538        let registry = SchemaRegistry::new();
539        parse_file(source, &registry).unwrap();
540
541        let func = registry.get_function("create_user").unwrap();
542        assert_eq!(func.name, "create_user");
543        assert_eq!(func.kind, FunctionKind::Mutation);
544        assert_eq!(func.args.len(), 2);
545        assert_eq!(func.args[0].name, "name");
546        assert_eq!(func.args[1].name, "email");
547    }
548}