reinhardt-admin 0.4.0-alpha.10

Admin panel functionality for Reinhardt framework
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
//! Model information types

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Presentation style for an inline related-model form.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InlineStyle {
	/// Render child rows in a compact table.
	Tabular,
	/// Render each child row as a labelled group.
	Stacked,
}

/// Values for one existing or blank inline child row.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InlineRowInfo {
	/// Existing child primary key, or `None` for an extra row.
	pub id: Option<String>,
	/// Editable child values keyed by configured field name.
	pub values: HashMap<String, serde_json::Value>,
}

/// Form schema and rows for one configured inline child model.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InlineFormInfo {
	/// Stable inline identifier used in control names.
	pub key: String,
	/// Child model display name.
	pub model_name: String,
	/// Inline presentation style.
	pub style: InlineStyle,
	/// Editable child field schema.
	pub fields: Vec<FieldInfo>,
	/// Existing rows followed by configured blank rows.
	pub rows: Vec<InlineRowInfo>,
	/// Whether existing rows may be changed; blank rows remain addable when false.
	#[serde(default)]
	pub can_change: bool,
	/// Whether existing rows may expose delete controls.
	pub can_delete: bool,
}

/// Relation control rendered for a foreign-key field.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RelationWidget {
	/// Searchable autocomplete control.
	Autocomplete,
	/// Direct primary-key input with a resolved label.
	RawId,
}

/// Display-safe option returned by a relation lookup.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RelationOption {
	/// Related object's primary key.
	pub id: String,
	/// Related object's display label.
	pub label: String,
}

impl RelationOption {
	/// Create a relation option.
	pub fn new(id: impl Into<String>, label: impl Into<String>) -> Self {
		Self {
			id: id.into(),
			label: label.into(),
		}
	}
}

/// Permission required to perform an admin action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelPermission {
	/// Permission to view model instances.
	View,
	/// Permission to add model instances.
	Add,
	/// Permission to change model instances.
	Change,
	/// Permission to delete model instances.
	Delete,
}

/// Metadata for an action available on an admin model.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AdminAction {
	/// Machine-readable action name.
	pub name: String,
	/// Human-readable action label.
	pub label: String,
	/// Permission required to execute the action.
	pub permission: ModelPermission,
	/// Whether the action requires user confirmation.
	pub requires_confirmation: bool,
}

impl AdminAction {
	/// Creates metadata for an admin action.
	pub fn new(
		name: impl Into<String>,
		label: impl Into<String>,
		permission: ModelPermission,
		requires_confirmation: bool,
	) -> Self {
		Self {
			name: name.into(),
			label: label.into(),
			permission,
			requires_confirmation,
		}
	}
}

/// Result of executing an admin action.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AdminActionOutcome {
	/// Canonical, deterministic, duplicate-free IDs changed by the action.
	pub successful_ids: Vec<String>,
	/// Total number of rows affected by the action.
	pub affected: u64,
}

impl AdminActionOutcome {
	/// Creates an admin action outcome.
	pub fn new(successful_ids: Vec<String>, affected: u64) -> Self {
		Self {
			successful_ids,
			affected,
		}
	}
}

/// A selected position within a date hierarchy.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DateHierarchySelection {
	/// Selected year.
	pub year: Option<i32>,
	/// Selected month within [`Self::year`].
	pub month: Option<u32>,
	/// Selected day within [`Self::month`].
	pub day: Option<u32>,
}

/// The next date hierarchy granularity available for selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DateHierarchyLevel {
	/// Select a year.
	Year,
	/// Select a month.
	Month,
	/// Select a day.
	Day,
}

/// Date hierarchy metadata included with a changelist response.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DateHierarchyInfo {
	/// Date or datetime field used for the hierarchy.
	pub field: String,
	/// Currently selected hierarchy position.
	pub selection: DateHierarchySelection,
	/// The next level the client may select.
	pub next_level: Option<DateHierarchyLevel>,
	/// Available values for [`Self::next_level`].
	pub choices: Vec<i32>,
}

/// Layout used to render a many-to-many relation selector.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RelationSelectorLayout {
	/// Side-by-side available and selected lists.
	Horizontal,
	/// Stacked available and selected lists.
	Vertical,
}

