facet-format 0.42.0

Core Serializer/Deserializer traits for facet's next-generation format architecture with JIT compilation support
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
extern crate alloc;

use alloc::borrow::Cow;
use alloc::string::String;
use core::fmt::Debug;
use core::fmt::Write as _;

use facet_core::{Def, DynDateTimeKind, DynValueKind, ScalarType, StructKind};
use facet_reflect::{HasFields as _, Peek, ReflectError};

use crate::ScalarValue;

/// Field ordering preference for serialization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FieldOrdering {
    /// Fields are serialized in declaration order (default for JSON, etc.)
    #[default]
    Declaration,
    /// Attributes first, then elements, then text (for XML)
    AttributesFirst,
}

/// How struct fields should be serialized.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StructFieldMode {
    /// Serialize fields with names/keys (default for text formats).
    #[default]
    Named,
    /// Serialize fields in declaration order without names (binary formats).
    Unnamed,
}

/// How map-like values should be serialized.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MapEncoding {
    /// Serialize maps as objects/structs with string keys.
    #[default]
    Struct,
    /// Serialize maps as key/value pairs (binary formats).
    Pairs,
}

/// How enum variants should be serialized.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EnumVariantEncoding {
    /// Serialize enums using tag/field-name strategies (default for text formats).
    #[default]
    Tagged,
    /// Serialize enums using a numeric variant index followed by fields (binary formats).
    Index,
}

/// How dynamic values (e.g. `facet_value::Value`) should be encoded.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DynamicValueEncoding {
    /// Use the format's native self-describing encoding (default for JSON, MsgPack, etc.).
    #[default]
    SelfDescribing,
    /// Use an explicit type tag before the dynamic value payload (binary formats).
    Tagged,
}

/// Tag describing the concrete payload type for a dynamic value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DynamicValueTag {
    /// Null value.
    Null,
    /// Boolean value.
    Bool,
    /// Signed 64-bit integer.
    I64,
    /// Unsigned 64-bit integer.
    U64,
    /// 64-bit float.
    F64,
    /// UTF-8 string.
    String,
    /// Raw bytes.
    Bytes,
    /// Sequence/array.
    Array,
    /// Object/map.
    Object,
    /// Date/time value (encoded as string for tagged formats).
    DateTime,
}

/// Low-level serializer interface implemented by each format backend.
///
/// This is intentionally event-ish: the shared serializer logic owns traversal
/// (struct/enum/seq decisions), while formats own representation details.
pub trait FormatSerializer {
    /// Format-specific error type.
    type Error: Debug;

    /// Begin a map/object/struct.
    fn begin_struct(&mut self) -> Result<(), Self::Error>;
    /// Emit a field key within a struct.
    fn field_key(&mut self, key: &str) -> Result<(), Self::Error>;
    /// End a map/object/struct.
    fn end_struct(&mut self) -> Result<(), Self::Error>;

    /// Begin a sequence/array.
    fn begin_seq(&mut self) -> Result<(), Self::Error>;
    /// End a sequence/array.
    fn end_seq(&mut self) -> Result<(), Self::Error>;

    /// Emit a scalar value.
    fn scalar(&mut self, scalar: ScalarValue<'_>) -> Result<(), Self::Error>;

    /// Optional: Provide field metadata before field_key is called.
    /// This allows formats like XML to extract namespace information.
    /// Default implementation does nothing.
    fn field_metadata(&mut self, _field: &facet_reflect::FieldItem) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Optional: Provide struct/enum type metadata when beginning to serialize it.
    /// This allows formats to extract container-level attributes like xml::ns_all.
    /// Default implementation does nothing.
    fn struct_metadata(&mut self, _shape: &facet_core::Shape) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Optional: Provide variant metadata before serializing an enum variant.
    /// This allows formats like XML to use the variant name as the element name
    /// for xml::elements serialization.
    /// Default implementation does nothing.
    fn variant_metadata(
        &mut self,
        _variant: &'static facet_core::Variant,
    ) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Preferred field ordering for this format.
    /// Formats like XML can request attributes-first ordering to avoid buffering.
    /// Default is declaration order.
    fn preferred_field_order(&self) -> FieldOrdering {
        FieldOrdering::Declaration
    }

    /// Preferred struct field mode for this format.
    fn struct_field_mode(&self) -> StructFieldMode {
        StructFieldMode::Named
    }

    /// Preferred map encoding for this format.
    fn map_encoding(&self) -> MapEncoding {
        MapEncoding::Struct
    }

    /// Preferred enum variant encoding for this format.
    fn enum_variant_encoding(&self) -> EnumVariantEncoding {
        EnumVariantEncoding::Tagged
    }

    /// Preferred dynamic value encoding for this format.
    fn dynamic_value_encoding(&self) -> DynamicValueEncoding {
        DynamicValueEncoding::SelfDescribing
    }

    /// Returns the shape of the format's raw capture type for serialization.
    ///
    /// When serializing a value whose shape matches this, the serializer will
    /// extract the inner string and call [`FormatSerializer::raw_scalar`] instead of normal
    /// serialization.
    fn raw_serialize_shape(&self) -> Option<&'static facet_core::Shape> {
        None
    }

    /// Emit a raw scalar value (for RawJson, etc.) without any encoding/escaping.
    ///
    /// The content is the format-specific raw representation that should be
    /// output directly.
    fn raw_scalar(&mut self, content: &str) -> Result<(), Self::Error> {
        // Default: treat as a regular string (formats should override this)
        self.scalar(ScalarValue::Str(Cow::Borrowed(content)))
    }

