|
| 1 | +//! BuildSchema derive implementation for structs (records) |
| 2 | +
|
| 3 | +use darling::FromField; |
| 4 | +use proc_macro2::TokenStream; |
| 5 | +use quote::{format_ident, quote}; |
| 6 | +use syn::{DataStruct, Fields}; |
| 7 | + |
| 8 | +use crate::attrs::FieldAttrs; |
| 9 | +use crate::context::MacroContext; |
| 10 | + |
| 11 | +pub fn generate_record_schema(context: &MacroContext, input: &DataStruct) -> TokenStream { |
| 12 | + match &input.fields { |
| 13 | + Fields::Named(fields) => generate_named_struct(context, &fields.named), |
| 14 | + Fields::Unnamed(fields) if fields.unnamed.len() == 1 => { |
| 15 | + generate_newtype_struct(context, &fields.unnamed[0].ty) |
| 16 | + } |
| 17 | + Fields::Unnamed(fields) => generate_tuple_struct(context, &fields.unnamed), |
| 18 | + Fields::Unit => generate_unit_struct(context), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn generate_named_struct( |
| 23 | + context: &MacroContext, |
| 24 | + fields: &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>, |
| 25 | +) -> TokenStream { |
| 26 | + let schema_crate = context.schema_crate(); |
| 27 | + |
| 28 | + let field_schemas: Vec<_> = fields |
| 29 | + .iter() |
| 30 | + .enumerate() |
| 31 | + .map(|(idx, f)| { |
| 32 | + let field_name = f.ident.as_ref().expect("named fields must have names"); |
| 33 | + let field_ty = &f.ty; |
| 34 | + let attrs = FieldAttrs::from_field(f).expect("failed to parse field attributes"); |
| 35 | + |
| 36 | + // Skip flatten fields for now - they need special handling |
| 37 | + if attrs.flatten || attrs.flatten_ext { |
| 38 | + panic!("flatten is not yet supported in BuildSchema derive"); |
| 39 | + } |
| 40 | + |
| 41 | + let field_name_str = attrs |
| 42 | + .rename |
| 43 | + .clone() |
| 44 | + .unwrap_or_else(|| context.apply_rename(&field_name.to_string())); |
| 45 | + |
| 46 | + let schema_var = format_ident!("field_{}_schema", idx); |
| 47 | + |
| 48 | + // Check if the field is Option<T> to mark as optional |
| 49 | + let is_optional = is_option_type(field_ty); |
| 50 | + |
| 51 | + (field_name_str, schema_var, field_ty.clone(), is_optional) |
| 52 | + }) |
| 53 | + .collect(); |
| 54 | + |
| 55 | + // Generate schema building for each field |
| 56 | + let field_builds: Vec<_> = field_schemas |
| 57 | + .iter() |
| 58 | + .map(|(_, schema_var, field_ty, _)| { |
| 59 | + quote! { |
| 60 | + let #schema_var = ctx.build::<#field_ty>(); |
| 61 | + } |
| 62 | + }) |
| 63 | + .collect(); |
| 64 | + |
| 65 | + // Generate the properties HashMap entries |
| 66 | + let properties_entries: Vec<_> = field_schemas |
| 67 | + .iter() |
| 68 | + .map(|(name, schema_var, _, is_optional)| { |
| 69 | + quote! { |
| 70 | + ( |
| 71 | + #name.to_string(), |
| 72 | + #schema_crate::RecordFieldSchema { |
| 73 | + schema: #schema_var, |
| 74 | + optional: #is_optional, |
| 75 | + binding_style: None, |
| 76 | + } |
| 77 | + ) |
| 78 | + } |
| 79 | + }) |
| 80 | + .collect(); |
| 81 | + |
| 82 | + // Determine unknown fields policy |
| 83 | + let unknown_fields_policy = if context.config.allow_unknown_fields { |
| 84 | + quote! { #schema_crate::UnknownFieldsPolicy::Allow } |
| 85 | + } else { |
| 86 | + quote! { #schema_crate::UnknownFieldsPolicy::Deny } |
| 87 | + }; |
| 88 | + |
| 89 | + let content = quote! { |
| 90 | + #(#field_builds)* |
| 91 | + |
| 92 | + #schema_crate::SchemaNodeContent::Record(#schema_crate::RecordSchema { |
| 93 | + properties: [ |
| 94 | + #(#properties_entries),* |
| 95 | + ].into_iter().collect(), |
| 96 | + unknown_fields: #unknown_fields_policy, |
| 97 | + }) |
| 98 | + }; |
| 99 | + |
| 100 | + context.impl_build_schema(content) |
| 101 | +} |
| 102 | + |
| 103 | +fn generate_unit_struct(context: &MacroContext) -> TokenStream { |
| 104 | + let schema_crate = context.schema_crate(); |
| 105 | + let content = quote! { |
| 106 | + #schema_crate::SchemaNodeContent::Null |
| 107 | + }; |
| 108 | + context.impl_build_schema(content) |
| 109 | +} |
| 110 | + |
| 111 | +fn generate_tuple_struct( |
| 112 | + context: &MacroContext, |
| 113 | + fields: &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>, |
| 114 | +) -> TokenStream { |
| 115 | + let schema_crate = context.schema_crate(); |
| 116 | + |
| 117 | + let field_builds: Vec<_> = fields |
| 118 | + .iter() |
| 119 | + .enumerate() |
| 120 | + .map(|(idx, f)| { |
| 121 | + let field_ty = &f.ty; |
| 122 | + let schema_var = format_ident!("field_{}_schema", idx); |
| 123 | + quote! { |
| 124 | + let #schema_var = ctx.build::<#field_ty>(); |
| 125 | + } |
| 126 | + }) |
| 127 | + .collect(); |
| 128 | + |
| 129 | + let schema_vars: Vec<_> = (0..fields.len()) |
| 130 | + .map(|idx| format_ident!("field_{}_schema", idx)) |
| 131 | + .collect(); |
| 132 | + |
| 133 | + let content = quote! { |
| 134 | + #(#field_builds)* |
| 135 | + |
| 136 | + #schema_crate::SchemaNodeContent::Tuple(#schema_crate::TupleSchema { |
| 137 | + elements: vec![#(#schema_vars),*], |
| 138 | + binding_style: None, |
| 139 | + }) |
| 140 | + }; |
| 141 | + |
| 142 | + context.impl_build_schema(content) |
| 143 | +} |
| 144 | + |
| 145 | +fn generate_newtype_struct(context: &MacroContext, field_ty: &syn::Type) -> TokenStream { |
| 146 | + // Newtype just delegates to the inner type |
| 147 | + let content = quote! { |
| 148 | + <#field_ty as BuildSchema>::build_schema(ctx) |
| 149 | + }; |
| 150 | + context.impl_build_schema(content) |
| 151 | +} |
| 152 | + |
| 153 | +/// Check if a type is Option<T> |
| 154 | +fn is_option_type(ty: &syn::Type) -> bool { |
| 155 | + if let syn::Type::Path(type_path) = ty |
| 156 | + && let Some(segment) = type_path.path.segments.last() |
| 157 | + { |
| 158 | + return segment.ident == "Option"; |
| 159 | + } |
| 160 | + false |
| 161 | +} |
0 commit comments