/// Closed widget configuration for an admin form field.
///
/// Runtime relation choices and selected values are resolved separately from
/// this declarative configuration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AdminWidget {
	/// Single-line text input.
	TextInput,
	/// Email input.
	EmailInput,
	/// Numeric input.
	NumberInput,
	/// Boolean checkbox.
	Checkbox,
	/// Date input.
	DateInput,
	/// Local date and time input.
	DateTimeInput,
	/// Multi-line text input.
	TextArea {
		/// Optional number of visible rows.
		rows: Option<u16>,
	},
	/// Single-value selection from explicit choices.
	Select {
		/// Available `(value, label)` choices.
		choices: Vec<(String, String)>,
	},
	/// Multiple-value selection from explicit choices.
	MultiSelect {
		/// Available `(value, label)` choices.
		choices: Vec<(String, String)>,
	},
	/// Permission-aware relation autocomplete control.
	Autocomplete,
	/// Direct relation primary-key control.
	RawId,
	/// Many-to-many relation selector.
	ManyToMany {
		/// Selector layout.
		layout: RelationSelectorLayout,
	},
	/// File upload input.
	FileInput,
	/// Hidden input.
	HiddenInput,
}

/// Optional presentation and requiredness overrides for one admin form field.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FormFieldOverride {
	/// Configured field name.
	pub field: String,
	/// Optional replacement widget.
	pub widget: Option<AdminWidget>,
	/// Optional display label.
	pub label: Option<String>,
	/// Optional help text.
	pub help_text: Option<String>,
	/// Optional input placeholder.
	pub placeholder: Option<String>,
	/// Optional requiredness override.
	pub required: Option<bool>,
}

impl FormFieldOverride {
	/// Create an empty override for a field.
	pub fn new(field: impl Into<String>) -> Self {
		Self {
			field: field.into(),
			widget: None,
			label: None,
			help_text: None,
			placeholder: None,
			required: None,
		}
	}

	/// Set the replacement widget.
	pub fn widget(mut self, widget: AdminWidget) -> Self {
		self.widget = Some(widget);
		self
	}

	/// Set the display label.
	pub fn label(mut self, label: impl Into<String>) -> Self {
		self.label = Some(label.into());
		self
	}

	/// Set the help text.
	pub fn help_text(mut self, help_text: impl Into<String>) -> Self {
		self.help_text = Some(help_text.into());
		self
	}

	/// Set the input placeholder.
	pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
		self.placeholder = Some(placeholder.into());
		self
	}

	/// Set the requiredness override.
	pub fn required(mut self, required: bool) -> Self {
		self.required = Some(required);
		self
	}
}

/// Client-side prepopulation rule for an admin form field.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PrepopulatedField {
	/// Field populated from the sources.
	pub target: String,
	/// Source fields in concatenation order.
	pub sources: Vec<String>,
}

impl PrepopulatedField {
	/// Create a prepopulation rule without validating metadata-dependent names.
	pub fn new<I, S>(target: impl Into<String>, sources: I) -> Self
	where
		I: IntoIterator<Item = S>,
		S: Into<String>,
	{
		Self {
			target: target.into(),
			sources: sources.into_iter().map(Into::into).collect(),
		}
	}
}

/// Model information for dashboard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
	/// Model name
	pub name: String,
	/// List URL
	pub list_url: String,
}

/// Field metadata for dynamic form generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldInfo {
	/// Field name (e.g., "username", "email")
	pub name: String,
	/// Display label (e.g., "Username", "Email Address")
	pub label: String,
	/// Field type
	pub field_type: FieldType,
	/// Whether the field is required
	pub required: bool,
	/// Whether the field accepts an explicit null or clear value.
	#[serde(default)]
	pub nullable: bool,
	/// Whether the field is readonly
	pub readonly: bool,
	/// Help text displayed below the field
	pub help_text: Option<String>,
	/// Placeholder text for input
	pub placeholder: Option<String>,
}