    /// Serialize an opaque scalar type with a format-specific encoding.
    ///
    /// Returns `Ok(true)` if handled, `Ok(false)` to fall back to standard logic.
    fn serialize_opaque_scalar(
        &mut self,
        _shape: &'static facet_core::Shape,
        _value: Peek<'_, '_>,
    ) -> Result<bool, Self::Error> {
        Ok(false)
    }

    /// Emit a dynamic value type tag.
    ///
    /// Formats that use [`DynamicValueEncoding::Tagged`] should override this.
    /// Self-describing formats can ignore it.
    fn dynamic_value_tag(&mut self, _tag: DynamicValueTag) -> Result<(), Self::Error> {
        Ok(())
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Binary format support methods
    //
    // The following methods enable proper serialization for binary formats like
    // postcard that need length prefixes, type-precise encoding, and explicit
    // discriminants. All have default implementations for backward compatibility
    // with existing text-format serializers.
    // ─────────────────────────────────────────────────────────────────────────

    /// Begin a sequence with known length.
    ///
    /// Binary formats (postcard, msgpack) can use this to write a length prefix
    /// before the elements. Text formats can ignore the length and just call
    /// `begin_seq()`.
    ///
    /// Default: delegates to `begin_seq()`.
    fn begin_seq_with_len(&mut self, _len: usize) -> Result<(), Self::Error> {
        self.begin_seq()
    }

    /// Begin serializing a map with known length.
    ///
    /// Default: delegates to `begin_struct()` for formats that encode maps as objects.
    fn begin_map_with_len(&mut self, _len: usize) -> Result<(), Self::Error> {
        self.begin_struct()
    }

    /// End a map/object/struct.
    ///
    /// Default: delegates to `end_struct()`.
    fn end_map(&mut self) -> Result<(), Self::Error> {
        self.end_struct()
    }

    /// Serialize a scalar with full type information.
    ///
    /// Binary formats need to encode different integer sizes differently:
    /// - postcard: u8 as raw byte, u16+ as varint, signed use zigzag
    /// - msgpack: different tags for different sizes
    ///
    /// Text formats can ignore the type and use the normalized `ScalarValue`.
    ///
    /// Default: normalizes to `ScalarValue` and calls `scalar()`.
    fn typed_scalar(
        &mut self,
        scalar_type: ScalarType,
        value: Peek<'_, '_>,
    ) -> Result<(), Self::Error> {
        // Default implementation: normalize to ScalarValue and call scalar()
        let scalar = match scalar_type {
            ScalarType::Unit => ScalarValue::Null,
            ScalarType::Bool => ScalarValue::Bool(*value.get::<bool>().unwrap()),
            ScalarType::Char => ScalarValue::Char(*value.get::<char>().unwrap()),
            ScalarType::Str | ScalarType::String | ScalarType::CowStr => {
                ScalarValue::Str(Cow::Borrowed(value.as_str().unwrap()))
            }
            ScalarType::F32 => ScalarValue::F64(*value.get::<f32>().unwrap() as f64),
            ScalarType::F64 => ScalarValue::F64(*value.get::<f64>().unwrap()),
            ScalarType::U8 => ScalarValue::U64(*value.get::<u8>().unwrap() as u64),
            ScalarType::U16 => ScalarValue::U64(*value.get::<u16>().unwrap() as u64),
            ScalarType::U32 => ScalarValue::U64(*value.get::<u32>().unwrap() as u64),
            ScalarType::U64 => ScalarValue::U64(*value.get::<u64>().unwrap()),
            ScalarType::U128 => {
                let n = *value.get::<u128>().unwrap();
                ScalarValue::Str(Cow::Owned(alloc::string::ToString::to_string(&n)))
            }
            ScalarType::USize => ScalarValue::U64(*value.get::<usize>().unwrap() as u64),
            ScalarType::I8 => ScalarValue::I64(*value.get::<i8>().unwrap() as i64),
            ScalarType::I16 => ScalarValue::I64(*value.get::<i16>().unwrap() as i64),
            ScalarType::I32 => ScalarValue::I64(*value.get::<i32>().unwrap() as i64),
            ScalarType::I64 => ScalarValue::I64(*value.get::<i64>().unwrap()),
            ScalarType::I128 => {
                let n = *value.get::<i128>().unwrap();
                ScalarValue::Str(Cow::Owned(alloc::string::ToString::to_string(&n)))
            }
            ScalarType::ISize => ScalarValue::I64(*value.get::<isize>().unwrap() as i64),
            #[cfg(feature = "net")]
            ScalarType::IpAddr => {
                let addr = *value.get::<core::net::IpAddr>().unwrap();
                ScalarValue::Str(Cow::Owned(alloc::string::ToString::to_string(&addr)))
            }
            #[cfg(feature = "net")]
            ScalarType::Ipv4Addr => {
                let addr = *value.get::<core::net::Ipv4Addr>().unwrap();
                ScalarValue::Str(Cow::Owned(alloc::string::ToString::to_string(&addr)))
            }
            #[cfg(feature = "net")]
            ScalarType::Ipv6Addr => {
                let addr = *value.get::<core::net::Ipv6Addr>().unwrap();
                ScalarValue::Str(Cow::Owned(alloc::string::ToString::to_string(&addr)))
            }
            #[cfg(feature = "net")]
            ScalarType::SocketAddr => {
                let addr = *value.get::<core::net::SocketAddr>().unwrap();
                ScalarValue::Str(Cow::Owned(alloc::string::ToString::to_string(&addr)))
            }
            _ => {
                // For unknown scalar types, try to get a string representation
                if let Some(s) = value.as_str() {
                    ScalarValue::Str(Cow::Borrowed(s))
                } else {
                    ScalarValue::Null
                }
            }
        };
        self.scalar(scalar)
    }

    /// Begin serializing `Option::Some(value)`.
    ///
    /// Binary formats like postcard write a `0x01` discriminant byte here.
    /// Text formats typically don't need a prefix (they just serialize the value).
    ///
    /// Default: no-op (text formats).
    fn begin_option_some(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }

    /// Serialize `Option::None`.
    ///
    /// Binary formats like postcard write a `0x00` discriminant byte.
    /// Text formats typically emit `null`.
    ///
    /// Default: emits `ScalarValue::Null`.
    fn serialize_none(&mut self) -> Result<(), Self::Error> {
        self.scalar(ScalarValue::Null)
    }

    /// Begin an enum variant with its index and name.
    ///
    /// Binary formats like postcard write the variant index as a varint.
    /// Text formats typically use the variant name as a key or value.
    ///
    /// This is called for externally tagged enums before the variant payload.
    /// For untagged enums, this is not called.
    ///
    /// Default: no-op (text formats handle variants via field_key/scalar).
    fn begin_enum_variant(
        &mut self,
        _variant_index: usize,
        _variant_name: &'static str,
    ) -> Result<(), Self::Error> {
        Ok(())
    }
}

/// Error produced by the shared serializer.
#[derive(Debug)]
pub enum SerializeError<E: Debug> {
    /// Format backend error.
    Backend(E),
    /// Reflection failed while traversing the value.
    Reflect(ReflectError),
    /// Value can't be represented by the shared serializer.
    Unsupported(Cow<'static, str>),
    /// Internal invariant violation.
    Internal(Cow<'static, str>),
}

impl<E: Debug> core::fmt::Display for SerializeError<E> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            SerializeError::Backend(_) => f.write_str("format serializer error"),
            SerializeError::Reflect(err) => write!(f, "{err}"),
            SerializeError::Unsupported(msg) => f.write_str(msg.as_ref()),
            SerializeError::Internal(msg) => f.write_str(msg.as_ref()),
        }
    }
}

