versatiles_derive 4.6.0

A toolbox for converting, checking and serving map tiles in various formats.
Documentation
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use syn::{Attribute, DataStruct, DeriveInput, Field, Fields, Meta};

/// Metadata for mapping a Rust type to its VPL parsing method and documentation.
struct TypeMapping {
	/// The type pattern as a string (e.g., "String", "Option<u8>")
	pattern: &'static str,
	/// Human-readable type name for documentation
	display_name: &'static str,
	/// The method name on VPLNode to call for parsing this type
	method_name: &'static str,
	/// Whether this is a required field (affects documentation formatting)
	is_required: bool,
	/// Optional generic type parameter (e.g., "u8" for property_number_option::<u8>)
	generic_param: Option<&'static str>,
	/// Optional second generic parameter (e.g., "4" for array lengths)
	generic_param2: Option<&'static str>,
	/// True if `generic_param` names a string-enum exposing `fn variants() ->
	/// &'static [&'static str]`. The derive emits a call to that method into
	/// `VPLFieldMeta::enum_variants`, which downstream codegen
	/// (`versatiles_node`) uses to synthesize TS string-literal unions.
	is_enum: bool,
}

/// All supported type mappings for VPLDecode.
const TYPE_MAPPINGS: &[TypeMapping] = &[
	// Required types
	TypeMapping {
		pattern: "String",
		display_name: "String",
		method_name: "property_string_required",
		is_required: true,
		generic_param: None,
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "bool",
		display_name: "Boolean",
		method_name: "property_bool_required",
		is_required: true,
		generic_param: None,
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "u8",
		display_name: "u8",
		method_name: "property_number_required",
		is_required: true,
		generic_param: Some("u8"),
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "[f64;4]",
		display_name: "[f64,f64,f64,f64]",
		method_name: "property_number_array_required",
		is_required: true,
		generic_param: Some("f64"),
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Vec<String>",
		display_name: "[String,...]",
		method_name: "property_string_list_required",
		is_required: true,
		generic_param: None,
		generic_param2: None,
		is_enum: false,
	},
	// Optional types
	TypeMapping {
		pattern: "Option<bool>",
		display_name: "bool",
		method_name: "property_bool_option",
		is_required: false,
		generic_param: None,
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<String>",
		display_name: "String",
		method_name: "property_string_option",
		is_required: false,
		generic_param: None,
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<f32>",
		display_name: "f32",
		method_name: "property_number_option",
		is_required: false,
		generic_param: Some("f32"),
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<u8>",
		display_name: "u8",
		method_name: "property_number_option",
		is_required: false,
		generic_param: Some("u8"),
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<u16>",
		display_name: "u16",
		method_name: "property_number_option",
		is_required: false,
		generic_param: Some("u16"),
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<u32>",
		display_name: "u32",
		method_name: "property_number_option",
		is_required: false,
		generic_param: Some("u32"),
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<f64>",
		display_name: "f64",
		method_name: "property_number_option",
		is_required: false,
		generic_param: Some("f64"),
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<[f64;3]>",
		display_name: "[f64,f64,f64]",
		method_name: "property_number_array_option",
		is_required: false,
		generic_param: Some("f64"),
		generic_param2: Some("3"),
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<[f64;4]>",
		display_name: "[f64,f64,f64,f64]",
		method_name: "property_number_array_option",
		is_required: false,
		generic_param: Some("f64"),
		generic_param2: Some("4"),
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<[u8;3]>",
		display_name: "[u8,u8,u8]",
		method_name: "property_number_array_option",
		is_required: false,
		generic_param: Some("u8"),
		generic_param2: Some("3"),
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<Vec<String>>",
		display_name: "[String,...]",
		method_name: "property_string_list_option",
		is_required: false,
		generic_param: None,
		generic_param2: None,
		is_enum: false,
	},
	TypeMapping {
		pattern: "Option<TileCompression>",
		display_name: "TileCompression",
		method_name: "property_enum_option",
		is_required: false,
		generic_param: Some("TileCompression"),
		generic_param2: None,
		is_enum: true,
	},
	TypeMapping {
		pattern: "Option<TileSchema>",
		display_name: "TileSchema",
		method_name: "property_enum_option",
		is_required: false,
		generic_param: Some("TileSchema"),
		generic_param2: None,
		is_enum: true,
	},
	TypeMapping {
		pattern: "Option<TileFormat>",
		display_name: "TileFormat",
		method_name: "property_enum_option",
		is_required: false,
		generic_param: Some("TileFormat"),
		generic_param2: None,
		is_enum: true,
	},
	TypeMapping {
		pattern: "Option<PointReductionStrategy>",
		display_name: "PointReductionStrategy",
		method_name: "property_enum_option",
		is_required: false,
		generic_param: Some("PointReductionStrategy"),
		generic_param2: None,
		is_enum: true,
	},
	TypeMapping {
		pattern: "Option<RasterTileFormat>",
		display_name: "RasterTileFormat",
		method_name: "property_enum_option",
		is_required: false,
		generic_param: Some("RasterTileFormat"),
		generic_param2: None,
		is_enum: true,
	},
];