/// A titled group of fields in an admin form.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Fieldset {
	/// Optional heading displayed for the group.
	pub title: Option<String>,
	/// Field names included in the group.
	pub fields: Vec<String>,
	/// Whether the group is collapsed initially.
	#[serde(default)]
	pub collapsed: bool,
}

impl Fieldset {
	/// Create an expanded fieldset from field names.
	pub fn new(title: Option<&str>, fields: &[&str]) -> Self {
		Self {
			title: title.map(String::from),
			fields: fields.iter().map(|field| String::from(*field)).collect(),
			collapsed: false,
		}
	}

	/// Mark the fieldset as initially collapsed.
	pub fn collapsed(mut self) -> Self {
		self.collapsed = true;
		self
	}
}

/// Field type for form rendering
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", content = "options")]
pub enum FieldType {
	/// Text input (single line)
	Text,
	/// Textarea (multi-line)
	TextArea,
	/// Textarea (multi-line) with an explicit visible row count.
	///
	/// This public enum expansion is intentional: downstream exhaustive matches
	/// must handle this variant when supporting configured textarea rows.
	TextAreaWithRows {
		/// Optional number of visible rows.
		rows: Option<u16>,
	},
	/// Number input
	Number,
	/// Boolean checkbox
	Boolean,
	/// Email input
	Email,
	/// Date input
	Date,
	/// DateTime input
	DateTime,
	/// Select dropdown with choices.
	Select {
		/// Available choices as `(value, label)` pairs.
		choices: Vec<(String, String)>,
	},
	/// Multiple select.
	MultiSelect {
		/// Available choices as `(value, label)` pairs.
		choices: Vec<(String, String)>,
	},
	/// Many-to-many relation selector.
	ManyToManySelector {
		/// Selector layout.
		layout: RelationSelectorLayout,
		/// Options available for selection.
		available: Vec<RelationOption>,
		/// Currently selected options.
		selected: Vec<RelationOption>,
		/// Last page consumed while prefetching available options.
		page: u64,
		/// Whether more available options can be loaded.
		has_more: bool,
	},
	/// Permission-aware foreign-key relation control.
	Relation {
		/// Logical relation field name from the model.
		field_name: String,
		/// Relation control kind.
		widget: RelationWidget,
		/// Current related option for edit forms.
		selected: Option<RelationOption>,
		/// Whether the relation is displayed without a submitted control.
		#[serde(default)]
		readonly: bool,
	},
	/// File upload
	File,
	/// Hidden field
	Hidden,
}

/// Rendering specification for a form field.
///
/// This type preserves the structural information needed to emit the
/// correct HTML element (e.g., `<input>`, `<textarea>`, `<select>`),
/// along with any choices required for `<select>` options. It is derived
/// from `FieldType` via `From<&FieldType>`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "data")]
pub enum FormFieldSpec {
	/// Plain `<input>` element with the given HTML `type` attribute.
	Input {
		/// Value for the HTML `type` attribute (e.g., "text", "email",
		/// "number", "checkbox", "date", "datetime-local").
		///
		/// Owned `String` (not `&'static str`) so the variant can round-trip
		/// through `serde` deserialization at API boundaries — borrowed
		/// `'static` strings cannot be reconstructed from incoming JSON.
		html_type: String,
	},
	/// `<textarea>` element for multi-line text.
	TextArea,
	/// `<textarea>` element with an explicit visible row count.
	///
	/// This public enum expansion is intentional: downstream exhaustive matches
	/// must handle this variant when supporting configured textarea rows.
	TextAreaWithRows {
		/// Optional number of visible rows.
		rows: Option<u16>,
	},
	/// `<textarea>` element whose value is encoded as structured JSON.
	Json,
	/// `<select>` dropdown with the given `(value, label)` choices.
	Select {
		/// Available choices as `(value, label)` pairs.
		choices: Vec<(String, String)>,
	},
	/// `<select multiple>` dropdown with the given `(value, label)` choices.
	MultiSelect {
		/// Available choices as `(value, label)` pairs.
		choices: Vec<(String, String)>,
	},
	/// Many-to-many relation selector.
	ManyToManySelector {
		/// Selector layout.
		layout: RelationSelectorLayout,
		/// Options available for selection.
		available: Vec<RelationOption>,
		/// Currently selected options.
		selected: Vec<RelationOption>,
		/// Last page consumed while prefetching available options.
		page: u64,
		/// Whether more available options can be loaded.
		has_more: bool,
	},
	/// Permission-aware foreign-key relation control.
	Relation {
		/// Logical relation field name from the model.
		field_name: String,
		/// Relation control kind.
		widget: RelationWidget,
		/// Current related option for edit forms.
		selected: Option<RelationOption>,
		/// Whether the relation is displayed without a submitted control.
		#[serde(default)]
		readonly: bool,
	},
	/// `<input type="file">` for file uploads.
	File,
	/// `<input type="hidden">` for hidden values.
	Hidden,
}