impl<E: Debug> std::error::Error for SerializeError<E> {}

/// Serialize a root value using the shared traversal logic.
pub fn serialize_root<'mem, 'facet, S>(
    serializer: &mut S,
    value: Peek<'mem, 'facet>,
) -> Result<(), SerializeError<S::Error>>
where
    S: FormatSerializer,
{
    shared_serialize(serializer, value)
}

/// Helper to sort fields according to format preference
fn sort_fields_if_needed<'mem, 'facet, S>(
    serializer: &S,
    fields: &mut alloc::vec::Vec<(facet_reflect::FieldItem, Peek<'mem, 'facet>)>,
) where
    S: FormatSerializer,
{
    if serializer.preferred_field_order() == FieldOrdering::AttributesFirst {
        fields.sort_by_key(|(field_item, _)| {
            // Determine field category: 0=attribute, 1=element, 2=text
            // For flattened map entries (field is None), treat as attributes
            match &field_item.field {
                Some(field) if field.is_attribute() => 0, // attributes first
                Some(field) if field.is_text() => 2,      // text last
                None => 0,                                // flattened map entries are attributes
                _ => 1,                                   // elements in the middle
            }
        });
    }
}

fn shared_serialize<'mem, 'facet, S>(
    serializer: &mut S,
    value: Peek<'mem, 'facet>,
) -> Result<(), SerializeError<S::Error>>
where
    S: FormatSerializer,
{
    // Dereference pointers (Box, Arc, etc.) to get the underlying value
    let value = deref_if_pointer(value);

    // Check for raw serialization type (e.g., RawJson) BEFORE innermost_peek
    // because innermost_peek might unwrap the type if it has .inner set
    if serializer.raw_serialize_shape() == Some(value.shape()) {
        // RawJson is a tuple struct with a single Cow<str> field
        // Get the inner Cow<str> value via as_str on the inner
        if let Ok(struct_) = value.into_struct()
            && let Some((_field_item, inner_value)) = struct_.fields_for_serialize().next()
            && let Some(s) = inner_value.as_str()
        {
            return serializer.raw_scalar(s).map_err(SerializeError::Backend);
        }
        // If we get here, the raw shape matched but extraction failed
        // This shouldn't happen for properly implemented raw types
        return Err(SerializeError::Unsupported(Cow::Borrowed(
            "raw capture type matched but could not extract inner string",
        )));
    }

    if serializer
        .serialize_opaque_scalar(value.shape(), value)
        .map_err(SerializeError::Backend)?
    {
        return Ok(());
    }

    let value = value.innermost_peek();

    // Check for container-level proxy - serialize through the proxy type
    if let Some(proxy_def) = value.shape().proxy {
        return serialize_via_proxy(serializer, value, proxy_def);
    }

    // Use typed_scalar for scalars - allows binary formats to encode precisely
    if let Some(scalar_type) = value.scalar_type() {
        return serializer
            .typed_scalar(scalar_type, value)
            .map_err(SerializeError::Backend);
    }

    // Fallback for Def::Scalar types with Display trait (e.g., SmolStr, SmartString, CompactString)
    // These are string-like types that should serialize as strings
    if matches!(value.shape().def, Def::Scalar) && value.shape().vtable.has_display() {
        use alloc::string::ToString;
        let formatted = value.to_string();
        return serializer
            .scalar(ScalarValue::Str(Cow::Owned(formatted)))
            .map_err(SerializeError::Backend);
    }

    // Handle Option<T> - use begin_option_some/serialize_none for binary formats
    if let Ok(opt) = value.into_option() {
        return match opt.value() {
            Some(inner) => {
                serializer
                    .begin_option_some()
                    .map_err(SerializeError::Backend)?;
                shared_serialize(serializer, inner)
            }
            None => serializer.serialize_none().map_err(SerializeError::Backend),
        };
    }

    if let Ok(result) = value.into_result() {
        let (variant_index, variant_name, inner) = if result.is_ok() {
            (
                0,
                "Ok",
                result.ok().ok_or(SerializeError::Internal(Cow::Borrowed(
                    "result reported Ok but value was missing",
                )))?,
            )
        } else {
            (
                1,
                "Err",
                result.err().ok_or(SerializeError::Internal(Cow::Borrowed(
                    "result reported Err but value was missing",
                )))?,
            )
        };

        if serializer.enum_variant_encoding() == EnumVariantEncoding::Index {
            serializer
                .begin_enum_variant(variant_index, variant_name)
                .map_err(SerializeError::Backend)?;
            return shared_serialize(serializer, inner);
        }

        // Externally tagged representation for non-index encodings.
        serializer.begin_struct().map_err(SerializeError::Backend)?;
        serializer
            .field_key(variant_name)
            .map_err(SerializeError::Backend)?;
        shared_serialize(serializer, inner)?;
        serializer.end_struct().map_err(SerializeError::Backend)?;
        return Ok(());
    }

    if let Ok(dynamic) = value.into_dynamic_value() {
        return serialize_dynamic_value(serializer, dynamic);
    }

    match value.shape().def {
        facet_core::Def::List(_) | facet_core::Def::Array(_) | facet_core::Def::Slice(_) => {
            let list = value.into_list_like().map_err(SerializeError::Reflect)?;
            let len = list.len();
            match value.shape().def {
                facet_core::Def::Array(_) => {
                    serializer.begin_seq().map_err(SerializeError::Backend)?
                }
                _ => serializer
                    .begin_seq_with_len(len)
                    .map_err(SerializeError::Backend)?,
            };
            for item in list.iter() {
                shared_serialize(serializer, item)?;
            }
            serializer.end_seq().map_err(SerializeError::Backend)?;
            return Ok(());
        }
        _ => {}
    }

    if let Ok(map) = value.into_map() {
        let len = map.len();
        match serializer.map_encoding() {
            MapEncoding::Pairs => {
                serializer
                    .begin_map_with_len(len)
                    .map_err(SerializeError::Backend)?;
                for (key, val) in map.iter() {
                    shared_serialize(serializer, key)?;
                    shared_serialize(serializer, val)?;
                }
                serializer.end_map().map_err(SerializeError::Backend)?;
            }
            MapEncoding::Struct => {
                serializer.begin_struct().map_err(SerializeError::Backend)?;
                for (key, val) in map.iter() {
                    // Convert the key to a string for the field name
                    let key_str = if let Some(s) = key.as_str() {
                        Cow::Borrowed(s)
                    } else {
                        // For non-string keys, use Display format (not Debug, which adds quotes)
                        Cow::Owned(alloc::format!("{}", key))
                    };
                    serializer
                        .field_key(&key_str)
                        .map_err(SerializeError::Backend)?;
                    shared_serialize(serializer, val)?;
                }
                serializer.end_struct().map_err(SerializeError::Backend)?;
            }
        }
        return Ok(());
    }

    if let Ok(set) = value.into_set() {
        // Use begin_seq_with_len for binary formats that need length prefixes
        let len = set.len();
        serializer
            .begin_seq_with_len(len)
            .map_err(SerializeError::Backend)?;
        for item in set.iter() {
            shared_serialize(serializer, item)?;
        }
        serializer.end_seq().map_err(SerializeError::Backend)?;
        return Ok(());
    }

    if let Ok(struct_) = value.into_struct() {
        let kind = struct_.ty().kind;
        if kind == StructKind::Tuple || kind == StructKind::TupleStruct {
            // Serialize tuples as arrays without length prefixes
            let fields: alloc::vec::Vec<_> = struct_.fields_for_serialize().collect();
            serializer.begin_seq().map_err(SerializeError::Backend)?;
            for (field_item, field_value) in fields {
                // Check for field-level proxy
                if let Some(proxy_def) = field_item.field.and_then(|f| f.proxy()) {
                    serialize_via_proxy(serializer, field_value, proxy_def)?;
                } else {
                    shared_serialize(serializer, field_value)?;
                }
            }
            serializer.end_seq().map_err(SerializeError::Backend)?;
        } else {
            // Regular structs as objects
            serializer
                .struct_metadata(value.shape())
                .map_err(SerializeError::Backend)?;
            serializer.begin_struct().map_err(SerializeError::Backend)?;

            // Collect fields and sort according to format preference
            let mut fields: alloc::vec::Vec<_> = struct_.fields_for_serialize().collect();
            sort_fields_if_needed(serializer, &mut fields);
            let field_mode = serializer.struct_field_mode();

            for (field_item, field_value) in fields {
                serializer
                    .field_metadata(&field_item)
                    .map_err(SerializeError::Backend)?;
                if field_mode == StructFieldMode::Named {
                    serializer
                        .field_key(&field_item.name)
                        .map_err(SerializeError::Backend)?;
                }
                // Check for field-level proxy
                if let Some(proxy_def) = field_item.field.and_then(|f| f.proxy()) {
                    serialize_via_proxy(serializer, field_value, proxy_def)?;
                } else {
                    shared_serialize(serializer, field_value)?;
                }
            }
            serializer.end_struct().map_err(SerializeError::Backend)?;
        }
        return Ok(());
    }

    if let Ok(enum_) = value.into_enum() {
        let variant = enum_.active_variant().map_err(|_| {
            SerializeError::Unsupported(Cow::Borrowed("opaque enum layout is unsupported"))
        })?;

        // Notify format of the variant being serialized (for xml::elements support)
        serializer
            .variant_metadata(variant)
            .map_err(SerializeError::Backend)?;

        if serializer.enum_variant_encoding() == EnumVariantEncoding::Index {
            let variant_index = enum_.variant_index().map_err(|_| {
                SerializeError::Unsupported(Cow::Borrowed("opaque enum layout is unsupported"))
            })?;
            serializer
                .begin_enum_variant(variant_index, variant.name)
                .map_err(SerializeError::Backend)?;

            match variant.data.kind {
                StructKind::Unit => return Ok(()),
                StructKind::TupleStruct | StructKind::Tuple | StructKind::Struct => {
                    for (field_item, field_value) in enum_.fields_for_serialize() {
                        if let Some(proxy_def) = field_item.field.and_then(|f| f.proxy()) {
                            serialize_via_proxy(serializer, field_value, proxy_def)?;
                        } else {
                            shared_serialize(serializer, field_value)?;
                        }
                    }
                    return Ok(());
                }
            }
        }

        let numeric = value.shape().is_numeric();
        let untagged = value.shape().is_untagged();
        let tag = value.shape().get_tag_attr();
        let content = value.shape().get_content_attr();

        if numeric {
            return serialize_numeric_enum(serializer, variant);
        }
        if untagged {
            return serialize_untagged_enum(serializer, enum_, variant);
        }

        match (tag, content) {
            (Some(tag_key), None) => {
                // Internally tagged.
                serializer.begin_struct().map_err(SerializeError::Backend)?;
                serializer
                    .field_key(tag_key)
                    .map_err(SerializeError::Backend)?;
                serializer
                    .scalar(ScalarValue::Str(Cow::Borrowed(variant.name)))
                    .map_err(SerializeError::Backend)?;

                match variant.data.kind {
                    StructKind::Unit => {}
                    StructKind::Struct => {
                        let mut fields: alloc::vec::Vec<_> = enum_.fields_for_serialize().collect();
                        sort_fields_if_needed(serializer, &mut fields);
                        let field_mode = serializer.struct_field_mode();
                        for (field_item, field_value) in fields {
                            serializer
                                .field_metadata(&field_item)
                                .map_err(SerializeError::Backend)?;
                            if field_mode == StructFieldMode::Named {
                                serializer
                                    .field_key(&field_item.name)
                                    .map_err(SerializeError::Backend)?;
                            }
                            // Check for field-level proxy
                            if let Some(proxy_def) = field_item.field.and_then(|f| f.proxy()) {
                                serialize_via_proxy(serializer, field_value, proxy_def)?;
                            } else {
                                shared_serialize(serializer, field_value)?;
                            }
                        }
                    }
                    StructKind::TupleStruct | StructKind::Tuple => {
                        return Err(SerializeError::Unsupported(Cow::Borrowed(
                            "internally tagged tuple variants are not supported",
                        )));
                    }
                }

                serializer.end_struct().map_err(SerializeError::Backend)?;
                return Ok(());
            }
            (Some(tag_key), Some(content_key)) => {
                // Adjacently tagged.
                serializer.begin_struct().map_err(SerializeError::Backend)?;
                serializer
                    .field_key(tag_key)
                    .map_err(SerializeError::Backend)?;
                serializer
                    .scalar(ScalarValue::Str(Cow::Borrowed(variant.name)))
                    .map_err(SerializeError::Backend)?;

                match variant.data.kind {
                    StructKind::Unit => {
                        // Unit variants with adjacent tagging omit the content field.
                    }
                    StructKind::Struct => {
                        serializer
                            .field_key(content_key)
                            .map_err(SerializeError::Backend)?;
                        serializer.begin_struct().map_err(SerializeError::Backend)?;
                        let mut fields: alloc::vec::Vec<_> = enum_.fields_for_serialize().collect();
                        sort_fields_if_needed(serializer, &mut fields);
                        let field_mode = serializer.struct_field_mode();
                        for (field_item, field_value) in fields {
                            serializer
                                .field_metadata(&field_item)
                                .map_err(SerializeError::Backend)?;
                            if field_mode == StructFieldMode::Named {
                                serializer
                                    .field_key(&field_item.name)
                                    .map_err(SerializeError::Backend)?;
                            }
                            // Check for field-level proxy
                            if let Some(proxy_def) = field_item.field.and_then(|f| f.proxy()) {
                                serialize_via_proxy(serializer, field_value, proxy_def)?;
                            } else {
                                shared_serialize(serializer, field_value)?;
                            }
                        }
                        serializer.end_struct().map_err(SerializeError::Backend)?;
                    }
                    StructKind::TupleStruct | StructKind::Tuple => {
                        serializer
                            .field_key(content_key)
                            .map_err(SerializeError::Backend)?;

                        let field_count = variant.data.fields.len();
                        if field_count == 1 {
                            let inner = enum_
                                .field(0)
                                .map_err(|_| {
                                    SerializeError::Internal(Cow::Borrowed(
                                        "variant field lookup failed",
                                    ))
                                })?
                                .ok_or(SerializeError::Internal(Cow::Borrowed(
                                    "variant reported 1 field but field(0) returned None",
                                )))?;
                            shared_serialize(serializer, inner)?;
                        } else {
                            serializer.begin_seq().map_err(SerializeError::Backend)?;
                            for idx in 0..field_count {
                                let inner = enum_
                                    .field(idx)
                                    .map_err(|_| {
                                        SerializeError::Internal(Cow::Borrowed(
                                            "variant field lookup failed",
                                        ))
                                    })?
                                    .ok_or(SerializeError::Internal(Cow::Borrowed(
                                        "variant field missing while iterating tuple fields",
                                    )))?;
                                shared_serialize(serializer, inner)?;
                            }
                            serializer.end_seq().map_err(SerializeError::Backend)?;
                        }
                    }
                }

                serializer.end_struct().map_err(SerializeError::Backend)?;
                return Ok(());
            }
            (None, Some(_)) => {
                return Err(SerializeError::Unsupported(Cow::Borrowed(
                    "adjacent content key set without tag key",
                )));
            }
            (None, None) => {}
        }

        // Externally tagged (default).
        return match variant.data.kind {
            StructKind::Unit => {
                serializer
                    .scalar(ScalarValue::Str(Cow::Borrowed(variant.name)))
                    .map_err(SerializeError::Backend)?;
                Ok(())
            }
            StructKind::TupleStruct | StructKind::Tuple => {
                serializer.begin_struct().map_err(SerializeError::Backend)?;
                serializer
                    .field_key(variant.name)
                    .map_err(SerializeError::Backend)?;

                let field_count = variant.data.fields.len();
                if field_count == 1 {
                    let inner = enum_
                        .field(0)
                        .map_err(|_| {
                            SerializeError::Internal(Cow::Borrowed("variant field lookup failed"))
                        })?
                        .ok_or(SerializeError::Internal(Cow::Borrowed(
                            "variant reported 1 field but field(0) returned None",
                        )))?;
                    shared_serialize(serializer, inner)?;
                } else {
                    serializer.begin_seq().map_err(SerializeError::Backend)?;
                    for idx in 0..field_count {
                        let inner = enum_
                            .field(idx)
                            .map_err(|_| {
                                SerializeError::Internal(Cow::Borrowed(
                                    "variant field lookup failed",
                                ))
                            })?
                            .ok_or(SerializeError::Internal(Cow::Borrowed(
                                "variant field missing while iterating tuple fields",
                            )))?;
                        shared_serialize(serializer, inner)?;
                    }
                    serializer.end_seq().map_err(SerializeError::Backend)?;
                }

                serializer.end_struct().map_err(SerializeError::Backend)?;
                Ok(())
            }
            StructKind::Struct => {
                serializer.begin_struct().map_err(SerializeError::Backend)?;
                serializer
                    .field_key(variant.name)
                    .map_err(SerializeError::Backend)?;

                serializer.begin_struct().map_err(SerializeError::Backend)?;
                let mut fields: alloc::vec::Vec<_> = enum_.fields_for_serialize().collect();
                sort_fields_if_needed(serializer, &mut fields);
                let field_mode = serializer.struct_field_mode();
                for (field_item, field_value) in fields {
                    serializer
                        .field_metadata(&field_item)
                        .map_err(SerializeError::Backend)?;
                    if field_mode == StructFieldMode::Named {
                        serializer
                            .field_key(&field_item.name)
                            .map_err(SerializeError::Backend)?;
                    }
                    // Check for field-level proxy
                    if let Some(proxy_def) = field_item.field.and_then(|f| f.proxy()) {
                        serialize_via_proxy(serializer, field_value, proxy_def)?;
                    } else {
                        shared_serialize(serializer, field_value)?;
                    }
                }
                serializer.end_struct().map_err(SerializeError::Backend)?;

                serializer.end_struct().map_err(SerializeError::Backend)?;
                Ok(())
            }
        };
    }

    Err(SerializeError::Unsupported(Cow::Borrowed(
        "unsupported value kind for serialization",
    )))
}

