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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
use proc_macro2::TokenStream;
use quote::quote;
use syn::{DataStruct, Fields, Ident};
use crate::container_attrs::ContainerAttributes;
use crate::parsers::field_parser::parse_field_attributes;
use crate::type_utils::{
get_array_inner_type, get_box_inner_type, get_map_types, get_option_inner_type,
get_schema_type_from_rust_type, get_tuple_element_types, get_type_name, is_array_type,
is_box_type, is_json_value_type, is_map_type, is_option_type, is_self_reference, is_tuple_type,
};
/// Generate the schema implementation for a struct
pub fn generate_struct_schema(
name: &Ident,
data_struct: &DataStruct,
container_attrs: &ContainerAttributes,
) -> TokenStream {
let mut property_setters = Vec::new();
let mut required_setters = Vec::new();
let mut has_self_reference = false;
let struct_name_str = name.to_string();
match &data_struct.fields {
Fields::Named(fields) => {
// First pass: check for self-references
for field in &fields.named {
if is_self_reference(&field.ty, &struct_name_str) {
has_self_reference = true;
break;
}
}
for field in &fields.named {
// Parse field attributes first to check for serde rename
let attrs = parse_field_attributes(field);
let original_field_name = field.ident.as_ref().unwrap().to_string();
// Priority: 1) field-level #[serde(rename)], 2) container #[serde(rename_all)], 3) original name
let field_name = if let Some(ref rename) = attrs.serde_rename {
rename.clone()
} else if let Some(rename_all) = &container_attrs.serde_rename_all {
// Apply the serde rename_all transformation
apply_rename_all(&original_field_name, rename_all)
} else {
original_field_name
};
let is_optional = is_option_type(&field.ty);
// Get schema type
let schema_type = get_schema_type_from_rust_type(&field.ty);
// Extract type name for well-known library types only (exact matches, no heuristics)
let type_name = get_type_name(&field.ty);
// Also extract the inner type name when the field is Optional,
// so Option<NaiveDate>, Option<DateTime>, Option<Uuid> etc. get proper format metadata
let inner_type_name = if is_optional {
get_type_name(get_option_inner_type(&field.ty))
} else {
None
};
// Check for well-known library types by exact match only (no contains checks)
let is_datetime_type = matches!(
type_name.as_deref(),
Some("DateTime") | Some("NaiveDateTime")
) || matches!(
inner_type_name.as_deref(),
Some("DateTime") | Some("NaiveDateTime")
);
let is_date_only_type =
matches!(type_name.as_deref(), Some("NaiveDate") | Some("Date"))
|| matches!(inner_type_name.as_deref(), Some("NaiveDate") | Some("Date"));
let is_uuid_type = matches!(type_name.as_deref(), Some("Uuid"))
|| matches!(inner_type_name.as_deref(), Some("Uuid"));
// Create field property
// IMPORTANT: Default to treating unknown types as structs (objects)
// Structs are far more common than enums, and this is the safest default
let field_prop = if is_datetime_type {
// For date-time types (DateTime, NaiveDateTime)
quote! {
// Create property for this date-time field
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("string".to_string()));
props.insert("format".to_string(), ::serde_json::Value::String("date-time".to_string()));
props.insert("description".to_string(),
::serde_json::Value::String("ISO-8601 formatted date and time".to_string()));
}
} else if is_date_only_type {
// For date-only types (NaiveDate, Date)
quote! {
// Create property for this date field
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("string".to_string()));
props.insert("format".to_string(), ::serde_json::Value::String("date".to_string()));
props.insert("description".to_string(),
::serde_json::Value::String("ISO-8601 formatted date (YYYY-MM-DD)".to_string()));
}
} else if is_uuid_type {
// For UUID types
quote! {
// Create property for this UUID field
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("string".to_string()));
props.insert("format".to_string(), ::serde_json::Value::String("uuid".to_string()));
props.insert("description".to_string(),
::serde_json::Value::String("UUID identifier string".to_string()));
}
} else if is_array_type(&field.ty)
|| (is_optional && is_array_type(get_option_inner_type(&field.ty)))
{
// For array types (including Option<Vec<T>>), we need to add the 'items' property
let actual_array_type =
if is_optional && is_array_type(get_option_inner_type(&field.ty)) {
get_option_inner_type(&field.ty)
} else {
&field.ty
};
if let Some(inner_type) = get_array_inner_type(actual_array_type) {
// Get the inner schema type
let inner_schema_type = get_schema_type_from_rust_type(inner_type);
// Extract inner type name for well-known library types only
let inner_type_name = get_type_name(inner_type);
// Check for well-known library types by exact match only
let is_datetime = matches!(
inner_type_name.as_deref(),
Some("DateTime") | Some("NaiveDateTime")
);
let is_date_only =
matches!(inner_type_name.as_deref(), Some("NaiveDate") | Some("Date"));
let is_uuid = matches!(inner_type_name.as_deref(), Some("Uuid"));
// Generate items schema
if is_datetime {
// Handle array of date-times
quote! {
// Create property for this array field with date-time items
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
// Add items schema for date-times
let mut items_schema = ::serde_json::Map::new();
items_schema.insert("type".to_string(), ::serde_json::Value::String("string".to_string()));
items_schema.insert("format".to_string(), ::serde_json::Value::String("date-time".to_string()));
items_schema.insert("description".to_string(),
::serde_json::Value::String("ISO-8601 formatted date and time".to_string()));
props.insert("items".to_string(), ::serde_json::Value::Object(items_schema));
}
} else if is_date_only {
// Handle array of date-only values
quote! {
// Create property for this array field with date items
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
// Add items schema for dates
let mut items_schema = ::serde_json::Map::new();
items_schema.insert("type".to_string(), ::serde_json::Value::String("string".to_string()));
items_schema.insert("format".to_string(), ::serde_json::Value::String("date".to_string()));
items_schema.insert("description".to_string(),
::serde_json::Value::String("ISO-8601 formatted date (YYYY-MM-DD)".to_string()));
props.insert("items".to_string(), ::serde_json::Value::Object(items_schema));
}
} else if is_uuid {
// Handle array of UUIDs
quote! {
// Create property for this array field with UUID items
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
// Add items schema for UUIDs
let mut items_schema = ::serde_json::Map::new();
items_schema.insert("type".to_string(), ::serde_json::Value::String("string".to_string()));
items_schema.insert("format".to_string(), ::serde_json::Value::String("uuid".to_string()));
items_schema.insert("description".to_string(),
::serde_json::Value::String("UUID identifier string".to_string()));
props.insert("items".to_string(), ::serde_json::Value::Object(items_schema));
}
} else if inner_schema_type == "object" {
// Check if this is a self-reference (recursive type)
let struct_name_str = name.to_string();
if is_self_reference(inner_type, &struct_name_str) {
// For self-referential types, use $ref to prevent infinite recursion
quote! {
// Create property for this array field with recursive reference
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
// Use $ref for recursive type
let mut items_schema = ::serde_json::Map::new();
items_schema.insert("$ref".to_string(), ::serde_json::Value::String(format!("#/$defs/{}", #struct_name_str)));
props.insert("items".to_string(), ::serde_json::Value::Object(items_schema));
}
} else {
// For arrays of nested structs, embed the inner type's schema directly
// This requires the inner type to implement SchemaType
quote! {
// Create property for this array field with nested struct items
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
// Get the inner type's schema directly
// This embeds the full schema with properties at compile time
let inner_schema = <#inner_type as ::rstructor::schema::SchemaType>::schema();
let items_schema = inner_schema.to_json();
props.insert("items".to_string(), items_schema);
}
}
} else {
// Standard handling for primitive types
quote! {
// Create property for this array field
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
// Add items schema
let mut items_schema = ::serde_json::Map::new();
items_schema.insert("type".to_string(), ::serde_json::Value::String(#inner_schema_type.to_string()));
props.insert("items".to_string(), ::serde_json::Value::Object(items_schema));
}
}
} else {
// Fallback for array without detectable item type
quote! {
// Create property for this array field (fallback)
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
// Add default items schema
let mut items_schema = ::serde_json::Map::new();
items_schema.insert("type".to_string(), ::serde_json::Value::String("string".to_string()));
props.insert("items".to_string(), ::serde_json::Value::Object(items_schema));
}
}
} else if is_json_value_type(&field.ty)
|| (is_optional && is_json_value_type(get_option_inner_type(&field.ty)))
{
// For serde_json::Value, use an empty schema (any JSON is valid)
quote! {
let mut props = ::serde_json::Map::new();
// Empty object schema means any JSON value is accepted
}
} else if is_map_type(&field.ty)
|| (is_optional && is_map_type(get_option_inner_type(&field.ty)))
{
// For HashMap<K, V> or BTreeMap<K, V>
let actual_type = if is_optional {
get_option_inner_type(&field.ty)
} else {
&field.ty
};
if let Some((key_ty, val_ty)) = get_map_types(actual_type) {
// Use SchemaType::schema() for all value types to get complete schema
// This ensures arrays get proper `items`, objects get properties, etc.
// For enum keys, extract the enum variants and add them to description
// so that Gemini can use the correct keys instead of generic placeholders
quote! {
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("object".to_string()));
let value_schema = <#val_ty as ::rstructor::schema::SchemaType>::schema();
props.insert("additionalProperties".to_string(), value_schema.to_json());
// Try to extract enum keys from key type schema (for enum keys)
let key_schema = <#key_ty as ::rstructor::schema::SchemaType>::schema();
if let Some(enum_values) = key_schema.to_json().get("enum")
.and_then(|e| e.as_array())
{
let keys: Vec<String> = enum_values
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect();
if !keys.is_empty() {
let keys_hint = format!("Keys: [{}]", keys.join(", "));
props.insert("description".to_string(), ::serde_json::Value::String(keys_hint));
// Also store enum keys as a structured extension field
// so backends can extract them without parsing the description
let keys_json: Vec<::serde_json::Value> = keys.iter()
.map(|k| ::serde_json::Value::String(k.clone()))
.collect();
props.insert("x-enum-keys".to_string(), ::serde_json::Value::Array(keys_json));
}
}
}
} else {
// Fallback for map without detectable types
quote! {
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("object".to_string()));
}
}
} else if is_box_type(&field.ty)
|| (is_optional && is_box_type(get_option_inner_type(&field.ty)))
{
// For Box<T>, unwrap and use the inner type's schema
let actual_type = if is_optional {
get_option_inner_type(&field.ty)
} else {
&field.ty
};
if let Some(inner_ty) = get_box_inner_type(actual_type) {
let inner_schema_type = get_schema_type_from_rust_type(inner_ty);
if inner_schema_type == "object" {
// Inner type is a complex type, use its schema
quote! {
let nested_schema = <#inner_ty as ::rstructor::schema::SchemaType>::schema();
let props_json = nested_schema.to_json();
let mut props = if let ::serde_json::Value::Object(m) = props_json {
m
} else {
let mut m = ::serde_json::Map::new();
m.insert("type".to_string(), ::serde_json::Value::String("object".to_string()));
m
};
}
} else {
// Inner type is a primitive
quote! {
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#inner_schema_type.to_string()));
}
}
} else {
// Fallback
quote! {
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("object".to_string()));
}
}
} else if is_tuple_type(&field.ty)
|| (is_optional && is_tuple_type(get_option_inner_type(&field.ty)))
{
// For tuples, generate array with prefixItems
let actual_type = if is_optional {
get_option_inner_type(&field.ty)
} else {
&field.ty
};
if let Some(element_types) = get_tuple_element_types(actual_type) {
let element_count = element_types.len();
// Generate schema for each element
let element_schemas: Vec<TokenStream> = element_types
.iter()
.map(|elem_ty| {
let elem_schema_type = get_schema_type_from_rust_type(elem_ty);
if elem_schema_type == "object" {
quote! {
<#elem_ty as ::rstructor::schema::SchemaType>::schema().to_json()
}
} else {
quote! {
::serde_json::json!({"type": #elem_schema_type})
}
}
})
.collect();
quote! {
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("array".to_string()));
let prefix_items = vec![
#(#element_schemas),*
];
props.insert("prefixItems".to_string(), ::serde_json::Value::Array(prefix_items));
props.insert("minItems".to_string(), ::serde_json::Value::Number(::serde_json::Number::from(#element_count)));
props.insert("maxItems".to_string(), ::serde_json::Value::Number(::serde_json::Number::from(#element_count)));
}
} else {
// Fallback for tuple without detectable elements
quote! {
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String("array".to_string()));
}
}
} else if type_name.is_some() && schema_type == "object" {
// For nested struct fields, embed the inner type's schema directly
// This requires the inner type to implement SchemaType
// If it's an Option<T>, unwrap to get T first
let actual_type = if is_optional {
get_option_inner_type(&field.ty)
} else {
&field.ty
};
quote! {
// Get the nested type's schema directly
let nested_schema = <#actual_type as ::rstructor::schema::SchemaType>::schema();
let props_json = nested_schema.to_json();
// Convert to a mutable map so we can add to it
let mut props = if let ::serde_json::Value::Object(m) = props_json {
m
} else {
let mut m = ::serde_json::Map::new();
m.insert("type".to_string(), ::serde_json::Value::String("object".to_string()));
m
};
}
} else {
// Regular primitive type
quote! {
// Create property for this field
let mut props = ::serde_json::Map::new();
props.insert("type".to_string(), ::serde_json::Value::String(#schema_type.to_string()));
}
};
property_setters.push(field_prop);
// Add description if available - merge with existing keys hint if present
if let Some(desc) = attrs.description {
let desc_prop = quote! {
if let Some(existing) = props.get("description").and_then(|v| v.as_str()) {
if existing.contains("Keys: [") {
// Merge user description with the keys hint
let merged = format!("{}. {}", #desc, existing);
props.insert("description".to_string(), ::serde_json::Value::String(merged));
} else {
props.insert("description".to_string(), ::serde_json::Value::String(#desc.to_string()));
}
} else {
props.insert("description".to_string(), ::serde_json::Value::String(#desc.to_string()));
}
};
property_setters.push(desc_prop);
}
// Add single example if available
if let Some(ex_val) = &attrs.example_value {
let ex_prop = quote! {
let example_value = #ex_val;
props.insert("example".to_string(), example_value);
};
property_setters.push(ex_prop);
}
// Add multiple examples if available
if !attrs.examples_array.is_empty() {
let examples_tokens = attrs.examples_array.iter().collect::<Vec<_>>();
let exs_prop = quote! {
let examples_value = ::serde_json::Value::Array(vec![
#(#examples_tokens),*
]);
props.insert("examples".to_string(), examples_value);
};
property_setters.push(exs_prop);
}
// Add the property to the schema
let add_prop = quote! {
// Add property to the schema
let props_val = ::serde_json::Value::Object(props);
if let ::serde_json::Value::Object(obj) = schema_obj.get_mut("properties").unwrap() {
obj.insert(#field_name.to_string(), props_val);
}
};
property_setters.push(add_prop);
// Add to required fields if not Optional type
if !is_optional {
let required_field = quote! {
required.push(::serde_json::Value::String(#field_name.to_string()));
};
required_setters.push(required_field);
}
}
}
_ => panic!("Instructor can only be derived for structs with named fields"),
}
// Handle container attributes
let mut container_setters = Vec::new();
// Description
if let Some(desc) = &container_attrs.description {
container_setters.push(quote! {
schema_obj["description"] = ::serde_json::Value::String(#desc.to_string());
});
}
// Title (override default)
if let Some(title) = &container_attrs.title {
container_setters.push(quote! {
schema_obj["title"] = ::serde_json::Value::String(#title.to_string());
});
}
// Examples
if !container_attrs.examples.is_empty() {
let examples_values = &container_attrs.examples;
container_setters.push(quote! {
let examples_array = vec![
#(#examples_values),*
];
schema_obj["examples"] = ::serde_json::Value::Array(examples_array);
});
}
// Combine all container attribute setters
let container_setter = if !container_setters.is_empty() {
quote! {
#(#container_setters)*
}
} else {
quote! {}
};
// Generate implementation with $defs support for recursive types
if has_self_reference {
quote! {
impl ::rstructor::schema::SchemaType for #name {
fn schema() -> ::rstructor::schema::Schema {
// Create base schema object (properties will be added to $defs)
let mut schema_obj = ::serde_json::json!({
"type": "object",
"title": stringify!(#name),
"properties": {}
});
// Add container attributes if available
#container_setter
// Fill properties
#(#property_setters)*
// Add required fields
let mut required = Vec::new();
#(#required_setters)*
schema_obj["required"] = ::serde_json::Value::Array(required);
// Create root schema with $defs for recursive types
let struct_name = stringify!(#name);
let root_schema = ::serde_json::json!({
"$defs": {
struct_name: schema_obj
},
"$ref": format!("#/$defs/{}", struct_name)
});
::rstructor::schema::Schema::new(root_schema)
}
fn schema_name() -> Option<String> {
Some(stringify!(#name).to_string())
}
}
}
} else {
quote! {
impl ::rstructor::schema::SchemaType for #name {
fn schema() -> ::rstructor::schema::Schema {
// Create base schema object
let mut schema_obj = ::serde_json::json!({
"type": "object",
"title": stringify!(#name),
"properties": {}
});
// Add container attributes if available
#container_setter
// Fill properties
#(#property_setters)*
// Add required fields
let mut required = Vec::new();
#(#required_setters)*
schema_obj["required"] = ::serde_json::Value::Array(required);
::rstructor::schema::Schema::new(schema_obj)
}
fn schema_name() -> Option<String> {
Some(stringify!(#name).to_string())
}
}
}
}
}
/// Apply serde rename_all transformation to a field/variant name
pub fn apply_rename_all(name: &str, rename_all: &str) -> String {
match rename_all {
"lowercase" => name.to_lowercase(),
"UPPERCASE" => name.to_uppercase(),
"camelCase" => {
// Convert snake_case to camelCase, or PascalCase to camelCase
if name.contains('_') {
// snake_case input
let parts: Vec<&str> = name.split('_').collect();
if parts.is_empty() {
name.to_string()
} else {
let mut result = parts[0].to_lowercase();
for part in &parts[1..] {
if !part.is_empty() {
let mut chars = part.chars();
if let Some(first) = chars.next() {
result.push(first.to_ascii_uppercase());
result.extend(chars.map(|c| c.to_ascii_lowercase()));
}
}
}
result
}
} else {
// PascalCase input - just lowercase the first char
let mut chars = name.chars();
match chars.next() {
Some(first) => first.to_ascii_lowercase().to_string() + chars.as_str(),
None => name.to_string(),
}
}
}
"PascalCase" => {
// Convert snake_case to PascalCase
let parts: Vec<&str> = name.split('_').collect();
let mut result = String::new();
for part in parts {
if !part.is_empty() {
let mut chars = part.chars();
if let Some(first) = chars.next() {
result.push(first.to_ascii_uppercase());
result.extend(chars);
}
}
}
result
}
"snake_case" => {
// Convert PascalCase/camelCase to snake_case
pascal_to_snake_case(name)
}
"SCREAMING_SNAKE_CASE" => {
// Convert to SCREAMING_SNAKE_CASE
pascal_to_snake_case(name).to_uppercase()
}
"kebab-case" => {
// Convert to kebab-case
pascal_to_snake_case(name).replace('_', "-")
}
"SCREAMING-KEBAB-CASE" => {
// Convert to SCREAMING-KEBAB-CASE
pascal_to_snake_case(name).to_uppercase().replace('_', "-")
}
_ => name.to_string(),
}
}
/// Convert PascalCase or camelCase to snake_case
fn pascal_to_snake_case(name: &str) -> String {
let mut result = String::new();
for (i, c) in name.chars().enumerate() {
if c.is_uppercase() && i > 0 {
result.push('_');
}
result.push(c.to_ascii_lowercase());
}
result
}