impl From<&FieldType> for FormFieldSpec {
	fn from(field_type: &FieldType) -> Self {
		match field_type {
			FieldType::Text => FormFieldSpec::Input {
				html_type: "text".to_string(),
			},
			FieldType::Number => FormFieldSpec::Input {
				html_type: "number".to_string(),
			},
			FieldType::Boolean => FormFieldSpec::Input {
				html_type: "checkbox".to_string(),
			},
			FieldType::Email => FormFieldSpec::Input {
				html_type: "email".to_string(),
			},
			FieldType::Date => FormFieldSpec::Input {
				html_type: "date".to_string(),
			},
			FieldType::DateTime => FormFieldSpec::Input {
				html_type: "datetime-local".to_string(),
			},
			FieldType::TextArea => FormFieldSpec::TextArea,
			FieldType::TextAreaWithRows { rows } => FormFieldSpec::TextAreaWithRows { rows: *rows },
			FieldType::Select { choices } => FormFieldSpec::Select {
				choices: choices.clone(),
			},
			FieldType::MultiSelect { choices } => FormFieldSpec::MultiSelect {
				choices: choices.clone(),
			},
			FieldType::ManyToManySelector {
				layout,
				available,
				selected,
				page,
				has_more,
			} => FormFieldSpec::ManyToManySelector {
				layout: *layout,
				available: available.clone(),
				selected: selected.clone(),
				page: *page,
				has_more: *has_more,
			},
			FieldType::Relation {
				field_name,
				widget,
				selected,
				readonly,
			} => FormFieldSpec::Relation {
				field_name: field_name.clone(),
				widget: *widget,
				selected: selected.clone(),
				readonly: *readonly,
			},
			FieldType::File => FormFieldSpec::File,
			FieldType::Hidden => FormFieldSpec::Hidden,
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use serde_json::json;

	#[test]
	fn textarea_unit_variants_keep_the_legacy_wire_shape() {
		assert_eq!(
			serde_json::to_value(FieldType::TextArea).expect("field type should serialize"),
			json!({"type": "TextArea"})
		);
		assert_eq!(
			serde_json::from_value::<FieldType>(json!({"type": "TextArea"}))
				.expect("legacy field type should deserialize"),
			FieldType::TextArea
		);
		assert_eq!(
			serde_json::to_value(FormFieldSpec::TextArea).expect("form spec should serialize"),
			json!({"kind": "TextArea"})
		);
		assert_eq!(
			serde_json::from_value::<FormFieldSpec>(json!({"kind": "TextArea"}))
				.expect("legacy form spec should deserialize"),
			FormFieldSpec::TextArea
		);
	}

	#[test]
	fn many_to_many_selector_conversion_preserves_selector_data() {
		let field_type = FieldType::ManyToManySelector {
			layout: RelationSelectorLayout::Horizontal,
			available: vec![RelationOption::new("1", "Rust")],
			selected: vec![RelationOption::new("2", "WebAssembly")],
			page: 3,
			has_more: true,
		};

		assert_eq!(
			FormFieldSpec::from(&field_type),
			FormFieldSpec::ManyToManySelector {
				layout: RelationSelectorLayout::Horizontal,
				available: vec![RelationOption::new("1", "Rust")],
				selected: vec![RelationOption::new("2", "WebAssembly")],
				page: 3,
				has_more: true,
			}
		);
	}
}

/// Filter type for UI rendering
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "options")]
pub enum FilterType {
	/// Boolean filter (Yes/No checkbox)
	Boolean,
	/// Choice filter (dropdown with predefined options).
	Choice {
		/// Available filter choices.
		choices: Vec<FilterChoice>,
	},
	/// Date range filter (predefined ranges like "Today", "Last 7 days").
	DateRange {
		/// Available date range options.
		ranges: Vec<FilterChoice>,
	},
	/// Number range filter (predefined ranges).
	NumberRange {
		/// Available number range options.
		ranges: Vec<FilterChoice>,
	},
}