fn serialize_dynamic_value<'mem, 'facet, S>(
    serializer: &mut S,
    dynamic: facet_reflect::PeekDynamicValue<'mem, 'facet>,
) -> Result<(), SerializeError<S::Error>>
where
    S: FormatSerializer,
{
    let tagged = serializer.dynamic_value_encoding() == DynamicValueEncoding::Tagged;

    match dynamic.kind() {
        DynValueKind::Null => {
            if tagged {
                serializer
                    .dynamic_value_tag(DynamicValueTag::Null)
                    .map_err(SerializeError::Backend)?;
            }
            serializer
                .scalar(ScalarValue::Null)
                .map_err(SerializeError::Backend)
        }
        DynValueKind::Bool => {
            let value = dynamic.as_bool().ok_or_else(|| {
                SerializeError::Internal(Cow::Borrowed("dynamic bool missing value"))
            })?;
            if tagged {
                serializer
                    .dynamic_value_tag(DynamicValueTag::Bool)
                    .map_err(SerializeError::Backend)?;
            }
            serializer
                .scalar(ScalarValue::Bool(value))
                .map_err(SerializeError::Backend)
        }
        DynValueKind::Number => {
            if let Some(n) = dynamic.as_i64() {
                if tagged {
                    serializer
                        .dynamic_value_tag(DynamicValueTag::I64)
                        .map_err(SerializeError::Backend)?;
                }
                serializer
                    .scalar(ScalarValue::I64(n))
                    .map_err(SerializeError::Backend)
            } else if let Some(n) = dynamic.as_u64() {
                if tagged {
                    serializer
                        .dynamic_value_tag(DynamicValueTag::U64)
                        .map_err(SerializeError::Backend)?;
                }
                serializer
                    .scalar(ScalarValue::U64(n))
                    .map_err(SerializeError::Backend)
            } else if let Some(n) = dynamic.as_f64() {
                if tagged {
                    serializer
                        .dynamic_value_tag(DynamicValueTag::F64)
                        .map_err(SerializeError::Backend)?;
                }
                serializer
                    .scalar(ScalarValue::F64(n))
                    .map_err(SerializeError::Backend)
            } else {
                Err(SerializeError::Unsupported(Cow::Borrowed(
                    "dynamic number not representable",
                )))
            }
        }
        DynValueKind::String => {
            let value = dynamic.as_str().ok_or_else(|| {
                SerializeError::Internal(Cow::Borrowed("dynamic string missing value"))
            })?;
            if tagged {
                serializer
                    .dynamic_value_tag(DynamicValueTag::String)
                    .map_err(SerializeError::Backend)?;
            }
            serializer
                .scalar(ScalarValue::Str(Cow::Borrowed(value)))
                .map_err(SerializeError::Backend)
        }
        DynValueKind::Bytes => {
            let value = dynamic.as_bytes().ok_or_else(|| {
                SerializeError::Internal(Cow::Borrowed("dynamic bytes missing value"))
            })?;
            if tagged {
                serializer
                    .dynamic_value_tag(DynamicValueTag::Bytes)
                    .map_err(SerializeError::Backend)?;
            }
            serializer
                .scalar(ScalarValue::Bytes(Cow::Borrowed(value)))
                .map_err(SerializeError::Backend)
        }
        DynValueKind::Array => {
            let len = dynamic.array_len().ok_or_else(|| {
                SerializeError::Internal(Cow::Borrowed("dynamic array missing length"))
            })?;
            if tagged {
                serializer
                    .dynamic_value_tag(DynamicValueTag::Array)
                    .map_err(SerializeError::Backend)?;
            }
            serializer
                .begin_seq_with_len(len)
                .map_err(SerializeError::Backend)?;
            if let Some(iter) = dynamic.array_iter() {
                for item in iter {
                    shared_serialize(serializer, item)?;
                }
            }
            serializer.end_seq().map_err(SerializeError::Backend)
        }
        DynValueKind::Object => {
            let len = dynamic.object_len().ok_or_else(|| {
                SerializeError::Internal(Cow::Borrowed("dynamic object missing length"))
            })?;
            if tagged {
                serializer
                    .dynamic_value_tag(DynamicValueTag::Object)
                    .map_err(SerializeError::Backend)?;
            }
            match serializer.map_encoding() {
                MapEncoding::Pairs => {
                    serializer
                        .begin_map_with_len(len)
                        .map_err(SerializeError::Backend)?;
                    if let Some(iter) = dynamic.object_iter() {
                        for (key, value) in iter {
                            serializer
                                .scalar(ScalarValue::Str(Cow::Borrowed(key)))
                                .map_err(SerializeError::Backend)?;
                            shared_serialize(serializer, value)?;
                        }
                    }
                    serializer.end_map().map_err(SerializeError::Backend)
                }
                MapEncoding::Struct => {
                    serializer.begin_struct().map_err(SerializeError::Backend)?;
                    if let Some(iter) = dynamic.object_iter() {
                        for (key, value) in iter {
                            serializer.field_key(key).map_err(SerializeError::Backend)?;
                            shared_serialize(serializer, value)?;
                        }
                    }
                    serializer.end_struct().map_err(SerializeError::Backend)
                }
            }
        }
        DynValueKind::DateTime => {
            let dt = dynamic.as_datetime().ok_or_else(|| {
                SerializeError::Internal(Cow::Borrowed("dynamic datetime missing value"))
            })?;
            if tagged {
                serializer
                    .dynamic_value_tag(DynamicValueTag::DateTime)
                    .map_err(SerializeError::Backend)?;
            }
            let s = format_dyn_datetime(dt);
            serializer
                .scalar(ScalarValue::Str(Cow::Owned(s)))
                .map_err(SerializeError::Backend)
        }
        DynValueKind::QName | DynValueKind::Uuid => Err(SerializeError::Unsupported(
            Cow::Borrowed("dynamic QName/Uuid serialization is not supported"),
        )),
    }
}