/// Find a type mapping by its pattern string.
fn find_type_mapping(type_str: &str) -> Option<&'static TypeMapping> {
	TYPE_MAPPINGS.iter().find(|m| m.pattern == type_str)
}

/// Generate the list of supported types for error messages.
fn supported_types_list() -> String {
	TYPE_MAPPINGS.iter().map(|m| m.pattern).collect::<Vec<_>>().join(", ")
}

/// Extract a doc comment from an attribute, if present.
pub fn extract_comment(attr: &Attribute) -> Option<String> {
	if attr.path().is_ident("doc")
		&& let Meta::NameValue(meta) = &attr.meta
		&& let syn::Expr::Lit(lit) = &meta.value
		&& let syn::Lit::Str(lit_str) = &lit.lit
	{
		let text = lit_str.value().trim().to_string();
		return if text.is_empty() { None } else { Some(text) };
	}
	None
}

/// Extract doc comments from struct attributes, preserving blank lines so the
/// rendered markdown keeps headings and code fences legible. `extract_comment`
/// collapses blank `///` lines to `None`; here we promote them to empty strings
/// so `join("\n")` yields real paragraph breaks.
fn extract_struct_docs(attrs: &[Attribute]) -> String {
	attrs
		.iter()
		.filter_map(|a| {
			if a.path().is_ident("doc") {
				Some(extract_comment(a).unwrap_or_default())
			} else {
				None
			}
		})
		.collect::<Vec<String>>()
		.join("\n")
		.trim()
		.to_string()
}

/// Metadata for a single field, used for code generation.
struct FieldMeta {
	name: String,
	rust_type: String,
	is_required: bool,
	is_sources: bool,
	doc: String,
	/// `Some(generic_param)` when the field is a string-enum (i.e. its
	/// `TypeMapping::is_enum` is `true`); `None` otherwise. The derive
	/// emits `<T>::variants().to_vec()` into the generated metadata for
	/// these fields, where `T` is this generic param.
	enum_type: Option<&'static str>,
}

/// Processed field information returned by `process_field`.
enum ProcessedField {
	/// A regular property field with (doc_field, parser_field)
	Property { doc: String, parser: TokenStream },
	/// The special "sources" field with its doc string
	Sources { doc: String, parser: TokenStream },
}

