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
//! Tree-based deserializer for DOM documents.
use std::borrow::Cow;
use facet_core::{Def, StructKind, Type, UserType};
use facet_reflect::Partial;
use crate::error::DomDeserializeError;
use crate::naming::to_element_name;
use crate::trace;
use crate::{AttributeRecord, DomEvent, DomParser, DomParserExt};
mod entrypoints;
mod field_map;
mod struct_deser;
use struct_deser::StructDeserializer;
/// Extension trait for chaining deserialization on `Partial`.
pub(crate) trait PartialDeserializeExt<'de, const BORROW: bool, P: DomParser<'de>> {
/// Deserialize into this partial using the given deserializer.
fn deserialize_with(
self,
deserializer: &mut DomDeserializer<'de, BORROW, P>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>>;
/// Deserialize into this partial with an explicit expected element name.
fn deserialize_with_name(
self,
deserializer: &mut DomDeserializer<'de, BORROW, P>,
expected_name: Cow<'static, str>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>>;
}
impl<'de, const BORROW: bool, P: DomParser<'de>> PartialDeserializeExt<'de, BORROW, P>
for Partial<'de, BORROW>
{
fn deserialize_with(
self,
deserializer: &mut DomDeserializer<'de, BORROW, P>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
deserializer.deserialize_into(self)
}
fn deserialize_with_name(
self,
deserializer: &mut DomDeserializer<'de, BORROW, P>,
expected_name: Cow<'static, str>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
deserializer.deserialize_into_named(self, Some(expected_name))
}
}
/// DOM deserializer.
///
/// The `BORROW` parameter controls whether strings can be borrowed from the input:
/// - `BORROW = true`: Allows zero-copy deserialization of `&str` and `Cow<str>`
/// - `BORROW = false`: All strings are owned, input doesn't need to outlive result
pub struct DomDeserializer<'de, const BORROW: bool, P> {
parser: P,
_marker: std::marker::PhantomData<&'de ()>,
}
impl<'de, const BORROW: bool, P> DomDeserializer<'de, BORROW, P>
where
P: DomParser<'de>,
{
/// Deserialize a value into an existing Partial.
///
/// # Parser State Contract
///
/// **Entry:** The parser should be positioned such that the next event represents
/// the value to deserialize. For structs/enums, this means a `NodeStart` is next
/// (peeked but not consumed). For scalars within an element, the parser should be
/// inside the element (after `ChildrenStart`).
///
/// **Exit:** The parser will have consumed all events related to this value,
/// including the closing `NodeEnd` for struct types.
pub fn deserialize_into(
&mut self,
wip: Partial<'de, BORROW>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
self.deserialize_into_named(wip, None)
}
/// Deserialize a value into an existing Partial with an optional expected element name.
///
/// When `expected_name` is `Some`, it overrides the element name that would normally
/// be computed from the type. This is used when deserializing struct fields, where
/// the XML element name comes from the field name rather than the type name.
///
/// When `expected_name` is `None`, the element name is computed from the type's
/// `#[facet(rename = "...")]` attribute or its type identifier.
pub(crate) fn deserialize_into_named(
&mut self,
wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
let format_ns = self.parser.format_namespace();
// Check for field-level proxy first (e.g., #[facet(xml::proxy = ProxyType)] on a field)
// This takes precedence over container-level proxies.
if let Some(field) = wip.parent_field()
&& field.effective_proxy(format_ns).is_some()
{
let proxy_wip = wip
.begin_custom_deserialization_with_format(format_ns)
.map_err(DomDeserializeError::Reflect)?;
// Deserialize into proxy buffer with the same expected_name
let proxy_wip = self.deserialize_into_inner(proxy_wip, expected_name)?;
// Convert proxy -> target via TryFrom
return proxy_wip.end().map_err(DomDeserializeError::Reflect);
}
// Check for container-level proxy (e.g., #[facet(xml::proxy = ProxyType)] on the type)
// If present, we deserialize into the proxy type, then convert via TryFrom.
// The expected_name is preserved - it controls the XML element name, not the type.
if wip.shape().effective_proxy(format_ns).is_some() {
let (proxy_wip, found) = wip
.begin_custom_deserialization_from_shape_with_format(format_ns)
.map_err(DomDeserializeError::Reflect)?;
if found {
// Deserialize into proxy buffer with the same expected_name
let proxy_wip = self.deserialize_into_inner(proxy_wip, expected_name)?;
// Convert proxy -> target via TryFrom
return proxy_wip.end().map_err(DomDeserializeError::Reflect);
}
// Proxy check returned true but begin_custom_deserialization didn't find it
// (shouldn't happen, but fall through to normal path)
return self.deserialize_into_inner(proxy_wip, expected_name);
}
self.deserialize_into_inner(wip, expected_name)
}
/// Inner deserialization logic, called after proxy handling.
fn deserialize_into_inner(
&mut self,
mut wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
let shape = wip.shape();
#[cfg(any(test, feature = "tracing"))]
{
use owo_colors::OwoColorize;
let module_path = shape.module_path.unwrap_or("?");
let module = module_path.dimmed();
let name = shape.cyan();
trace!(into = %format_args!("{module}::{name}"));
}
// Check for RawMarkup BEFORE transparent wrapper handling
// (RawMarkup has inner=String but needs special raw capture handling)
if crate::raw_markup::is_raw_markup(shape) {
return self.deserialize_raw_markup(wip);
}
// Handle transparent wrappers (like NonZero, newtype structs with #[facet(transparent)])
// Collections (List/Map/Set/Array), Option, and Pointer have .inner for variance but shouldn't use this path
if shape.inner.is_some()
&& !matches!(
&shape.def,
Def::List(_)
| Def::Map(_)
| Def::Set(_)
| Def::Array(_)
| Def::Option(_)
| Def::Pointer(_)
)
{
wip = wip.begin_inner().map_err(DomDeserializeError::Reflect)?;
wip = self.deserialize_into_named(wip, expected_name)?;
wip = wip.end().map_err(DomDeserializeError::Reflect)?;
return Ok(wip);
}
// Check Def::Option first since Option now reports as UserType::Enum
if let Def::Option(_) = &shape.def {
return self.deserialize_option(wip, expected_name);
}
match &shape.ty {
Type::User(UserType::Struct(_)) => self.deserialize_struct(wip, expected_name),
Type::User(UserType::Enum(_)) => self.deserialize_enum(wip, expected_name),
_ => match &shape.def {
Def::Scalar => self.deserialize_scalar(wip),
Def::Pointer(_) => self.deserialize_pointer(wip, expected_name),
Def::List(_) => self.deserialize_list(wip, expected_name),
Def::Set(_) => self.deserialize_set(wip, expected_name),
Def::Map(_) => self.deserialize_map(wip),
_ => Err(DomDeserializeError::Unsupported(format!(
"unsupported type: {:?}",
shape.ty
))),
},
}
}
/// Deserialize a struct type.
///
/// # Parser State Contract
///
/// **Entry:** Parser is positioned before the struct's `NodeStart` (peeked, not consumed).
///
/// **Exit:** Parser has consumed through the struct's closing `NodeEnd`.
///
/// If `expected_name` is `Some`, it overrides the element name (used when deserializing
/// a field where the element name comes from the field, not the type).
fn deserialize_struct(
&mut self,
wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
let shape = wip.shape();
let struct_def = match &shape.ty {
Type::User(UserType::Struct(def)) => def,
_ => {
return Err(DomDeserializeError::Unsupported(
"expected struct type".into(),
));
}
};
// Use provided expected_name, or compute from shape:
// rename > rename_all(type_identifier) > lowerCamelCase(type_identifier)
let expected_name = expected_name.unwrap_or_else(|| {
if let Some(rename) = shape.get_builtin_attr_value::<&str>("rename") {
Cow::Borrowed(rename)
} else if let Some(rename_all) = shape.get_builtin_attr_value::<&str>("rename_all") {
Cow::Owned(crate::naming::apply_rename_all(
shape.type_identifier,
rename_all,
))
} else {
to_element_name(shape.type_identifier)
}
});
// For regular structs, rename_all is handled by facet-derive setting field.rename
// So we pass None here - the field map will use field.rename if present
self.deserialize_struct_innards(wip, struct_def, expected_name, None)
}
/// Deserialize the innards of a struct-like thing (struct, tuple, or enum variant data).
///
/// Delegates to `StructDeserializer` for the actual implementation.
///
/// The `rename_all` parameter, when provided, overrides any `rename_all` on the struct's shape.
/// This is used when deserializing enum variants, where the parent enum's `rename_all` should
/// apply to the variant's fields.
fn deserialize_struct_innards(
&mut self,
wip: Partial<'de, BORROW>,
struct_def: &'static facet_core::StructType,
expected_name: Cow<'static, str>,
rename_all: Option<&'static str>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
// Extract xml::ns_all attribute from the shape
let ns_all = wip
.shape()
.attributes
.iter()
.find(|attr| attr.ns == Some("xml") && attr.key == "ns_all")
.and_then(|attr| attr.get_as::<&str>().copied());
// Check if deny_unknown_fields is set
let deny_unknown_fields = wip.shape().has_deny_unknown_fields_attr();
StructDeserializer::new(
self,
struct_def,
ns_all,
rename_all,
expected_name,
deny_unknown_fields,
)
.deserialize(wip)
}
/// Deserialize an enum type.
///
/// # Parser State Contract
///
/// **Entry:** Parser is positioned at either:
/// - A `NodeStart` event (element-based variant), or
/// - A `Text` event (text-based variant, e.g., for enums with a `#[xml::text]` variant)
///
/// **Exit:** All events for this enum have been consumed:
/// - If entry was `NodeStart`: through the closing `NodeEnd`
/// - If entry was `Text`: just that text event
///
/// # Variant Selection
///
/// For `NodeStart`: The element tag name is matched against variant names (considering
/// `#[rename]` attributes). If no match, looks for a variant with `#[xml::custom_element]`.
///
/// For `Text`: Looks for a variant with `#[xml::text]` attribute.
///
/// If `expected_name` is `Some`, it's used for untagged enums. For tagged enums,
/// the element name is determined by the variant.
fn deserialize_enum(
&mut self,
mut wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
let event = self.parser.peek_event_or_eof("NodeStart or Text")?;
match event {
DomEvent::NodeStart { tag, .. } => {
let tag = tag.clone();
let enum_shape = wip.shape();
let enum_def = match &enum_shape.ty {
Type::User(UserType::Enum(def)) => def,
_ => {
return Err(DomDeserializeError::Unsupported(
"expected enum type".into(),
));
}
};
// Extract rename_all from the enum shape BEFORE selecting variant
// (wip.shape() changes after select_nth_variant)
// This propagates the enum's rename_all to variant field names
let rename_all = enum_shape.get_builtin_attr_value::<&str>("rename_all");
// For untagged enums, the element tag is the enum's name (not a variant name)
// We need to select the first variant and deserialize the content into it
let is_untagged = enum_shape.is_untagged();
let variant_idx = if is_untagged {
// For untagged enums, select the first (and typically only) variant
// The element tag should match the enum's rename, not a variant name
trace!(tag = %tag, "untagged enum - selecting first variant");
0
} else {
// For tagged enums, match the element tag against variant names.
// Compute effective element name: use rename attribute if present,
// otherwise convert to lowerCamelCase.
enum_def
.variants
.iter()
.position(|v| {
let effective_name: Cow<'_, str> = if v.rename.is_some() {
Cow::Borrowed(v.effective_name())
} else {
to_element_name(v.name)
};
effective_name == tag
})
.or_else(|| enum_def.variants.iter().position(|v| v.is_custom_element()))
.ok_or_else(|| DomDeserializeError::UnknownElement {
tag: tag.to_string(),
})?
};
let variant = &enum_def.variants[variant_idx];
wip = wip.select_nth_variant(variant_idx)?;
trace!(variant_name = variant.name, variant_kind = ?variant.data.kind, is_untagged, "selected variant");
// Compute element name for this variant
let variant_element_name: Cow<'static, str> = if is_untagged {
// For untagged enums, use provided expected_name or compute from enum type
expected_name.clone().unwrap_or_else(|| {
let shape = wip.shape();
if let Some(renamed) = shape.get_builtin_attr_value::<&str>("rename") {
Cow::Borrowed(renamed)
} else {
to_element_name(shape.type_identifier)
}
})
} else if variant.rename.is_some() {
Cow::Borrowed(variant.effective_name())
} else {
to_element_name(variant.name)
};
// Handle variant based on its kind
match variant.data.kind {
StructKind::Unit => {
// Unit variant: just consume the element
self.parser.expect_node_start()?;
// Skip to end of element
let event = self.parser.peek_event_or_eof("ChildrenStart or NodeEnd")?;
if matches!(event, DomEvent::ChildrenStart) {
self.parser.expect_children_start()?;
self.parser.expect_children_end()?;
}
self.parser.expect_node_end()?;
}
StructKind::TupleStruct if variant.data.fields.len() == 1 => {
// Newtype variant (single unnamed field): deserialize the inner type
// Use deserialize_into_named to handle proxies and pass through element name
wip = wip
.begin_nth_field(0)?
.deserialize_with_name(self, variant_element_name)?
.end()?;
}
StructKind::TupleStruct | StructKind::Struct | StructKind::Tuple => {
// Struct variant, tuple variant (2+ fields), or tuple type:
// deserialize using the variant's data as a StructType
// Pass enum's rename_all to apply to variant field names
wip = self.deserialize_struct_innards(
wip,
&variant.data,
variant_element_name,
rename_all,
)?;
}
}
}
DomEvent::Text(_) => {
let text = self.parser.expect_text()?;
wip = self.deserialize_text_into_enum(wip, text)?;
}
other => {
return Err(DomDeserializeError::TypeMismatch {
expected: "NodeStart or Text",
got: format!("{other:?}"),
});
}
}
Ok(wip)
}
/// Deserialize text content into an enum by selecting the `#[xml::text]` variant.
///
/// # Parser State Contract
///
/// **Entry:** The text has already been consumed from the parser (passed as argument).
///
/// **Exit:** No parser state change (text was already consumed).
///
/// # Fallback
///
/// If `wip` is not actually an enum, falls back to `set_string_value`.
fn deserialize_text_into_enum(
&mut self,
mut wip: Partial<'de, BORROW>,
text: Cow<'de, str>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
let enum_def = match &wip.shape().ty {
Type::User(UserType::Enum(def)) => def,
_ => {
return self.set_string_value(wip, text);
}
};
let text_variant_idx = match enum_def.variants.iter().position(|v| v.is_text()) {
Some(idx) => idx,
None => {
// No text variant - either error (XML) or silently discard (HTML)
if self.parser.is_lenient() {
return Ok(wip);
} else {
return Err(DomDeserializeError::Unsupported(
"enum has no Text variant for text content".into(),
));
}
}
};
let variant = &enum_def.variants[text_variant_idx];
wip = wip.select_nth_variant(text_variant_idx)?;
// Handle the variant based on its kind
match variant.data.kind {
StructKind::TupleStruct => {
// Newtype variant like Text(String) - navigate to field 0
wip = wip.begin_nth_field(0)?;
wip = self.set_string_value(wip, text)?;
wip = wip.end()?;
}
StructKind::Unit => {
// Unit variant - nothing to set (unusual for text variant but handle it)
}
_ => {
// For other kinds, try direct set (may fail)
wip = self.set_string_value(wip, text)?;
}
}
Ok(wip)
}
/// Deserialize RawMarkup by capturing raw source from the parser.
fn deserialize_raw_markup(
&mut self,
wip: Partial<'de, BORROW>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
// Must be at a NodeStart
let event = self.parser.peek_event_or_eof("NodeStart for RawMarkup")?;
if !matches!(event, DomEvent::NodeStart { .. }) {
return Err(DomDeserializeError::TypeMismatch {
expected: "NodeStart for RawMarkup",
got: format!("{event:?}"),
});
}
// Consume the NodeStart
self.parser
.next_event()
.map_err(DomDeserializeError::Parser)?;
// Try to capture raw - if not supported, fall back to error
let raw = self
.parser
.capture_raw_node()
.map_err(DomDeserializeError::Parser)?
.ok_or_else(|| {
DomDeserializeError::Unsupported("parser does not support raw capture".into())
})?;
// Set via the vtable's parse function
self.set_string_value(wip, raw)
}
/// Deserialize a scalar value (string, number, bool, etc.).
///
/// # Parser State Contract
///
/// **Entry:** Parser is positioned at either:
/// - A `Text` event (inline text content), or
/// - A `NodeStart` event (element wrapping the text content)
///
/// **Exit:** All events for this scalar have been consumed:
/// - If entry was `Text`: just that text event
/// - If entry was `NodeStart`: through the closing `NodeEnd`
///
/// # XML Data Model
///
/// In XML, scalars can appear as:
/// - Attribute values (handled elsewhere)
/// - Text content: `<parent>text here</parent>`
/// - Element with text: `<field>value</field>` (element is consumed)
fn deserialize_scalar(
&mut self,
wip: Partial<'de, BORROW>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
trace!("deserialize_scalar called");
let event = self.parser.peek_event_or_eof("Text or NodeStart")?;
trace!(event = ?event, "peeked event in deserialize_scalar");
match event {
DomEvent::Text(_) => {
trace!("deserialize_scalar: matched Text arm");
let text = self.parser.expect_text()?;
// Use set_string_value_with_proxy for format-specific proxy support
self.set_string_value_with_proxy(wip, text)
}
DomEvent::NodeStart { .. } => {
trace!("deserialize_scalar: matched NodeStart arm");
let _tag = self.parser.expect_node_start()?;
trace!(tag = %_tag, "deserialize_scalar: consumed NodeStart");
loop {
let event = self
.parser
.peek_event_or_eof("Attribute or ChildrenStart or NodeEnd")?;
trace!(event = ?event, "deserialize_scalar: in attr loop");
match event {
DomEvent::Attribute { .. } => {
let AttributeRecord {
name: _name,
value: _value,
namespace: _namespace,
} = self.parser.expect_attribute()?;
trace!(name = %_name, "deserialize_scalar: consumed Attribute");
}
DomEvent::ChildrenStart => {
self.parser.expect_children_start()?;
trace!("deserialize_scalar: consumed ChildrenStart");
break;
}
DomEvent::NodeEnd => {
self.parser.expect_node_end()?;
trace!("deserialize_scalar: void element, returning empty string");
// Use set_string_value_with_proxy for format-specific proxy support
return self.set_string_value_with_proxy(wip, Cow::Borrowed(""));
}
other => {
trace!(other = ?other, "deserialize_scalar: unexpected event in attr loop");
return Err(DomDeserializeError::TypeMismatch {
expected: "Attribute or ChildrenStart or NodeEnd",
got: format!("{other:?}"),
});
}
}
}
trace!("deserialize_scalar: starting text content loop");
let mut text_content = String::new();
loop {
let event = self.parser.peek_event_or_eof("Text or ChildrenEnd")?;
trace!(event = ?event, "deserialize_scalar: in text content loop");
match event {
DomEvent::Text(_) => {
let text = self.parser.expect_text()?;
trace!(text = %text, "deserialize_scalar: got text");
text_content.push_str(&text);
}
DomEvent::ChildrenEnd => {
trace!("deserialize_scalar: got ChildrenEnd, breaking text loop");
break;
}
DomEvent::NodeStart { .. } => {
trace!("deserialize_scalar: skipping nested NodeStart");
self.parser
.skip_node()
.map_err(DomDeserializeError::Parser)?;
}
DomEvent::Comment(_) => {
let _comment = self.parser.expect_comment()?;
}
other => {
return Err(DomDeserializeError::TypeMismatch {
expected: "Text or ChildrenEnd",
got: format!("{other:?}"),
});
}
}
}
trace!("deserialize_scalar: consuming ChildrenEnd");
self.parser.expect_children_end()?;
trace!("deserialize_scalar: consuming NodeEnd");
self.parser.expect_node_end()?;
trace!(text_content = %text_content, "deserialize_scalar: setting string value");
// Use set_string_value_with_proxy for format-specific proxy support
self.set_string_value_with_proxy(wip, Cow::Owned(text_content))
}
other => Err(DomDeserializeError::TypeMismatch {
expected: "Text or NodeStart",
got: format!("{other:?}"),
}),
}
}
/// Deserialize a list (Vec, slice, etc.) from repeated child elements.
///
/// # Parser State Contract
///
/// **Entry:** Parser is positioned inside an element, after `ChildrenStart`.
/// Child elements will be deserialized as list items.
///
/// **Exit:** Parser is positioned at `ChildrenEnd` (peeked, not consumed).
/// The caller is responsible for consuming `ChildrenEnd` and `NodeEnd`.
///
/// # Note
///
/// This is used for "wrapped" list semantics where a parent element contains
/// the list items. For "flat" list semantics (items directly as siblings),
/// see the flat sequence handling in `deserialize_struct_innards`.
///
/// If `expected_name` is provided, it's used as the element name for each item.
fn deserialize_list(
&mut self,
mut wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
wip = wip.init_list()?;
loop {
let event = self.parser.peek_event_or_eof("child or ChildrenEnd")?;
if matches!(event, DomEvent::ChildrenEnd) {
break;
}
wip = wip.begin_list_item()?;
wip = self.deserialize_into_named(wip, expected_name.clone())?;
wip = wip.end()?;
}
Ok(wip)
}
/// Deserialize a set type (HashSet, BTreeSet, etc.).
///
/// Works the same as lists: each child element becomes a set item.
///
/// If `expected_name` is provided, it's used as the element name for each item.
fn deserialize_set(
&mut self,
mut wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
wip = wip.init_set()?;
loop {
let event = self.parser.peek_event_or_eof("child or ChildrenEnd")?;
if matches!(event, DomEvent::ChildrenEnd) {
break;
}
wip = wip.begin_set_item()?;
wip = self.deserialize_into_named(wip, expected_name.clone())?;
wip = wip.end()?;
}
Ok(wip)
}
/// Deserialize a map type (HashMap, BTreeMap, etc.).
///
/// In XML, maps use a **wrapped** model:
/// - The field name becomes a wrapper element
/// - Each child element becomes a map entry (tag = key, content = value)
///
/// Example: `<data><alpha>1</alpha><beta>2</beta></data>` -> {"alpha": 1, "beta": 2}
///
/// # Parser State Contract
///
/// **Entry:** Parser is positioned at the wrapper element's `NodeStart`.
///
/// **Exit:** Parser has consumed through the wrapper element's `NodeEnd`.
fn deserialize_map(
&mut self,
mut wip: Partial<'de, BORROW>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
// Consume the wrapper element's NodeStart
let event = self.parser.peek_event_or_eof("NodeStart for map wrapper")?;
match event {
DomEvent::NodeStart { .. } => {
trace!("map wrapper element");
let _ = self.parser.expect_node_start()?;
}
other => {
return Err(DomDeserializeError::TypeMismatch {
expected: "NodeStart for map wrapper",
got: format!("{other:?}"),
});
}
}
// Skip attributes on the wrapper element
loop {
let event = self
.parser
.peek_event_or_eof("Attribute or ChildrenStart or NodeEnd")?;
match event {
DomEvent::Attribute { .. } => {
self.parser.expect_attribute()?;
}
DomEvent::ChildrenStart => {
self.parser.expect_children_start()?;
break;
}
DomEvent::NodeEnd => {
// Empty map (void element)
self.parser.expect_node_end()?;
return Ok(wip.init_map()?);
}
other => {
return Err(DomDeserializeError::TypeMismatch {
expected: "Attribute or ChildrenStart or NodeEnd",
got: format!("{other:?}"),
});
}
}
}
wip = wip.init_map()?;
// Now parse map entries from children
loop {
let event = self.parser.peek_event_or_eof("child or ChildrenEnd")?;
match event {
DomEvent::ChildrenEnd => break,
DomEvent::NodeStart { tag, .. } => {
let key = tag.clone();
trace!(key = %key, "map entry");
// Set the key (element name)
wip = wip.begin_key()?;
wip = self.set_string_value(wip, key)?;
wip = wip.end()?;
// Deserialize the value (element content)
wip = wip.begin_value()?.deserialize_with(self)?.end()?;
}
DomEvent::Text(_) | DomEvent::Comment(_) => {
// Skip whitespace text and comments between map entries
if matches!(event, DomEvent::Text(_)) {
self.parser.expect_text()?;
} else {
self.parser.expect_comment()?;
}
}
_ => {
return Err(DomDeserializeError::TypeMismatch {
expected: "map entry element",
got: format!("{event:?}"),
});
}
}
}
// Consume wrapper's ChildrenEnd and NodeEnd
self.parser.expect_children_end()?;
self.parser.expect_node_end()?;
Ok(wip)
}
/// Deserialize an Option type.
///
/// # Parser State Contract
///
/// **Entry:** Parser is positioned where the optional value would be.
///
/// **Exit:** If value was present, all events for the value have been consumed.
/// If value was absent, no events consumed.
///
/// # None Detection
///
/// The option is `None` if the next event is `ChildrenEnd` or `NodeEnd`
/// (indicating no content). Otherwise, the inner value is deserialized.
///
/// If `expected_name` is provided, it's passed through to the inner deserialization.
fn deserialize_option(
&mut self,
mut wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
let event = self.parser.peek_event_or_eof("value")?;
if matches!(event, DomEvent::ChildrenEnd | DomEvent::NodeEnd) {
wip = wip.set_default()?;
} else {
wip = wip.begin_some()?;
wip = self.deserialize_into_named(wip, expected_name)?;
wip = wip.end()?;
}
Ok(wip)
}
/// Deserialize a pointer type (Box, Arc, Rc, etc.).
///
/// # Parser State Contract
///
/// **Entry:** Parser is positioned at the value that the pointer will wrap.
///
/// **Exit:** All events for the inner value have been consumed.
///
/// # Pointer Actions
///
/// Uses `facet_dessert::begin_pointer` to determine how to handle the pointer:
/// - `HandleAsScalar`: Treat as scalar (e.g., `Box<str>`)
/// - `SliceBuilder`: Build a slice (e.g., `Arc<[T]>`)
/// - `SizedPointee`: Regular pointer to sized type
///
/// If `expected_name` is provided, it's passed through to the inner deserialization.
fn deserialize_pointer(
&mut self,
wip: Partial<'de, BORROW>,
expected_name: Option<Cow<'static, str>>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
use facet_dessert::{PointerAction, begin_pointer};
let (wip, action) = begin_pointer(wip)?;
match action {
PointerAction::HandleAsScalar => self.deserialize_scalar(wip),
PointerAction::SliceBuilder => Ok(self.deserialize_list(wip, expected_name)?.end()?),
PointerAction::SizedPointee => {
Ok(self.deserialize_into_named(wip, expected_name)?.end()?)
}
}
}
/// Set a string value on the current partial, parsing it to the appropriate type.
///
/// # Parser State Contract
///
/// **Entry/Exit:** No parser state change. The string value is passed as an argument.
///
/// # Type Handling
///
/// For enums, matches the string against variant names using lowerCamelCase conversion
/// (matching the serialization behavior). For other types, delegates to
/// `facet_dessert::set_string_value` which handles parsing the string into the
/// appropriate scalar type (String, &str, integers, floats, bools, etc.).
pub(crate) fn set_string_value(
&mut self,
mut wip: Partial<'de, BORROW>,
value: Cow<'de, str>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
// Handle enums specially - match variant names with lowerCamelCase conversion
// Skip Option (now reports as UserType::Enum) - facet_dessert handles it
if let Type::User(UserType::Enum(enum_def)) = &wip.shape().ty
&& !matches!(wip.shape().def, Def::Option(_))
{
// Find matching variant
for (idx, variant) in enum_def.variants.iter().enumerate() {
// Only unit variants can be deserialized from a plain string
if variant.data.kind != StructKind::Unit {
continue;
}
// Compute the expected string for this variant (same logic as serialization)
let variant_str: Cow<'_, str> = if variant.rename.is_some() {
Cow::Borrowed(variant.effective_name())
} else {
to_element_name(variant.name)
};
if value == variant_str {
wip = wip.select_nth_variant(idx)?;
return Ok(wip);
}
}
// No match found - fall through to facet_dessert which will give a proper error
}
Ok(facet_dessert::set_string_value(
wip,
value,
self.parser.current_span(),
)?)
}
/// Set a string value, handling field-level proxy conversion if present.
///
/// If the field has a proxy attribute (e.g., `#[facet(proxy = PointsProxy)]`),
/// this will:
/// 1. Begin custom deserialization (push a frame for the proxy type)
/// 2. Set the string value into the proxy type
/// 3. End the frame (which converts proxy -> target via TryFrom)
///
/// If no proxy is present, it just calls `set_string_value` directly.
///
/// This method supports format-specific proxies: if the parser returns a format
/// namespace (e.g., "xml"), fields with `#[facet(xml::proxy = ...)]` will use
/// that proxy instead of the format-agnostic one.
pub(crate) fn set_string_value_with_proxy(
&mut self,
mut wip: Partial<'de, BORROW>,
value: Cow<'de, str>,
) -> Result<Partial<'de, BORROW>, DomDeserializeError<P::Error>> {
// Check if the field has a proxy (format-specific or format-agnostic)
let format_ns = self.parser.format_namespace();
let field_proxy = wip
.parent_field()
.and_then(|f| f.effective_proxy(format_ns));
if field_proxy.is_some() {
// Use custom deserialization through the field-level proxy
// The format-aware version will select the right proxy
wip = wip.begin_custom_deserialization_with_format(format_ns)?;
wip = self.set_string_value(wip, value)?;
wip = wip.end()?;
Ok(wip)
} else if wip.shape().effective_proxy(format_ns).is_some() {
// The target shape has a container-level proxy
// Use begin_custom_deserialization_from_shape_with_format
let (new_wip, _) =
wip.begin_custom_deserialization_from_shape_with_format(format_ns)?;
wip = new_wip;
wip = self.set_string_value(wip, value)?;
wip = wip.end()?;
Ok(wip)
} else {
self.set_string_value(wip, value)
}
}
}