/// Filter choice for Choice/DateRange/NumberRange filters
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FilterChoice {
	/// Value to send to API
	pub value: String,
	/// Display label for UI
	pub label: String,
}

/// Filter metadata sent from backend to frontend
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterInfo {
	/// Field name (e.g., "status", "is_active")
	pub field: String,
	/// Display title (e.g., "Status", "Active")
	pub title: String,
	/// Filter type and options
	pub filter_type: FilterType,
	/// Current value (if filter is active)
	pub current_value: Option<String>,
}

/// Column metadata for list view display
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnInfo {
	/// Field name to extract from data
	pub field: String,
	/// Display label for column header
	pub label: String,
	/// Whether column is sortable
	pub sortable: bool,
	/// Whether the column can be edited directly in the list view.
	#[serde(default)]
	pub editable: bool,
	/// Whether the column links to the row detail view.
	#[serde(default)]
	pub linked: bool,
	/// Whether an editable value is required.
	#[serde(default)]
	pub required: bool,
	/// Whether an editable value accepts the database NULL state.
	#[serde(default)]
	pub nullable: bool,
	/// Optional HTML numeric step for editable controls.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub step: Option<String>,
	/// Input rendering specification for editable columns.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub form_spec: Option<FormFieldSpec>,
}

#[cfg(all(test, server))]
mod relation_tests {
	use super::*;
	use rstest::rstest;
	use serde_json::json;

	#[rstest]
	#[case(RelationWidget::Autocomplete, json!("autocomplete"))]
	#[case(RelationWidget::RawId, json!("raw_id"))]
	fn relation_widget_uses_stable_wire_values(
		#[case] widget: RelationWidget,
		#[case] expected: serde_json::Value,
	) {
		// Act
		let serialized = serde_json::to_value(widget).expect("relation widget should serialize");

		// Assert
		assert_eq!(serialized, expected);
	}

	#[rstest]
	fn relation_option_round_trips() {
		// Arrange
		let option = RelationOption {
			id: "42".to_string(),
			label: "Ada Lovelace".to_string(),
		};

		// Act
		let serialized = serde_json::to_value(&option).expect("relation option should serialize");
		let deserialized: RelationOption =
			serde_json::from_value(serialized.clone()).expect("relation option should deserialize");

		// Assert
		assert_eq!(serialized, json!({"id": "42", "label": "Ada Lovelace"}));
		assert_eq!(deserialized, option);
	}

	#[rstest]
	fn relation_field_type_serializes_and_converts_without_losing_metadata() {
		// Arrange
		let selected = RelationOption {
			id: "7".to_string(),
			label: "Grace Hopper".to_string(),
		};
		let field_type = FieldType::Relation {
			field_name: "author".to_string(),
			widget: RelationWidget::Autocomplete,
			selected: Some(selected.clone()),
			readonly: false,
		};

		// Act
		let serialized =
			serde_json::to_value(&field_type).expect("relation field type should serialize");
		let form_spec = FormFieldSpec::from(&field_type);

		// Assert
		assert_eq!(
			serialized,
			json!({
				"type": "Relation",
				"options": {
					"field_name": "author",
					"widget": "autocomplete",
					"selected": {"id": "7", "label": "Grace Hopper"},
					"readonly": false
				}
			})
		);
		assert_eq!(
			form_spec,
			FormFieldSpec::Relation {
				field_name: "author".to_string(),
				widget: RelationWidget::Autocomplete,
				selected: Some(selected),
				readonly: false,
			}
		);
	}
}