/// Process a single struct field into parsing code.
fn process_field(field: &Field) -> Result<(String, ProcessedField, FieldMeta), syn::Error> {
	let field_name = &field.ident;
	let field_type = &field.ty;
	let field_str = field_name
		.as_ref()
		.ok_or_else(|| syn::Error::new_spanned(field, "field must have a name"))?
		.to_string();
	let field_type_str = quote!(#field_type).to_string().replace(' ', "");

	let raw_comment = field
		.attrs
		.iter()
		.filter_map(extract_comment)
		.collect::<Vec<String>>()
		.join(" ")
		.trim()
		.to_string();

	if field_str == "sources" {
		if field_type_str != "Vec<VPLPipeline>" {
			return Err(syn::Error::new_spanned(
				field_type,
				format!("type of 'sources' must be 'Vec<VPLPipeline>', but is '{field_type_str}'"),
			));
		}
		let doc = format!("### Sources\n\n{raw_comment}");
		let parser = quote! { sources: node.sources.clone() };
		let meta = FieldMeta {
			name: field_str.clone(),
			rust_type: field_type_str,
			is_required: true,
			is_sources: true,
			doc: raw_comment,
			enum_type: None,
		};
		return Ok((field_str, ProcessedField::Sources { doc, parser }, meta));
	}

	let mut comment = raw_comment.clone();
	if !comment.is_empty() {
		comment = format!(" - {comment}");
	}

	let Some(mapping) = find_type_mapping(&field_type_str) else {
		return Err(syn::Error::new_spanned(
			field_type,
			format!(
				"unsupported type `{}` for VPLDecode.\nSupported types: {}",
				field_type_str,
				supported_types_list()
			),
		));
	};

	let method = format_ident!("{}", mapping.method_name);

	// Build the method call with appropriate generic parameters
	let call = match (mapping.generic_param, mapping.generic_param2) {
		(Some(g1), Some(g2)) => {
			let g1_ident = format_ident!("{}", g1);
			let g2_lit: proc_macro2::TokenStream = g2.parse().unwrap();
			quote! { node.#method::<#g1_ident, #g2_lit>(#field_str)? }
		}
		(Some(g1), None) => {
			let g1_ident = format_ident!("{}", g1);
			quote! { node.#method::<#g1_ident>(#field_str)? }
		}
		(None, _) => {
			quote! { node.#method(#field_str)? }
		}
	};

	let doc = if mapping.is_required {
		format!("- **`{field_str}`: {} (required)**{comment}", mapping.display_name)
	} else {
		format!("- *`{field_str}`: {} (optional)*{comment}", mapping.display_name)
	};

	let meta = FieldMeta {
		name: field_str.clone(),
		rust_type: field_type_str,
		is_required: mapping.is_required,
		is_sources: false,
		doc: raw_comment,
		enum_type: if mapping.is_enum { mapping.generic_param } else { None },
	};

	Ok((
		field_str,
		ProcessedField::Property {
			doc: doc.trim().to_string(),
			parser: quote! { #field_name: #call },
		},
		meta,
	))
}

/// Build the final impl TokenStream for the struct.
fn build_impl_tokens(
	name: &Ident,
	field_names: &[String],
	parser_fields: &[TokenStream],
	doc: &str,
	field_metas: &[FieldMeta],
) -> TokenStream {
	let meta_entries: Vec<TokenStream> = field_metas
		.iter()
		.map(|m| {
			let fname = &m.name;
			let rtype = &m.rust_type;
			let required = m.is_required;
			let is_sources = m.is_sources;
			let fdoc = &m.doc;
			// For enum-typed fields, ask the enum itself for its accepted
			// variants — single source of truth, kept in sync with the
			// `TryFrom<&str>` impl by the enum's own round-trip test.
			let variants_expr: TokenStream = if let Some(enum_ty) = m.enum_type {
				let ty = format_ident!("{}", enum_ty);
				quote! { #ty::variants().to_vec() }
			} else {
				quote! { Vec::new() }
			};
			quote! {
				crate::vpl::VPLFieldMeta {
					name: #fname.to_string(),
					rust_type: #rtype.to_string(),
					is_required: #required,
					is_sources: #is_sources,
					doc: #fdoc.to_string(),
					enum_variants: #variants_expr,
				}
			}
		})
		.collect();

	quote! {
		impl #name {
			pub fn from_vpl_node(node: &VPLNode) -> Result<Self> {
				// scan node.property_names to ensure, that all properties are also defined in field_names
				let argument_names: Vec<String> = vec![#(#field_names.to_string()),*];
				let property_names = node.property_names();
				for property_name in property_names {
					if !argument_names.contains(&property_name) {
						anyhow::bail!(
							"The '{}' operation does not have a parameter '{}'.\nSupported parameters: '{}'",
							node.name,
							property_name,
							argument_names.join("', '")
						);
					}
				}

				Ok(Self {
					#(#parser_fields),*
				})
			}

			pub fn docs() -> String {
				#doc.to_string()
			}

			#[cfg(feature = "codegen")]
			pub fn field_metadata() -> Vec<crate::vpl::VPLFieldMeta> {
				vec![#(#meta_entries),*]
			}
		}
	}
}

/// Decode a struct definition into VPL parsing code.
///
/// Returns `Ok(TokenStream)` on success, or `Err(syn::Error)` if the struct
/// contains unsupported field types or is not a named-field struct.
pub fn decode_struct(input: DeriveInput, data_struct: DataStruct) -> Result<TokenStream, syn::Error> {
	let name = input.ident;
	let doc_struct = extract_struct_docs(&input.attrs);

	let fields = match data_struct.fields {
		Fields::Named(fields_named) => fields_named.named,
		_ => {
			return Err(syn::Error::new_spanned(
				&name,
				"VPLDecode can only be derived for structs with named fields",
			));
		}
	};

	let mut parser_fields: Vec<TokenStream> = Vec::new();
	let mut doc_fields: Vec<String> = Vec::new();
	let mut doc_sources: Option<String> = None;
	let mut field_names: Vec<String> = Vec::new();
	let mut field_metas: Vec<FieldMeta> = Vec::new();

	for field in fields {
		let (field_str, processed, meta) = process_field(&field)?;

		if field_str == "sources" && doc_sources.is_some() {
			return Err(syn::Error::new_spanned(
				&field.ident,
				"'sources' field is already defined",
			));
		}

		field_names.push(field_str);
		field_metas.push(meta);
		match processed {
			ProcessedField::Sources { doc, parser } => {
				doc_sources = Some(doc);
				parser_fields.push(parser);
			}
			ProcessedField::Property { doc, parser } => {
				doc_fields.push(doc);
				parser_fields.push(parser);
			}
		}
	}

	let doc_fields_str = if doc_fields.is_empty() {
		String::new()
	} else {
		format!("### Parameters\n\n{}", doc_fields.join("\n"))
	};

	let doc = vec![doc_struct, doc_sources.unwrap_or_default(), doc_fields_str]
		.into_iter()
		.filter(|s| !s.is_empty())
		.collect::<Vec<String>>()
		.join("\n\n")
		.trim()
		.to_string();

	Ok(build_impl_tokens(
		&name,
		&field_names,
		&parser_fields,
		&doc,
		&field_metas,
	))
}

#[cfg(test)]
mod tests {
	use super::decode_struct;
	use syn::{DeriveInput, parse_quote};

	fn pretty_tokens(ts: &proc_macro2::TokenStream) -> Vec<String> {
		prettyplease::unparse(&syn::parse_file(&ts.to_string()).unwrap())
			.split('\n')
			.map(std::string::ToString::to_string)
			.collect()
	}

	#[test]
	fn test_decode_struct_simple() {
		// Simple struct with one String field
		let input: DeriveInput = parse_quote!(
			/// Struct documentation
			struct Test {
				#[doc = "Field documentation"]
				field1: String,
			}
		);
		let data_struct = match &input.data {
			syn::Data::Struct(ds) => ds.clone(),
			_ => panic!("Expected struct data"),
		};
		let ts = decode_struct(input.clone(), data_struct).unwrap();
		let code = pretty_tokens(&ts).join("\n");
		assert!(code.contains("pub fn from_vpl_node(node: &VPLNode) -> Result<Self>"));
		assert!(code.contains("field1: node.property_string_required(\"field1\")?"));
		assert!(code.contains("pub fn docs() -> String"));
		assert!(code.contains("Struct documentation"));
		assert!(code.contains("Field documentation"));
		assert!(code.contains("pub fn field_metadata()"));
		assert!(code.contains("\"field1\".to_string()"));
		assert!(code.contains("\"String\""));
		let code_no_spaces = code.replace(' ', "");
		assert!(code_no_spaces.contains("is_required:true"));
		assert!(code_no_spaces.contains("is_sources:false"));
	}

	/// Helper to verify decode_struct output for a single-field struct.
	fn assert_field_type_decodes(input: DeriveInput, getter: &str, comment: &str) {
		let data_struct = match &input.data {
			syn::Data::Struct(ds) => ds.clone(),
			_ => panic!("Expected struct data"),
		};
		let ts = decode_struct(input, data_struct).unwrap();
		let code = pretty_tokens(&ts).join("\n");
		assert!(
			code.contains(&format!("v: node.{getter}(\"v\")?")),
			"missing getter {getter}"
		);
		assert!(code.contains(comment), "missing comment {comment}");
		assert!(code.contains("pub fn field_metadata() -> Vec<crate::vpl::VPLFieldMeta>"));
	}

	#[test]
	fn test_decode_struct_required_types() {
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: String,
				}
			),
			"property_string_required",
			"**`v`: String (required)**",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: bool,
				}
			),
			"property_bool_required",
			"**`v`: Boolean (required)**",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: u8,
				}
			),
			"property_number_required::<u8>",
			"**`v`: u8 (required)**",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: [f64; 4],
				}
			),
			"property_number_array_required::<f64>",
			"**`v`: [f64,f64,f64,f64] (required)**",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Vec<String>,
				}
			),
			"property_string_list_required",
			"**`v`: [String,...] (required)**",
		);
	}

	#[test]
	fn test_decode_struct_optional_types() {
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<bool>,
				}
			),
			"property_bool_option",
			"*`v`: bool (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<String>,
				}
			),
			"property_string_option",
			"*`v`: String (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<f32>,
				}
			),
			"property_number_option::<f32>",
			"*`v`: f32 (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<u8>,
				}
			),
			"property_number_option::<u8>",
			"*`v`: u8 (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<u32>,
				}
			),
			"property_number_option::<u32>",
			"*`v`: u32 (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<f64>,
				}
			),
			"property_number_option::<f64>",
			"*`v`: f64 (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<[f64; 4]>,
				}
			),
			"property_number_array_option::<f64, 4>",
			"*`v`: [f64,f64,f64,f64] (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<TileFormat>,
				}
			),
			"property_enum_option::<TileFormat>",
			"*`v`: TileFormat (optional)*",
		);
		assert_field_type_decodes(
			parse_quote!(
				struct T {
					v: Option<Vec<String>>,
				}
			),
			"property_string_list_option",
			"*`v`: [String,...] (optional)*",
		);
	}

	#[test]
	fn test_decode_struct_with_sources() {
		// Struct with sources field
		let input: DeriveInput = parse_quote!(
			/// Top-level doc
			struct Pipeline {
				#[doc = "List of sources"]
				sources: Vec<VPLPipeline>,
			}
		);
		let data_struct = match &input.data {
			syn::Data::Struct(ds) => ds.clone(),
			_ => panic!("Expected struct data"),
		};
		let ts = decode_struct(input.clone(), data_struct).unwrap();
		let code = ts.to_string();
		// Ensure docs includes Sources section
		assert!(code.contains("### Sources"));
		assert!(code.contains("List of sources"));
	}

	#[test]
	fn test_decode_struct_unsupported_type_error() {
		// Struct with unsupported type should return error
		let input: DeriveInput = parse_quote!(
			struct T {
				v: i128,
			}
		);
		let data_struct = match &input.data {
			syn::Data::Struct(ds) => ds.clone(),
			_ => panic!("Expected struct data"),
		};
		let result = decode_struct(input, data_struct);
		assert!(result.is_err());
		let err = result.unwrap_err();
		let msg = err.to_string();
		assert!(msg.contains("unsupported type"));
		assert!(msg.contains("i128"));
	}

	#[test]
	fn test_field_metadata_generated() {
		let input: DeriveInput = parse_quote!(
			/// MyStruct docs
			struct MyStruct {
				/// Required field_name
				field_name: String,
				/// Optional level
				level: Option<u8>,
				/// Sources list
				sources: Vec<VPLPipeline>,
			}
		);
		let data_struct = match &input.data {
			syn::Data::Struct(ds) => ds.clone(),
			_ => panic!("Expected struct data"),
		};
		let ts = decode_struct(input, data_struct).unwrap();
		let code = pretty_tokens(&ts).join("\n");

		// Verify field_metadata method is generated
		assert!(code.contains("pub fn field_metadata()"));

		// Verify field entries exist (prettyplease uses "key : value" formatting)
		assert!(code.contains("\"field_name\".to_string()"));
		assert!(code.contains("\"String\""));

		assert!(code.contains("\"level\".to_string()"));
		assert!(code.contains("\"Option<u8>\""));

		assert!(code.contains("\"sources\".to_string()"));
		assert!(code.contains("\"Vec<VPLPipeline>\""));
		let code_no_spaces = code.replace(' ', "");
		assert!(code_no_spaces.contains("is_sources:true"));
	}
}