fn format_dyn_datetime(
    (year, month, day, hour, minute, second, nanos, kind): (
        i32,
        u8,
        u8,
        u8,
        u8,
        u8,
        u32,
        DynDateTimeKind,
    ),
) -> String {
    let mut out = String::new();
    match kind {
        DynDateTimeKind::Offset { offset_minutes } => {
            let _ = write!(
                out,
                "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}",
                year, month, day, hour, minute, second
            );
            if nanos > 0 {
                let _ = write!(out, ".{:09}", nanos);
            }
            if offset_minutes == 0 {
                out.push('Z');
            } else {
                let sign = if offset_minutes >= 0 { '+' } else { '-' };
                let abs = offset_minutes.unsigned_abs();
                let _ = write!(out, "{}{:02}:{:02}", sign, abs / 60, abs % 60);
            }
        }
        DynDateTimeKind::LocalDateTime => {
            let _ = write!(
                out,
                "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}",
                year, month, day, hour, minute, second
            );
            if nanos > 0 {
                let _ = write!(out, ".{:09}", nanos);
            }
        }
        DynDateTimeKind::LocalDate => {
            let _ = write!(out, "{:04}-{:02}-{:02}", year, month, day);
        }
        DynDateTimeKind::LocalTime => {
            let _ = write!(out, "{:02}:{:02}:{:02}", hour, minute, second);
            if nanos > 0 {
                let _ = write!(out, ".{:09}", nanos);
            }
        }
    }
    out
}

