1#![cfg_attr(not(feature = "std"), no_std)]
2#![warn(missing_docs)]
3#![warn(clippy::std_instead_of_core)]
4#![warn(clippy::std_instead_of_alloc)]
5#![forbid(unsafe_code)]
6#![doc = include_str!("../README.md")]
7
8extern crate alloc;
9
10use alloc::string::ToString;
11use alloc::{vec, vec::Vec};
12use core::fmt::Debug;
13
14mod debug;
15mod error;
16use alloc::borrow::Cow;
17pub use debug::InputDebug;
18
19pub use error::*;
20
21mod span;
22use facet_core::{
23 Characteristic, Def, Facet, FieldFlags, PointerType, ScalarAffinity, StructKind, Type, UserType,
24};
25use owo_colors::OwoColorize;
26pub use span::*;
27
28use facet_reflect::{HeapValue, Partial, ReflectError};
29use log::trace;
30
31#[derive(PartialEq, Debug, Clone)]
32pub enum Scalar<'input> {
37 String(Cow<'input, str>),
39 U64(u64),
41 I64(i64),
43 F64(f64),
45 Bool(bool),
47 Null,
49}
50
51#[derive(PartialEq, Debug, Clone)]
52pub enum Expectation {
54 Value,
56 ObjectKeyOrObjectClose,
58 ObjectVal,
60 ListItemOrListClose,
62}
63
64#[derive(PartialEq, Debug, Clone)]
65pub enum Outcome<'input> {
67 Scalar(Scalar<'input>),
69 ListStarted,
71 ListEnded,
73 ObjectStarted,
75 ObjectEnded,
77 Resegmented(Vec<Subspan>),
79}
80
81impl<'input> From<Scalar<'input>> for Outcome<'input> {
82 fn from(scalar: Scalar<'input>) -> Self {
83 Outcome::Scalar(scalar)
84 }
85}
86
87use core::fmt;
88
89impl fmt::Display for Outcome<'_> {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 match self {
93 Outcome::Scalar(scalar) => write!(f, "scalar {}", scalar),
94 Outcome::ListStarted => write!(f, "list start"),
95 Outcome::ListEnded => write!(f, "list end"),
96 Outcome::ObjectStarted => write!(f, "object start"),
97 Outcome::ObjectEnded => write!(f, "object end"),
98 Outcome::Resegmented(_) => write!(f, "resegment"),
99 }
100 }
101}
102
103impl fmt::Display for Scalar<'_> {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 match self {
107 Scalar::String(s) => write!(f, "string \"{}\"", s),
108 Scalar::U64(val) => write!(f, "u64 {}", val),
109 Scalar::I64(val) => write!(f, "i64 {}", val),
110 Scalar::F64(val) => write!(f, "f64 {}", val),
111 Scalar::Bool(val) => write!(f, "bool {}", val),
112 Scalar::Null => write!(f, "null"),
113 }
114 }
115}
116
117impl Outcome<'_> {
118 fn into_owned(self) -> Outcome<'static> {
119 match self {
120 Outcome::Scalar(scalar) => {
121 let owned_scalar = match scalar {
122 Scalar::String(cow) => Scalar::String(Cow::Owned(cow.into_owned())),
123 Scalar::U64(val) => Scalar::U64(val),
124 Scalar::I64(val) => Scalar::I64(val),
125 Scalar::F64(val) => Scalar::F64(val),
126 Scalar::Bool(val) => Scalar::Bool(val),
127 Scalar::Null => Scalar::Null,
128 };
129 Outcome::Scalar(owned_scalar)
130 }
131 Outcome::ListStarted => Outcome::ListStarted,
132 Outcome::ListEnded => Outcome::ListEnded,
133 Outcome::ObjectStarted => Outcome::ObjectStarted,
134 Outcome::ObjectEnded => Outcome::ObjectEnded,
135 Outcome::Resegmented(subspans) => {
136 let owned_subspans = subspans
137 .into_iter()
138 .map(|s| Subspan {
139 offset: s.offset,
140 len: s.len,
141 meta: s.meta,
142 })
143 .collect();
144 Outcome::Resegmented(owned_subspans)
145 }
146 }
147 }
148}
149
150pub struct NextData<'input, 'facet, 'shape, C = Cooked, I = [u8]>
153where
154 'input: 'facet,
155 I: ?Sized + 'input,
156{
157 start: usize,
159
160 runner: StackRunner<'input, C, I>,
162
163 pub wip: Partial<'facet, 'shape>,
165}
166
167impl<'input, 'facet, 'shape, C, I> NextData<'input, 'facet, 'shape, C, I>
168where
169 'input: 'facet,
170 I: ?Sized + 'input,
171{
172 pub fn input(&self) -> &'input I {
174 self.runner.input
175 }
176
177 pub fn start(&self) -> usize {
179 self.start
180 }
181
182 pub fn substack(&self) -> &Substack<C> {
184 &self.runner.substack
185 }
186}
187
188pub type NextResult<'input, 'facet, 'shape, T, E, C, I = [u8]> =
190 (NextData<'input, 'facet, 'shape, C, I>, Result<T, E>);
191
192pub trait Format {
195 type Input<'input>: ?Sized;
200
201 type SpanType: Debug + SubstackBehavior + 'static;
203
204 fn source(&self) -> &'static str;
206
207 #[allow(clippy::type_complexity)]
209 fn next<'input, 'facet, 'shape>(
210 &mut self,
211 nd: NextData<'input, 'facet, 'shape, Self::SpanType, Self::Input<'input>>,
212 expectation: Expectation,
213 ) -> NextResult<
214 'input,
215 'facet,
216 'shape,
217 Spanned<Outcome<'input>, Self::SpanType>,
218 Spanned<DeserErrorKind<'shape>, Self::SpanType>,
219 Self::SpanType,
220 Self::Input<'input>,
221 >
222 where
223 'shape: 'input;
224
225 #[allow(clippy::type_complexity)]
227 fn skip<'input, 'facet, 'shape>(
228 &mut self,
229 nd: NextData<'input, 'facet, 'shape, Self::SpanType, Self::Input<'input>>,
230 ) -> NextResult<
231 'input,
232 'facet,
233 'shape,
234 Span<Self::SpanType>,
235 Spanned<DeserErrorKind<'shape>, Self::SpanType>,
236 Self::SpanType,
237 Self::Input<'input>,
238 >
239 where
240 'shape: 'input;
241}
242
243pub trait ToCooked<'input, F: Format> {
245 fn to_cooked(self, format: &F, input: &'input F::Input<'input>) -> Span<Cooked>;
247}
248
249impl<'input, F: Format> ToCooked<'input, F> for Span<Cooked> {
250 #[inline]
251 fn to_cooked(self, _format: &F, _input: &'input F::Input<'input>) -> Span<Cooked> {
252 self
253 }
254}
255
256impl<'input, F: Format<SpanType = Raw, Input<'input> = [&'input str]>> ToCooked<'input, F>
257 for Span<Raw>
258{
259 #[inline]
260 fn to_cooked(self, _format: &F, input: &'input [&'input str]) -> Span<Cooked> {
261 if self.start >= input.len() {
262 let mut total_len = 0;
265 for (i, arg) in input.iter().enumerate() {
266 total_len += arg.len();
267 if i < input.len() - 1 {
268 total_len += 1; }
270 }
271 return Span::<Cooked>::new(total_len.saturating_sub(1), 1);
272 }
273
274 let mut start = 0;
276 for arg in input.iter().take(self.start) {
277 start += arg.len() + 1; }
279
280 let len = input[self.start].len();
282
283 Span::<Cooked>::new(start, len)
284 }
285}
286
287#[derive(Debug, Clone, Copy, PartialEq, Eq)]
289pub enum Instruction {
290 Value(ValueReason),
292 SkipValue,
294 Pop(PopReason),
296 ObjectKeyOrObjectClose,
298 ListItemOrListClose,
300 SubstackClose,
302}
303
304#[derive(Debug, Clone, Copy, PartialEq, Eq)]
306pub enum ValueReason {
307 TopLevel,
309 ObjectVal,
311}
312
313#[derive(Debug, Clone, Copy, PartialEq, Eq)]
315pub enum PopReason {
316 TopLevel,
318 ObjectVal,
320 ListVal,
322 Some,
324 SmartPointer,
326 Wrapper,
328}
329
330mod deser_impl {
331 use super::*;
332
333 pub fn deserialize<'input, 'facet, 'shape, T, F>(
338 input: &'input F::Input<'input>,
339 format: &mut F,
340 ) -> Result<T, DeserError<'input, 'shape, Cooked>>
341 where
342 T: Facet<'facet>,
343 F: Format + 'shape,
344 F::Input<'input>: InputDebug,
345 F::SpanType: core::fmt::Debug,
346 Span<F::SpanType>: ToCooked<'input, F>,
347 'input: 'facet,
348 'shape: 'input,
349 {
350 let result: Result<T, DeserError<'input, 'shape, Cooked>> = {
352 let source = format.source();
353
354 let wip = match Partial::alloc_shape(T::SHAPE) {
356 Ok(wip) => wip,
357 Err(e) => {
358 let default_span = Span::<F::SpanType>::default();
359 let cooked_span = default_span.to_cooked(format, input);
361 return Err(DeserError::new_reflect(e, input, cooked_span, source));
362 }
363 };
364
365 let heap_value = match deserialize_wip(wip, input, format) {
367 Ok(val) => val,
368 Err(e) => {
369 let cooked_span = e.span.to_cooked(format, input);
370
371 let cooked_error = DeserError {
373 input: e.input,
374 span: cooked_span,
375 kind: e.kind,
376 source_id: e.source_id,
377 };
378
379 return Err(cooked_error);
380 }
381 };
382
383 match heap_value.materialize() {
385 Ok(val) => Ok(val),
386 Err(e) => {
387 let default_span = Span::<F::SpanType>::default();
388 let cooked_span = default_span.to_cooked(format, input);
389 return Err(DeserError::new_reflect(e, input, cooked_span, source));
390 }
391 }
392 };
393
394 match result {
396 Ok(value) => Ok(value),
397 Err(mut error) => {
398 let new_span = error.span.to_cooked(format, input);
399
400 if new_span != error.span {
401 error = DeserError {
402 input: error.input,
403 span: new_span,
404 kind: error.kind,
405 source_id: error.source_id,
406 };
407 }
408
409 Err(error)
410 }
411 }
412 }
413}
414
415pub fn deserialize<'input, 'facet, 'shape, T, F>(
420 input: &'input F::Input<'input>,
421 format: F,
422) -> Result<T, DeserError<'input, 'shape, Cooked>>
423where
424 T: Facet<'facet>,
425 F: Format + 'shape,
426 F::Input<'input>: InputDebug,
427 F::SpanType: core::fmt::Debug,
428 Span<F::SpanType>: ToCooked<'input, F>,
429 'input: 'facet,
430 'shape: 'input,
431{
432 let mut format_copy = format;
433 deser_impl::deserialize(input, &mut format_copy)
434}
435
436pub fn deserialize_wip<'input, 'facet, 'shape, F>(
439 mut wip: Partial<'facet, 'shape>,
440 input: &'input F::Input<'input>,
441 format: &mut F,
442) -> Result<HeapValue<'facet, 'shape>, DeserError<'input, 'shape, Cooked>>
443where
444 F: Format + 'shape,
445 F::SpanType: SubstackBehavior,
446 F::Input<'input>: InputDebug,
447 Span<F::SpanType>: ToCooked<'input, F>,
448 'input: 'facet,
449 'shape: 'input,
450{
451 let mut runner = StackRunner {
453 original_input: input,
454 input,
455 stack: vec![
456 Instruction::Pop(PopReason::TopLevel),
457 Instruction::Value(ValueReason::TopLevel),
458 ],
459 substack: Substack::new(),
460 last_span: Span::new(0, 0),
461 format_source: format.source(),
462 array_indices: Vec::new(),
463 enum_tuple_field_count: None,
464 enum_tuple_current_field: None,
465 };
466
467 macro_rules! next {
468 ($runner:ident, $wip:ident, $expectation:expr, $method:ident) => {{
469 let nd = NextData {
470 start: $runner.last_span.end(), runner: $runner,
472 wip: $wip,
473 };
474 let (nd, res) = format.next(nd, $expectation);
475 $runner = nd.runner;
476 $wip = nd.wip;
477 let outcome = res.map_err(|span_kind| {
478 $runner.last_span = span_kind.span;
479 let error = $runner.err(span_kind.node);
480 DeserError {
482 input: error.input,
483 span: error.span.to_cooked(format, input),
484 kind: error.kind,
485 source_id: error.source_id,
486 }
487 })?;
488 if F::SpanType::USES_SUBSTACK {
489 if !$runner.substack.get().is_empty() {
490 trace!("Substack: {}", "carried".cyan());
491 } else {
492 trace!("Substack: {}", "-".red());
493 }
494 }
495 $runner.last_span = outcome.span;
496 if F::SpanType::USES_SUBSTACK {
497 if let Outcome::Resegmented(subspans) = &outcome.node {
498 $runner.substack = subspans.clone().into();
499 }
500 }
501 $wip = $runner.$method($wip, outcome).map_err(|error| {
502 DeserError {
503 input: error.input,
504 span: error.span.to_cooked(format, input),
505 kind: error.kind,
506 source_id: error.source_id,
507 }
508 })?;
509 }};
510 }
511
512 loop {
513 let insn = match runner.stack.pop() {
517 Some(insn) => insn,
518 None => unreachable!("Instruction stack is empty"),
519 };
520
521 trace!("Instruction {:?}", insn.bright_red());
522
523 match insn {
524 Instruction::Pop(reason) => {
525 wip = runner.pop(wip, reason).map_err(|error| {
526 DeserError {
528 input: error.input,
529 span: error.span.to_cooked(format, input),
530 kind: error.kind,
531 source_id: error.source_id,
532 }
533 })?;
534
535 if reason == PopReason::TopLevel {
536 while wip.frame_count() > 1 {
538 wip.end().map_err(|e| {
539 let reflect_error = runner.reflect_err(e);
540 DeserError {
541 input: reflect_error.input,
542 span: reflect_error.span.to_cooked(format, input),
543 kind: reflect_error.kind,
544 source_id: reflect_error.source_id,
545 }
546 })?;
547 }
548
549 return wip.build().map_err(|e| {
550 let reflect_error = runner.reflect_err(e);
551 DeserError {
553 input: reflect_error.input,
554 span: reflect_error.span.to_cooked(format, input),
555 kind: reflect_error.kind,
556 source_id: reflect_error.source_id,
557 }
558 });
559 } else {
560 wip.end().map_err(|e| {
561 let reflect_error = runner.reflect_err(e);
562 DeserError {
564 input: reflect_error.input,
565 span: reflect_error.span.to_cooked(format, input),
566 kind: reflect_error.kind,
567 source_id: reflect_error.source_id,
568 }
569 })?;
570 }
571 }
572 Instruction::Value(_why) => {
573 let expectation = match _why {
574 ValueReason::TopLevel => Expectation::Value,
575 ValueReason::ObjectVal => Expectation::ObjectVal,
576 };
577 next!(runner, wip, expectation, value);
578 }
579 Instruction::ObjectKeyOrObjectClose => {
580 next!(
581 runner,
582 wip,
583 Expectation::ObjectKeyOrObjectClose,
584 object_key_or_object_close
585 );
586 }
587 Instruction::ListItemOrListClose => {
588 next!(
589 runner,
590 wip,
591 Expectation::ListItemOrListClose,
592 list_item_or_list_close
593 );
594 }
595 Instruction::SubstackClose => {
596 runner.substack.clear();
597 }
598 Instruction::SkipValue => {
599 let nd = NextData {
601 start: runner.last_span.end(),
602 runner,
603 wip,
604 };
605 let (nd, res) = format.skip(nd);
606 runner = nd.runner;
607 wip = nd.wip;
608 let span = res.map_err(|span_kind| {
610 runner.last_span = span_kind.span;
611 let error = runner.err(span_kind.node);
612 DeserError {
614 input: error.input,
615 span: error.span.to_cooked(format, input),
616 kind: error.kind,
617 source_id: error.source_id,
618 }
619 })?;
620 runner.last_span = span;
622 }
623 }
624 }
625}
626
627#[inline]
630fn has_no_fractional_part(value: f64) -> bool {
631 value == (value as i64) as f64
632}
633
634trait NumericConvert: Sized {
636 const TYPE_NAME: &'static str;
637
638 fn to_i8(self) -> Option<i8>;
639 fn to_i16(self) -> Option<i16>;
640 fn to_i32(self) -> Option<i32>;
641 fn to_i64(self) -> Option<i64>;
642 fn to_i128(self) -> Option<i128>;
643 fn to_isize(self) -> Option<isize>;
644
645 fn to_u8(self) -> Option<u8>;
646 fn to_u16(self) -> Option<u16>;
647 fn to_u32(self) -> Option<u32>;
648 fn to_u64(self) -> Option<u64>;
649 fn to_u128(self) -> Option<u128>;
650 fn to_usize(self) -> Option<usize>;
651
652 fn to_f32(self) -> Option<f32>;
653 fn to_f64(self) -> Option<f64>;
654}
655
656impl NumericConvert for u64 {
657 const TYPE_NAME: &'static str = "u64";
658
659 fn to_i8(self) -> Option<i8> {
660 self.try_into().ok()
661 }
662 fn to_i16(self) -> Option<i16> {
663 self.try_into().ok()
664 }
665 fn to_i32(self) -> Option<i32> {
666 self.try_into().ok()
667 }
668 fn to_i64(self) -> Option<i64> {
669 self.try_into().ok()
670 }
671 fn to_i128(self) -> Option<i128> {
672 Some(self as i128)
673 }
674 fn to_isize(self) -> Option<isize> {
675 self.try_into().ok()
676 }
677
678 fn to_u8(self) -> Option<u8> {
679 self.try_into().ok()
680 }
681 fn to_u16(self) -> Option<u16> {
682 self.try_into().ok()
683 }
684 fn to_u32(self) -> Option<u32> {
685 self.try_into().ok()
686 }
687 fn to_u64(self) -> Option<u64> {
688 Some(self)
689 }
690 fn to_u128(self) -> Option<u128> {
691 Some(self as u128)
692 }
693 fn to_usize(self) -> Option<usize> {
694 self.try_into().ok()
695 }
696
697 fn to_f32(self) -> Option<f32> {
698 Some(self as f32)
699 }
700 fn to_f64(self) -> Option<f64> {
701 Some(self as f64)
702 }
703}
704
705impl NumericConvert for i64 {
706 const TYPE_NAME: &'static str = "i64";
707
708 fn to_i8(self) -> Option<i8> {
709 self.try_into().ok()
710 }
711 fn to_i16(self) -> Option<i16> {
712 self.try_into().ok()
713 }
714 fn to_i32(self) -> Option<i32> {
715 self.try_into().ok()
716 }
717 fn to_i64(self) -> Option<i64> {
718 Some(self)
719 }
720 fn to_i128(self) -> Option<i128> {
721 Some(self as i128)
722 }
723 fn to_isize(self) -> Option<isize> {
724 self.try_into().ok()
725 }
726
727 fn to_u8(self) -> Option<u8> {
728 self.try_into().ok()
729 }
730 fn to_u16(self) -> Option<u16> {
731 self.try_into().ok()
732 }
733 fn to_u32(self) -> Option<u32> {
734 self.try_into().ok()
735 }
736 fn to_u64(self) -> Option<u64> {
737 self.try_into().ok()
738 }
739 fn to_u128(self) -> Option<u128> {
740 self.try_into().ok()
741 }
742 fn to_usize(self) -> Option<usize> {
743 self.try_into().ok()
744 }
745
746 fn to_f32(self) -> Option<f32> {
747 Some(self as f32)
748 }
749 fn to_f64(self) -> Option<f64> {
750 Some(self as f64)
751 }
752}
753
754impl NumericConvert for f64 {
755 const TYPE_NAME: &'static str = "f64";
756
757 fn to_i8(self) -> Option<i8> {
758 if has_no_fractional_part(self) && self >= i8::MIN as f64 && self <= i8::MAX as f64 {
759 Some(self as i8)
760 } else {
761 None
762 }
763 }
764 fn to_i16(self) -> Option<i16> {
765 if has_no_fractional_part(self) && self >= i16::MIN as f64 && self <= i16::MAX as f64 {
766 Some(self as i16)
767 } else {
768 None
769 }
770 }
771 fn to_i32(self) -> Option<i32> {
772 if has_no_fractional_part(self) && self >= i32::MIN as f64 && self <= i32::MAX as f64 {
773 Some(self as i32)
774 } else {
775 None
776 }
777 }
778 fn to_i64(self) -> Option<i64> {
779 if has_no_fractional_part(self) && self >= i64::MIN as f64 && self <= i64::MAX as f64 {
780 Some(self as i64)
781 } else {
782 None
783 }
784 }
785 fn to_i128(self) -> Option<i128> {
786 if has_no_fractional_part(self) && self >= i128::MIN as f64 && self <= i128::MAX as f64 {
787 Some(self as i128)
788 } else {
789 None
790 }
791 }
792 fn to_isize(self) -> Option<isize> {
793 if has_no_fractional_part(self) && self >= isize::MIN as f64 && self <= isize::MAX as f64 {
794 Some(self as isize)
795 } else {
796 None
797 }
798 }
799
800 fn to_u8(self) -> Option<u8> {
801 if has_no_fractional_part(self) && self >= 0.0 && self <= u8::MAX as f64 {
802 Some(self as u8)
803 } else {
804 None
805 }
806 }
807 fn to_u16(self) -> Option<u16> {
808 if has_no_fractional_part(self) && self >= 0.0 && self <= u16::MAX as f64 {
809 Some(self as u16)
810 } else {
811 None
812 }
813 }
814 fn to_u32(self) -> Option<u32> {
815 if has_no_fractional_part(self) && self >= 0.0 && self <= u32::MAX as f64 {
816 Some(self as u32)
817 } else {
818 None
819 }
820 }
821 fn to_u64(self) -> Option<u64> {
822 if has_no_fractional_part(self) && self >= 0.0 && self <= u64::MAX as f64 {
823 Some(self as u64)
824 } else {
825 None
826 }
827 }
828 fn to_u128(self) -> Option<u128> {
829 if has_no_fractional_part(self) && self >= 0.0 && self <= u128::MAX as f64 {
830 Some(self as u128)
831 } else {
832 None
833 }
834 }
835 fn to_usize(self) -> Option<usize> {
836 if has_no_fractional_part(self) && self >= 0.0 && self <= usize::MAX as f64 {
837 Some(self as usize)
838 } else {
839 None
840 }
841 }
842
843 fn to_f32(self) -> Option<f32> {
844 Some(self as f32)
845 }
846 fn to_f64(self) -> Option<f64> {
847 Some(self)
848 }
849}
850
851#[doc(hidden)]
852pub struct StackRunner<'input, C = Cooked, I: ?Sized + 'input = [u8]> {
857 pub original_input: &'input I,
859
860 pub input: &'input I,
862
863 pub stack: Vec<Instruction>,
865
866 pub substack: Substack<C>,
868
869 pub last_span: Span<C>,
871
872 pub format_source: &'static str,
874
875 pub array_indices: Vec<usize>,
877
878 pub enum_tuple_field_count: Option<usize>,
880
881 pub enum_tuple_current_field: Option<usize>,
883}
884
885impl<'input, 'shape, C, I: ?Sized + 'input> StackRunner<'input, C, I>
886where
887 I: InputDebug,
888{
889 fn err(&self, kind: DeserErrorKind<'shape>) -> DeserError<'input, 'shape, C> {
891 DeserError::new(
892 kind,
893 self.original_input,
894 self.last_span,
895 self.format_source,
896 )
897 }
898
899 fn reflect_err(&self, err: ReflectError<'shape>) -> DeserError<'input, 'shape, C> {
902 DeserError::new_reflect(err, self.original_input, self.last_span, self.format_source)
903 }
904
905 pub fn pop<'facet>(
906 &mut self,
907 mut wip: Partial<'facet, 'shape>,
908 reason: PopReason,
909 ) -> Result<Partial<'facet, 'shape>, DeserError<'input, 'shape, C>> {
910 trace!(
911 "--- STACK has {:?} {}",
912 self.stack.green(),
913 "(POP)".bright_yellow()
914 );
915 trace!("Popping because {:?}", reason.yellow());
916
917 let container_shape = wip.shape();
918 match container_shape.ty {
919 Type::User(UserType::Struct(sd)) => {
920 let mut has_unset = false;
921
922 trace!("Let's check all fields are initialized");
923 for (index, field) in sd.fields.iter().enumerate() {
924 let is_set = wip.is_field_set(index).map_err(|err| {
925 trace!("Error checking field set status: {:?}", err);
926 self.reflect_err(err)
927 })?;
928 if !is_set {
929 if field.flags.contains(FieldFlags::DEFAULT) {
930 wip.begin_nth_field(index)
931 .map_err(|e| self.reflect_err(e))?;
932
933 if let Some(field_default_fn) = field.vtable.default_fn {
935 wip.set_field_default(field_default_fn)
936 .map_err(|e| self.reflect_err(e))?;
937 trace!(
938 "Field #{} {} @ {} was set to default value (via field default function)",
939 index.yellow(),
940 field.name.green(),
941 field.offset.blue(),
942 );
943 } else if field.shape().is(Characteristic::Default) {
944 wip.set_default().map_err(|e| self.reflect_err(e))?;
945 trace!(
946 "Field #{} {} @ {} was set to default value (via type default impl)",
947 index.yellow(),
948 field.name.green(),
949 field.offset.blue(),
950 );
951 } else {
952 return Err(self.reflect_err(
953 ReflectError::DefaultAttrButNoDefaultImpl {
954 shape: field.shape(),
955 },
956 ));
957 }
958 wip.end().map_err(|e| self.reflect_err(e))?;
959 } else {
960 trace!(
961 "Field #{} {} @ {} is not initialized",
962 index.yellow(),
963 field.name.green(),
964 field.offset.blue(),
965 );
966 has_unset = true;
967 }
968 }
969 }
970
971 if has_unset {
972 if container_shape.has_default_attr() {
973 let default_val = Partial::alloc_shape(container_shape)
975 .map_err(|e| self.reflect_err(e))?
976 .set_default()
977 .map_err(|e| self.reflect_err(e))?
978 .build()
979 .map_err(|e| self.reflect_err(e))?;
980 let peek = default_val.peek().into_struct().unwrap();
981
982 for (index, field) in sd.fields.iter().enumerate() {
983 let is_set = wip.is_field_set(index).map_err(|err| {
984 trace!("Error checking field set status: {:?}", err);
985 self.reflect_err(err)
986 })?;
987 if !is_set {
988 trace!(
989 "Field #{} {} @ {} is being set to default value (from default instance)",
990 index.yellow(),
991 field.name.green(),
992 field.offset.blue(),
993 );
994 wip.begin_nth_field(index)
995 .map_err(|e| self.reflect_err(e))?;
996 let def_field = peek.field(index).unwrap();
998 wip.set_from_peek(&def_field)
999 .map_err(|e| self.reflect_err(e))?;
1000 wip.end().map_err(|e| self.reflect_err(e))?;
1001 }
1002 }
1003 } else {
1004 for (index, field) in sd.fields.iter().enumerate() {
1006 let is_set = wip.is_field_set(index).map_err(|err| {
1007 trace!("Error checking field set status: {:?}", err);
1008 self.reflect_err(err)
1009 })?;
1010 if !is_set {
1011 return Err(self.reflect_err(ReflectError::UninitializedField {
1012 shape: container_shape,
1013 field_name: field.name,
1014 }));
1015 }
1016 }
1017 }
1018 }
1019 }
1020 Type::User(UserType::Enum(ed)) => {
1021 trace!("Checking if enum is initialized correctly");
1022
1023 if let Some(variant) = wip.selected_variant() {
1025 trace!("Variant {} is selected", variant.name.blue());
1026
1027 if !variant.data.fields.is_empty() {
1029 let mut has_unset = false;
1030
1031 for (index, field) in variant.data.fields.iter().enumerate() {
1032 let is_set = wip.is_field_set(index).map_err(|err| {
1033 trace!("Error checking field set status: {:?}", err);
1034 self.reflect_err(err)
1035 })?;
1036
1037 if !is_set {
1038 if field.flags.contains(FieldFlags::DEFAULT) {
1039 wip.begin_nth_field(index)
1040 .map_err(|e| self.reflect_err(e))?;
1041
1042 if let Some(field_default_fn) = field.vtable.default_fn {
1044 wip.set_field_default(field_default_fn)
1045 .map_err(|e| self.reflect_err(e))?;
1046 trace!(
1047 "Field #{} @ {} in variant {} was set to default value (via field default function)",
1048 index.yellow(),
1049 field.offset.blue(),
1050 variant.name
1051 );
1052 } else if field.shape().is(Characteristic::Default) {
1053 wip.set_default().map_err(|e| self.reflect_err(e))?;
1054 trace!(
1055 "Field #{} @ {} in variant {} was set to default value (via type default impl)",
1056 index.yellow(),
1057 field.offset.blue(),
1058 variant.name
1059 );
1060 } else {
1061 return Err(self.reflect_err(
1062 ReflectError::DefaultAttrButNoDefaultImpl {
1063 shape: field.shape(),
1064 },
1065 ));
1066 }
1067 wip.end().map_err(|e| self.reflect_err(e))?;
1068 } else {
1069 trace!(
1070 "Field #{} @ {} in variant {} is not initialized",
1071 index.yellow(),
1072 field.offset.blue(),
1073 variant.name
1074 );
1075 has_unset = true;
1076 }
1077 }
1078 }
1079
1080 if has_unset {
1081 if container_shape.has_default_attr() {
1082 trace!(
1083 "Enum has DEFAULT attr but variant has uninitialized fields"
1084 );
1085 let default_val = Partial::alloc_shape(container_shape)
1087 .map_err(|e| self.reflect_err(e))?
1088 .set_default()
1089 .map_err(|e| self.reflect_err(e))?
1090 .build()
1091 .map_err(|e| self.reflect_err(e))?;
1092
1093 let peek = default_val.peek();
1094 let peek_enum =
1095 peek.into_enum().map_err(|e| self.reflect_err(e))?;
1096 let default_variant = peek_enum
1097 .active_variant()
1098 .map_err(|e| self.err(DeserErrorKind::VariantError(e)))?;
1099
1100 if default_variant == &variant {
1101 for (index, _field) in variant.data.fields.iter().enumerate() {
1103 let is_set = wip.is_field_set(index).map_err(|err| {
1104 trace!("Error checking field set status: {:?}", err);
1105 self.reflect_err(err)
1106 })?;
1107 if !is_set {
1108 if let Ok(Some(def_field)) = peek_enum.field(index) {
1109 wip.begin_nth_field(index)
1110 .map_err(|e| self.reflect_err(e))?;
1111 wip.set_from_peek(&def_field)
1112 .map_err(|e| self.reflect_err(e))?;
1113 wip.end().map_err(|e| self.reflect_err(e))?;
1114 }
1115 }
1116 }
1117 }
1118 } else {
1119 for (index, field) in variant.data.fields.iter().enumerate() {
1121 let is_set = wip.is_field_set(index).map_err(|err| {
1122 trace!("Error checking field set status: {:?}", err);
1123 self.reflect_err(err)
1124 })?;
1125 if !is_set {
1126 return Err(self.reflect_err(
1127 ReflectError::UninitializedEnumField {
1128 shape: container_shape,
1129 variant_name: variant.name,
1130 field_name: field.name,
1131 },
1132 ));
1133 }
1134 }
1135 }
1136 }
1137 }
1138 } else if container_shape.has_default_attr() {
1139 trace!("No variant selected but enum has DEFAULT attr; setting to default");
1141 let default_val = Partial::alloc_shape(container_shape)
1142 .map_err(|e| self.reflect_err(e))?
1143 .set_default()
1144 .map_err(|e| self.reflect_err(e))?
1145 .build()
1146 .map_err(|e| self.reflect_err(e))?;
1147
1148 let peek = default_val.peek();
1149 let peek_enum = peek.into_enum().map_err(|e| self.reflect_err(e))?;
1150 let default_variant_idx = peek_enum
1151 .variant_index()
1152 .map_err(|e| self.err(DeserErrorKind::VariantError(e)))?;
1153
1154 wip.select_nth_variant(default_variant_idx)
1156 .map_err(|e| self.reflect_err(e))?;
1157
1158 let variant = &ed.variants[default_variant_idx];
1160 for (index, _field) in variant.data.fields.iter().enumerate() {
1161 if let Ok(Some(def_field)) = peek_enum.field(index) {
1162 wip.begin_nth_field(index)
1163 .map_err(|e| self.reflect_err(e))?;
1164 wip.set_from_peek(&def_field)
1165 .map_err(|e| self.reflect_err(e))?;
1166 wip.end().map_err(|e| self.reflect_err(e))?;
1167 }
1168 }
1169 }
1170 }
1171 _ => {
1172 trace!(
1173 "Thing being popped is not a container I guess (it's a {}, innermost is {})",
1174 wip.shape(),
1175 wip.innermost_shape()
1176 );
1177 }
1178 }
1179 Ok(wip)
1180 }
1181
1182 fn set_numeric_value<'facet, N>(
1185 &self,
1186 wip: &mut Partial<'facet, 'shape>,
1187 value: N,
1188 ) -> Result<(), DeserError<'input, 'shape, C>>
1189 where
1190 'input: 'facet,
1191 N: NumericConvert,
1192 {
1193 let shape = wip.innermost_shape();
1194
1195 if let Def::Scalar(sd) = shape.def {
1197 if let ScalarAffinity::Number(num_affinity) = sd.affinity {
1198 use facet_core::{IntegerSize, NumberBits, Signedness};
1199
1200 macro_rules! convert_and_set {
1202 ($converter:expr, $target_type:expr) => {{
1203 let converted = $converter(value).ok_or_else(|| {
1204 self.err(DeserErrorKind::NumericConversion {
1205 from: N::TYPE_NAME,
1206 to: $target_type,
1207 })
1208 })?;
1209 wip.set(converted).map_err(|e| self.reflect_err(e))?;
1210 }};
1211 }
1212
1213 match num_affinity.bits {
1215 NumberBits::Integer { size, sign } => {
1216 match (size, sign) {
1218 (IntegerSize::Fixed(bits), Signedness::Signed) => match bits {
1219 8 => convert_and_set!(N::to_i8, "i8"),
1220 16 => convert_and_set!(N::to_i16, "i16"),
1221 32 => convert_and_set!(N::to_i32, "i32"),
1222 64 => convert_and_set!(N::to_i64, "i64"),
1223 128 => convert_and_set!(N::to_i128, "i128"),
1224 _ => {
1225 return Err(self.err(DeserErrorKind::NumericConversion {
1226 from: N::TYPE_NAME,
1227 to: "unknown fixed-size signed integer",
1228 }));
1229 }
1230 },
1231 (IntegerSize::Fixed(bits), Signedness::Unsigned) => match bits {
1232 8 => convert_and_set!(N::to_u8, "u8"),
1233 16 => convert_and_set!(N::to_u16, "u16"),
1234 32 => convert_and_set!(N::to_u32, "u32"),
1235 64 => convert_and_set!(N::to_u64, "u64"),
1236 128 => convert_and_set!(N::to_u128, "u128"),
1237 _ => {
1238 return Err(self.err(DeserErrorKind::NumericConversion {
1239 from: N::TYPE_NAME,
1240 to: "unknown fixed-size unsigned integer",
1241 }));
1242 }
1243 },
1244 (IntegerSize::PointerSized, Signedness::Signed) => {
1245 convert_and_set!(N::to_isize, "isize")
1246 }
1247 (IntegerSize::PointerSized, Signedness::Unsigned) => {
1248 convert_and_set!(N::to_usize, "usize")
1249 }
1250 }
1251 }
1252 NumberBits::Float {
1253 sign_bits,
1254 exponent_bits,
1255 mantissa_bits,
1256 ..
1257 } => {
1258 let total_bits = sign_bits + exponent_bits + mantissa_bits;
1260 match total_bits {
1261 32 => convert_and_set!(N::to_f32, "f32"),
1262 64 => convert_and_set!(N::to_f64, "f64"),
1263 _ => {
1264 return Err(self.err(DeserErrorKind::NumericConversion {
1266 from: N::TYPE_NAME,
1267 to: "unknown float size",
1268 }));
1269 }
1270 }
1271 }
1272 _ => {
1273 return Err(self.err(DeserErrorKind::NumericConversion {
1275 from: N::TYPE_NAME,
1276 to: "fixed-point or decimal",
1277 }));
1278 }
1279 }
1280 } else {
1281 return Err(self.err(DeserErrorKind::UnsupportedType {
1283 got: shape,
1284 wanted: "numeric type",
1285 }));
1286 }
1287 } else {
1288 return Err(self.err(DeserErrorKind::UnsupportedType {
1290 got: shape,
1291 wanted: "scalar type",
1292 }));
1293 }
1294
1295 Ok(())
1296 }
1297
1298 fn handle_scalar<'facet>(
1299 &self,
1300 wip: &mut Partial<'facet, 'shape>,
1301 scalar: Scalar<'input>,
1302 ) -> Result<(), DeserError<'input, 'shape, C>>
1303 where
1304 'input: 'facet, {
1306 match scalar {
1307 Scalar::String(cow) => {
1308 match wip.innermost_shape().ty {
1309 Type::User(UserType::Enum(_)) => {
1310 if wip.selected_variant().is_some() {
1311 wip.set(cow).map_err(|e| self.reflect_err(e))?;
1313 } else {
1314 match wip.find_variant(&cow) {
1316 Some((variant_index, _)) => {
1317 wip.select_nth_variant(variant_index)
1318 .map_err(|e| self.reflect_err(e))?;
1319 }
1320 None => {
1321 return Err(self.err(DeserErrorKind::NoSuchVariant {
1322 name: cow.to_string(),
1323 enum_shape: wip.innermost_shape(),
1324 }));
1325 }
1326 }
1327 }
1328 }
1329 Type::Pointer(PointerType::Reference(_))
1330 if wip.innermost_shape().is_type::<&str>() =>
1331 {
1332 match cow {
1335 Cow::Borrowed(s) => wip.set(s).map_err(|e| self.reflect_err(e))?,
1336 Cow::Owned(s) => wip.set(s).map_err(|e| self.reflect_err(e))?,
1337 }; }
1339 _ => {
1340 let shape = wip.innermost_shape();
1342 if let Def::Scalar(scalar_def) = shape.def {
1343 if !matches!(scalar_def.affinity, facet_core::ScalarAffinity::String(_))
1346 {
1347 match wip.parse_from_str(cow.as_ref()) {
1349 Ok(_) => {
1350 }
1352 Err(parse_err) => {
1353 match parse_err {
1356 ReflectError::OperationFailed {
1357 shape: _,
1358 operation,
1359 } if operation.contains("does not support parsing") => {
1360 wip.set(cow.to_string())
1362 .map_err(|e| self.reflect_err(e))?;
1363 }
1364 _ => {
1365 return Err(self.err(DeserErrorKind::ReflectError(
1367 ReflectError::OperationFailed {
1368 shape,
1369 operation: "Failed to parse string value",
1370 }
1371 )));
1372 }
1373 }
1374 }
1375 }
1376 } else {
1377 wip.set(cow.to_string()).map_err(|e| self.reflect_err(e))?;
1379 }
1380 } else {
1381 wip.set(cow.to_string()).map_err(|e| self.reflect_err(e))?;
1383 }
1384 }
1385 }
1386 }
1387 Scalar::U64(value) => {
1388 self.set_numeric_value(wip, value)?;
1389 }
1390 Scalar::I64(value) => {
1391 self.set_numeric_value(wip, value)?;
1392 }
1393 Scalar::F64(value) => {
1394 self.set_numeric_value(wip, value)?;
1395 }
1396 Scalar::Bool(value) => {
1397 wip.set(value).map_err(|e| self.reflect_err(e))?;
1398 }
1399 Scalar::Null => {
1400 wip.set_default().map_err(|e| self.reflect_err(e))?;
1401 }
1402 }
1403 Ok(())
1404 }
1405
1406 fn value<'facet>(
1408 &mut self,
1409 mut wip: Partial<'facet, 'shape>,
1410 outcome: Spanned<Outcome<'input>, C>,
1411 ) -> Result<Partial<'facet, 'shape>, DeserError<'input, 'shape, C>>
1412 where
1413 'input: 'facet, {
1415 trace!(
1416 "--- STACK has {:?} {}",
1417 self.stack.green(),
1418 "(VALUE)".bright_yellow()
1419 );
1420
1421 let original_shape = wip.shape();
1422 trace!("Handling value of type {}", original_shape.blue());
1423
1424 if matches!(outcome.node, Outcome::Scalar(Scalar::Null)) {
1426 wip.set_default().map_err(|e| self.reflect_err(e))?;
1427 return Ok(wip);
1428 }
1429
1430 loop {
1432 if matches!(wip.shape().def, Def::Option(_)) {
1433 trace!(" Starting Some(_) option for {}", wip.shape().blue());
1434 wip.begin_some().map_err(|e| self.reflect_err(e))?;
1435 self.stack.push(Instruction::Pop(PopReason::Some));
1436 } else if let Def::SmartPointer(inner) = wip.shape().def {
1437 if let Some(pointee) = inner.pointee() {
1438 trace!(
1439 " Starting smart pointer for {} (pointee is {})",
1440 wip.shape().blue(),
1441 pointee.yellow(),
1442 );
1443 } else {
1444 trace!(
1445 " Starting smart pointer for {} (no pointee)",
1446 wip.shape().blue()
1447 );
1448 }
1449 wip.begin_smart_ptr().map_err(|e| self.reflect_err(e))?;
1450 self.stack.push(Instruction::Pop(PopReason::SmartPointer));
1451 } else if let Some(inner_fn) = wip.shape().inner {
1452 let inner = inner_fn();
1453 trace!(
1454 " Starting wrapped value for {} (inner is {})",
1455 wip.shape().blue(),
1456 inner.yellow()
1457 );
1458 wip.begin_inner().map_err(|e| self.reflect_err(e))?;
1459 self.stack.push(Instruction::Pop(PopReason::Wrapper));
1460 } else {
1461 break;
1462 }
1463 }
1464
1465 if wip.shape() != original_shape {
1466 trace!(
1467 "Handling shape {} as innermost {}",
1468 original_shape.blue(),
1469 wip.shape().yellow()
1470 );
1471 }
1472
1473 match outcome.node {
1474 Outcome::Scalar(s) => {
1475 trace!("Parsed scalar value: {}", s.cyan());
1476 self.handle_scalar(&mut wip, s)?;
1477 }
1478 Outcome::ListStarted => {
1479 let shape = wip.innermost_shape();
1480
1481 if let Type::User(UserType::Struct(st)) = shape.ty {
1483 if st.kind == StructKind::Tuple {
1484 trace!(
1485 "Array starting for tuple struct ({}) with {} fields!",
1486 shape.blue(),
1487 st.fields.len()
1488 );
1489
1490 trace!("Beginning pushback");
1492 self.stack.push(Instruction::ListItemOrListClose);
1493 return Ok(wip);
1494 }
1495 }
1496
1497 match shape.def {
1498 Def::Array(_) => {
1499 trace!("Array starting for array ({})!", shape.blue());
1500 }
1503 Def::Slice(_) => {
1504 trace!("Array starting for slice ({})!", shape.blue());
1505 }
1506 Def::List(_) => {
1507 trace!("Array starting for list ({})!", shape.blue());
1508 wip.set_default().map_err(|e| self.reflect_err(e))?;
1509 }
1510 _ => {
1511 if let Type::User(user_ty) = shape.ty {
1513 match user_ty {
1514 UserType::Enum(_) => {
1515 trace!("Array starting for enum ({})!", shape.blue());
1516 if let Some(variant) = wip.selected_variant() {
1518 use facet_core::StructKind;
1519 if variant.data.kind == StructKind::Tuple {
1520 self.enum_tuple_field_count =
1523 Some(variant.data.fields.len());
1524 self.enum_tuple_current_field = Some(0);
1525 } else {
1526 return Err(self.err(DeserErrorKind::UnsupportedType {
1527 got: shape,
1528 wanted: "tuple variant for array deserialization",
1529 }));
1530 }
1531 } else {
1532 return Err(self.err(DeserErrorKind::UnsupportedType {
1533 got: shape,
1534 wanted: "enum with variant selected",
1535 }));
1536 }
1537 }
1538 UserType::Struct(_) => {
1539 return Err(self.err(DeserErrorKind::UnsupportedType {
1542 got: shape,
1543 wanted: "array, list, tuple, or slice",
1544 }));
1545 }
1546 _ => {
1547 return Err(self.err(DeserErrorKind::UnsupportedType {
1548 got: shape,
1549 wanted: "array, list, tuple, or slice",
1550 }));
1551 }
1552 }
1553 } else {
1554 return Err(self.err(DeserErrorKind::UnsupportedType {
1555 got: shape,
1556 wanted: "array, list, tuple, or slice",
1557 }));
1558 }
1559 }
1560 }
1561 trace!("Beginning pushback");
1562 self.stack.push(Instruction::ListItemOrListClose);
1563
1564 match shape.def {
1566 Def::List(_) => {
1567 wip.begin_list().map_err(|e| self.reflect_err(e))?;
1568 }
1569 Def::Array(_) => {
1570 self.array_indices.push(0);
1573 }
1574 Def::Slice(_) => {
1575 }
1578 _ => {
1579 }
1581 }
1582 }
1583 Outcome::ListEnded => {
1584 trace!("List closing");
1585 let shape = wip.shape();
1587 if matches!(shape.def, Def::Array(_)) {
1588 self.array_indices.pop();
1589 }
1590 wip.end().map_err(|e| self.reflect_err(e))?;
1591 }
1592 Outcome::ObjectStarted => {
1593 let shape = wip.shape();
1594 match shape.def {
1595 Def::Map(_md) => {
1596 trace!("Object starting for map value ({})!", shape.blue());
1597 wip.begin_map().map_err(|e| self.reflect_err(e))?;
1598 }
1599 _ => {
1600 if let Type::User(user_ty) = shape.ty {
1602 match user_ty {
1603 UserType::Enum(_) => {
1604 trace!("Object starting for enum value ({})!", shape.blue());
1605 }
1607 UserType::Struct(_) => {
1608 trace!("Object starting for struct value ({})!", shape.blue());
1609 }
1611 _ => {
1612 return Err(self.err(DeserErrorKind::UnsupportedType {
1613 got: shape,
1614 wanted: "map, enum, or struct",
1615 }));
1616 }
1617 }
1618 } else if let Type::User(UserType::Struct(struct_type)) = shape.ty {
1619 if struct_type.kind == StructKind::Tuple {
1620 trace!(
1623 "Object starting for tuple ({}) with {} fields - unusual but handling",
1624 shape.blue(),
1625 struct_type.fields.len()
1626 );
1627 }
1629 } else {
1630 return Err(self.err(DeserErrorKind::UnsupportedType {
1631 got: shape,
1632 wanted: "map, enum, struct, or tuple",
1633 }));
1634 }
1635 }
1636 }
1637
1638 self.stack.push(Instruction::ObjectKeyOrObjectClose);
1639 }
1640 Outcome::Resegmented(subspans) => {
1641 trace!("Resegmented with {} subspans (value)", subspans.len());
1642 }
1649 Outcome::ObjectEnded => todo!(),
1650 }
1651 Ok(wip)
1652 }
1653
1654 fn object_key_or_object_close<'facet>(
1655 &mut self,
1656 mut wip: Partial<'facet, 'shape>,
1657 outcome: Spanned<Outcome<'input>, C>,
1658 ) -> Result<Partial<'facet, 'shape>, DeserError<'input, 'shape, C>>
1659 where
1660 'input: 'facet,
1661 {
1662 trace!(
1663 "STACK: {:?} {}",
1664 self.stack.green(),
1665 "(OK/OC)".bright_yellow()
1666 );
1667 trace!("SUBSTACK: {:?}", self.substack.get().bright_green());
1668 match outcome.node {
1669 Outcome::Scalar(Scalar::String(key)) => {
1670 trace!("Parsed object key: {}", key.cyan());
1671
1672 let mut ignore = false;
1673 let mut needs_pop = true;
1674 let mut handled_by_flatten = false;
1675 let has_substack = !self.substack.get().is_empty();
1676
1677 let shape = wip.innermost_shape();
1678 match shape.ty {
1679 Type::User(UserType::Struct(sd)) => {
1680 if let Some(index) = wip.field_index(&key) {
1682 trace!("It's a struct field");
1683 wip.begin_nth_field(index)
1684 .map_err(|e| self.reflect_err(e))?;
1685 } else {
1686 trace!(
1687 "Did not find direct field match in innermost shape {}",
1688 shape.blue()
1689 );
1690
1691 let mut found_in_flatten = false;
1693 for (index, field) in sd.fields.iter().enumerate() {
1694 if field.flags.contains(FieldFlags::FLATTEN) {
1695 trace!("Found flattened field #{}", index);
1696 wip.begin_nth_field(index)
1698 .map_err(|e| self.reflect_err(e))?;
1699
1700 if let Some(subfield_index) = wip.field_index(&key) {
1702 trace!("Found key {} in flattened field", key);
1703 wip.begin_nth_field(subfield_index)
1704 .map_err(|e| self.reflect_err(e))?;
1705 found_in_flatten = true;
1706 handled_by_flatten = true;
1707 break;
1708 } else if let Some((_variant_index, _variant)) =
1709 wip.find_variant(&key)
1710 {
1711 trace!("Found key {} in flattened field", key);
1712 wip.select_variant_named(&key)
1713 .map_err(|e| self.reflect_err(e))?;
1714 found_in_flatten = true;
1715 break;
1716 } else {
1717 wip.end().map_err(|e| self.reflect_err(e))?;
1719 }
1720 }
1721 }
1722
1723 if !found_in_flatten {
1724 if wip.shape().has_deny_unknown_fields_attr() {
1725 trace!(
1726 "It's not a struct field AND we're denying unknown fields"
1727 );
1728 return Err(self.err(DeserErrorKind::UnknownField {
1729 field_name: key.to_string(),
1730 shape: wip.shape(),
1731 }));
1732 } else {
1733 trace!(
1734 "It's not a struct field and we're ignoring unknown fields"
1735 );
1736 ignore = true;
1737 }
1738 }
1739 }
1740 }
1741 Type::User(UserType::Enum(_ed)) => match wip.find_variant(&key) {
1742 Some((index, variant)) => {
1743 trace!(
1744 "Selecting variant {}::{}",
1745 wip.shape().blue(),
1746 variant.name.yellow(),
1747 );
1748 wip.select_nth_variant(index)
1749 .map_err(|e| self.reflect_err(e))?;
1750
1751 if matches!(variant.data.kind, StructKind::Tuple)
1753 && variant.data.fields.len() == 1
1754 {
1755 trace!(
1756 "Tuple variant {}::{} encountered, pushing field 0",
1757 wip.shape().blue(),
1758 variant.name.yellow()
1759 );
1760 wip.begin_nth_field(0).map_err(|e| self.reflect_err(e))?;
1761 self.stack.push(Instruction::Pop(PopReason::ObjectVal));
1762 }
1763
1764 needs_pop = false;
1765 }
1766 None => {
1767 if let Some(_variant_index) = wip.selected_variant() {
1768 trace!(
1769 "Already have a variant selected, treating {} as struct field of {}::{}",
1770 key,
1771 wip.shape().blue(),
1772 wip.selected_variant().unwrap().name.yellow(),
1773 );
1774 if let Some(index) = wip.field_index(&key) {
1776 trace!("Found field {} in selected variant", key.blue());
1777 wip.begin_nth_field(index)
1778 .map_err(|e| self.reflect_err(e))?;
1779 } else if wip.shape().has_deny_unknown_fields_attr() {
1780 trace!("Unknown field in variant and denying unknown fields");
1781 return Err(self.err(DeserErrorKind::UnknownField {
1782 field_name: key.to_string(),
1783 shape: wip.shape(),
1784 }));
1785 } else {
1786 trace!(
1787 "Ignoring unknown field '{}' in variant '{}::{}'",
1788 key,
1789 wip.shape(),
1790 wip.selected_variant().unwrap().name
1791 );
1792 ignore = true;
1793 }
1794 } else {
1795 return Err(self.err(DeserErrorKind::NoSuchVariant {
1796 name: key.to_string(),
1797 enum_shape: wip.shape(),
1798 }));
1799 }
1800 }
1801 },
1802 _ => {
1803 if let Def::Map(map_def) = shape.def {
1805 wip.begin_key().map_err(|e| self.reflect_err(e))?;
1806
1807 let key_shape = map_def.k();
1809 if key_shape.inner.is_some() {
1810 wip.begin_inner().map_err(|e| self.reflect_err(e))?;
1814 wip.set(key.to_string()).map_err(|e| self.reflect_err(e))?;
1815 wip.end().map_err(|e| self.reflect_err(e))?; } else {
1817 wip.set(key.to_string()).map_err(|e| self.reflect_err(e))?;
1819 }
1820
1821 wip.end().map_err(|e| self.reflect_err(e))?; wip.begin_value().map_err(|e| self.reflect_err(e))?;
1823 } else {
1824 return Err(self.err(DeserErrorKind::Unimplemented(
1825 "object key for non-struct/map",
1826 )));
1827 }
1828 }
1829 }
1830
1831 self.stack.push(Instruction::ObjectKeyOrObjectClose);
1832 if ignore {
1833 self.stack.push(Instruction::SkipValue);
1834 } else {
1835 if needs_pop && !handled_by_flatten {
1836 trace!("Pushing Pop insn to stack (ObjectVal)");
1837 self.stack.push(Instruction::Pop(PopReason::ObjectVal));
1838 if has_substack {
1839 trace!("Pushing SubstackClose insn to stack");
1840 self.stack.push(Instruction::SubstackClose);
1841 }
1842 } else if handled_by_flatten {
1843 trace!("Pushing Pop insn to stack (ObjectVal) for flattened field");
1846 self.stack.push(Instruction::Pop(PopReason::ObjectVal));
1847 if has_substack {
1848 trace!("Pushing SubstackClose insn to stack");
1849 self.stack.push(Instruction::SubstackClose);
1850 }
1851 }
1852 self.stack.push(Instruction::Value(ValueReason::ObjectVal));
1853 }
1854 Ok(wip)
1855 }
1856 Outcome::ObjectEnded => {
1857 trace!("Object closing");
1858 Ok(wip)
1859 }
1860 Outcome::Resegmented(subspans) => {
1861 trace!(
1862 "Resegmented into {} subspans ({:?}) - obj. key/close",
1863 subspans.len(),
1864 subspans
1865 );
1866 self.stack.push(Instruction::ObjectKeyOrObjectClose);
1868 Ok(wip)
1869 }
1870 _ => Err(self.err(DeserErrorKind::UnexpectedOutcome {
1871 got: outcome.node.into_owned(),
1872 wanted: "scalar or object close",
1873 })),
1874 }
1875 }
1876
1877 fn list_item_or_list_close<'facet>(
1878 &mut self,
1879 mut wip: Partial<'facet, 'shape>,
1880 outcome: Spanned<Outcome<'input>, C>,
1881 ) -> Result<Partial<'facet, 'shape>, DeserError<'input, 'shape, C>>
1882 where
1883 'input: 'facet,
1884 {
1885 trace!(
1886 "--- STACK has {:?} {}",
1887 self.stack.green(),
1888 "(LI/LC)".bright_yellow()
1889 );
1890 match outcome.node {
1891 Outcome::ListEnded => {
1892 trace!("List close");
1893 let shape = wip.shape();
1895 if matches!(shape.def, Def::Array(_)) {
1896 self.array_indices.pop();
1897 }
1898
1899 if let Type::User(UserType::Enum(_)) = shape.ty {
1901 if self.enum_tuple_field_count.is_some() {
1902 trace!("Enum tuple variant list ended");
1903 self.enum_tuple_field_count = None;
1904 self.enum_tuple_current_field = None;
1905 }
1906 }
1907
1908 if let Type::User(UserType::Struct(st)) = shape.ty {
1910 if st.kind == StructKind::Tuple && st.fields.is_empty() {
1911 trace!("Empty tuple parsed from []");
1912 }
1914 }
1915
1916 Ok(wip)
1918 }
1919 _ => {
1920 self.stack.push(Instruction::ListItemOrListClose);
1921 self.stack.push(Instruction::Pop(PopReason::ListVal));
1922
1923 trace!(
1924 "Expecting list item, doing a little push before doing value with outcome {}",
1925 outcome.magenta()
1926 );
1927 trace!("Before push, wip.shape is {}", wip.shape().blue());
1928
1929 let shape = wip.shape();
1931 match shape.def {
1932 Def::Array(ad) => {
1933 if let Some(current_index) = self.array_indices.last().copied() {
1935 if current_index >= ad.n {
1937 return Err(self.err(DeserErrorKind::ArrayOverflow {
1938 shape,
1939 max_len: ad.n,
1940 }));
1941 }
1942
1943 wip.begin_nth_element(current_index)
1945 .map_err(|e| self.reflect_err(e))?;
1946
1947 if let Some(last) = self.array_indices.last_mut() {
1949 *last += 1;
1950 }
1951 } else {
1952 return Err(self.err(DeserErrorKind::Unimplemented(
1954 "Array index tracking not initialized",
1955 )));
1956 }
1957 }
1958 Def::List(_) => {
1959 wip.begin_list_item().map_err(|e| self.reflect_err(e))?;
1960 }
1961 _ => {
1962 if let Type::User(UserType::Enum(_)) = shape.ty {
1964 if let (Some(field_count), Some(current_field)) =
1965 (self.enum_tuple_field_count, self.enum_tuple_current_field)
1966 {
1967 if current_field >= field_count {
1968 return Err(self.err(DeserErrorKind::ArrayOverflow {
1970 shape,
1971 max_len: field_count,
1972 }));
1973 }
1974
1975 wip.begin_nth_enum_field(current_field)
1977 .map_err(|e| self.reflect_err(e))?;
1978
1979 self.enum_tuple_current_field = Some(current_field + 1);
1981 } else {
1982 return Err(self.err(DeserErrorKind::UnsupportedType {
1983 got: shape,
1984 wanted: "enum with tuple variant selected",
1985 }));
1986 }
1987 }
1988 else if let Type::User(UserType::Struct(struct_type)) = shape.ty {
1990 if struct_type.kind == StructKind::Tuple {
1991 let mut field_index = None;
1994 for i in 0..struct_type.fields.len() {
1995 if !wip.is_field_set(i).map_err(|e| self.reflect_err(e))? {
1996 field_index = Some(i);
1997 break;
1998 }
1999 }
2000
2001 if let Some(idx) = field_index {
2002 wip.begin_nth_field(idx).map_err(|e| self.reflect_err(e))?;
2003 } else {
2004 return Err(self.err(DeserErrorKind::ArrayOverflow {
2006 shape,
2007 max_len: struct_type.fields.len(),
2008 }));
2009 }
2010 } else {
2011 return Err(self.err(DeserErrorKind::UnsupportedType {
2013 got: shape,
2014 wanted: "array, list, or tuple",
2015 }));
2016 }
2017 } else {
2018 return Err(self.err(DeserErrorKind::UnsupportedType {
2020 got: shape,
2021 wanted: "array, list, or tuple",
2022 }));
2023 }
2024 }
2025 }
2026
2027 trace!(" After push, wip.shape is {}", wip.shape().cyan());
2028
2029 if matches!(outcome.node, Outcome::ListStarted) {
2032 if let Type::User(UserType::Struct(st)) = wip.shape().ty {
2033 if st.kind == StructKind::Tuple && st.fields.is_empty() {
2034 trace!(
2035 "Empty tuple field with list start - initializing empty tuple and expecting immediate close"
2036 );
2037 wip.set_default().map_err(|e| self.reflect_err(e))?;
2039 }
2041 }
2042 }
2043
2044 wip = self.value(wip, outcome)?;
2045 Ok(wip)
2046 }
2047 }
2048 }
2049}