fn serialize_numeric_enum<S>(
    serializer: &mut S,
    variant: &'static facet_core::Variant,
) -> Result<(), SerializeError<S::Error>>
where
    S: FormatSerializer,
{
    let discriminant = variant
        .discriminant
        .ok_or(SerializeError::Unsupported(Cow::Borrowed(
            "Enum without a discriminant",
        )))?;
    serializer
        .scalar(ScalarValue::I64(discriminant))
        .map_err(SerializeError::Backend)
}

fn serialize_untagged_enum<'mem, 'facet, S>(
    serializer: &mut S,
    enum_: facet_reflect::PeekEnum<'mem, 'facet>,
    variant: &'static facet_core::Variant,
) -> Result<(), SerializeError<S::Error>>
where
    S: FormatSerializer,
{
    match variant.data.kind {
        StructKind::Unit => {
            // The codex test suite uses `null` for unit variants like `Null`.
            // To preserve round-trippability for those fixtures, treat a `Null`
            // variant name specially; other unit variants fall back to a string.
            if variant.name.eq_ignore_ascii_case("null") {
                return serializer
                    .scalar(ScalarValue::Null)
                    .map_err(SerializeError::Backend);
            }
            serializer
                .scalar(ScalarValue::Str(Cow::Borrowed(variant.name)))
                .map_err(SerializeError::Backend)
        }
        StructKind::TupleStruct | StructKind::Tuple => {
            let field_count = variant.data.fields.len();
            if field_count == 1 {
                let inner = enum_
                    .field(0)
                    .map_err(|_| {
                        SerializeError::Internal(Cow::Borrowed("variant field lookup failed"))
                    })?
                    .ok_or(SerializeError::Internal(Cow::Borrowed(
                        "variant reported 1 field but field(0) returned None",
                    )))?;
                shared_serialize(serializer, inner)
            } else {
                serializer.begin_seq().map_err(SerializeError::Backend)?;
                for idx in 0..field_count {
                    let inner = enum_
                        .field(idx)
                        .map_err(|_| {
                            SerializeError::Internal(Cow::Borrowed("variant field lookup failed"))
                        })?
                        .ok_or(SerializeError::Internal(Cow::Borrowed(
                            "variant field missing while iterating tuple fields",
                        )))?;
                    shared_serialize(serializer, inner)?;
                }
                serializer.end_seq().map_err(SerializeError::Backend)?;
                Ok(())
            }
        }
        StructKind::Struct => {
            serializer.begin_struct().map_err(SerializeError::Backend)?;
            let mut fields: alloc::vec::Vec<_> = enum_.fields_for_serialize().collect();
            sort_fields_if_needed(serializer, &mut fields);
            for (field_item, field_value) in fields {
                serializer
                    .field_metadata(&field_item)
                    .map_err(SerializeError::Backend)?;
                serializer
                    .field_key(&field_item.name)
                    .map_err(SerializeError::Backend)?;
                // Check for field-level proxy
                if let Some(proxy_def) = field_item.field.and_then(|f| f.proxy()) {
                    serialize_via_proxy(serializer, field_value, proxy_def)?;
                } else {
                    shared_serialize(serializer, field_value)?;
                }
            }
            serializer.end_struct().map_err(SerializeError::Backend)?;
            Ok(())
        }
    }
}

/// Dereference a pointer/reference (Box, Arc, etc.) to get the underlying value
fn deref_if_pointer<'mem, 'facet>(peek: Peek<'mem, 'facet>) -> Peek<'mem, 'facet> {
    if let Ok(ptr) = peek.into_pointer()
        && let Some(target) = ptr.borrow_inner()
    {
        return deref_if_pointer(target);
    }
    peek
}

/// Serialize a value through its proxy type.
///
/// # Safety note
/// This function requires unsafe code to:
/// - Allocate memory for the proxy type
/// - Call the conversion function from target to proxy
/// - Drop the proxy value after serialization
#[allow(unsafe_code)]
fn serialize_via_proxy<'mem, 'facet, S>(
    serializer: &mut S,
    value: Peek<'mem, 'facet>,
    proxy_def: &'static facet_core::ProxyDef,
) -> Result<(), SerializeError<S::Error>>
where
    S: FormatSerializer,
{
    use facet_core::PtrUninit;

    let proxy_shape = proxy_def.shape;
    let proxy_layout = proxy_shape.layout.sized_layout().map_err(|_| {
        SerializeError::Unsupported(Cow::Borrowed("proxy type must be sized for serialization"))
    })?;

    // Allocate memory for the proxy value
    let proxy_mem = unsafe { alloc::alloc::alloc(proxy_layout) };
    if proxy_mem.is_null() {
        return Err(SerializeError::Internal(Cow::Borrowed(
            "failed to allocate proxy memory",
        )));
    }

    // Convert target → proxy
    let proxy_uninit = PtrUninit::new(proxy_mem);
    let convert_result = unsafe { (proxy_def.convert_out)(value.data(), proxy_uninit) };

    let proxy_ptr = match convert_result {
        Ok(ptr) => ptr,
        Err(msg) => {
            unsafe { alloc::alloc::dealloc(proxy_mem, proxy_layout) };
            return Err(SerializeError::Unsupported(Cow::Owned(msg)));
        }
    };

    // Create a Peek to the proxy value and serialize it
    let proxy_peek = unsafe { Peek::unchecked_new(proxy_ptr.as_const(), proxy_shape) };
    let result = shared_serialize(serializer, proxy_peek);

    // Clean up: drop the proxy value and deallocate
    unsafe {
        let _ = proxy_shape.call_drop_in_place(proxy_ptr);
        alloc::alloc::dealloc(proxy_mem, proxy_layout);
    }

    result
}