facet_msgpack/
deserialize.rs

1use crate::constants::*;
2use crate::errors::Error as DecodeError;
3
4use facet_core::{Def, Facet, Type, UserType};
5use facet_reflect::Partial;
6use log::trace;
7
8/// Deserializes MessagePack-encoded data into a type that implements `Facet`.
9///
10/// # Example
11/// ```
12/// use facet::Facet;
13/// use facet_msgpack::from_slice;
14///
15/// #[derive(Debug, Facet, PartialEq)]
16/// struct User {
17///     id: u64,
18///     username: String,
19/// }
20///
21/// // MessagePack binary data (equivalent to {"id": 42, "username": "user123"})
22/// let msgpack_data = [
23///     0x82, 0xa2, 0x69, 0x64, 0x2a, 0xa8, 0x75, 0x73,
24///     0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0xa7, 0x75,
25///     0x73, 0x65, 0x72, 0x31, 0x32, 0x33
26/// ];
27///
28/// let user: User = from_slice(&msgpack_data).unwrap();
29/// assert_eq!(user, User { id: 42, username: "user123".to_string() });
30/// ```
31pub fn from_slice<T: Facet<'static>>(msgpack: &[u8]) -> Result<T, DecodeError> {
32    // FIXME: why is the bound `'static` up there? msgpack can borrow from the input afaict?
33
34    trace!("from_slice: Starting deserialization for type {}", T::SHAPE);
35    let partial = Partial::alloc::<T>()?;
36    trace!(
37        "from_slice: Allocated Partial, inner shape: {}",
38        partial.shape()
39    );
40
41    let partial = from_slice_value(msgpack, partial)?;
42
43    trace!("from_slice: Deserialization complete, building value");
44    let heap_value = partial.build()?;
45    trace!("from_slice: Value built successfully");
46    let value = heap_value.materialize()?;
47    Ok(value)
48}
49
50/// Deserializes MessagePack-encoded data into a Facet value.
51///
52/// This function takes a MessagePack byte array and populates a Partial object
53/// according to the shape description, returning an Opaque value.
54///
55/// # Example
56///
57/// ```
58/// use facet::Facet;
59/// use facet_msgpack::from_slice;
60///
61/// #[derive(Debug, Facet, PartialEq)]
62/// struct User {
63///     id: u64,
64///     username: String,
65/// }
66///
67/// // MessagePack binary data (equivalent to {"id": 42, "username": "user123"})
68/// let msgpack_data = [
69///     0x82, 0xa2, 0x69, 0x64, 0x2a, 0xa8, 0x75, 0x73,
70///     0x65, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0xa7, 0x75,
71///     0x73, 0x65, 0x72, 0x31, 0x32, 0x33
72/// ];
73///
74/// let user: User = from_slice(&msgpack_data).unwrap();
75/// assert_eq!(user, User { id: 42, username: "user123".to_string() });
76/// ```
77///
78/// # Parameters
79/// * `wip` - A Partial object that will be filled with deserialized data
80/// * `msgpack` - A byte slice containing MessagePack-encoded data
81///
82/// # Returns
83/// * `Ok(Opaque)` containing the deserialized data if successful
84/// * `Err(DecodeError)` if an error occurred during deserialization
85///
86/// # MessagePack Format
87/// This implementation follows the MessagePack specification:
88/// <https://github.com/msgpack/msgpack/blob/master/spec.md>
89pub fn from_slice_value<'facet>(
90    msgpack: &[u8],
91    wip: Partial<'facet>,
92) -> Result<Partial<'facet>, DecodeError> {
93    trace!("from_slice_value: Starting with shape {}", wip.shape());
94    let mut decoder = Decoder::new(msgpack);
95    let result = decoder.deserialize_value(wip);
96    match &result {
97        Ok(_) => trace!("from_slice_value: Deserialization successful"),
98        Err(e) => trace!("from_slice_value: Deserialization failed: {e:?}"),
99    }
100    result
101}
102
103struct Decoder<'input> {
104    input: &'input [u8],
105    offset: usize,
106}
107
108impl<'input> Decoder<'input> {
109    fn new(input: &'input [u8]) -> Self {
110        Decoder { input, offset: 0 }
111    }
112
113    /// Decodes a single byte from the input.
114    /// This is a low-level method used by other decoders.
115    fn decode_u8(&mut self) -> Result<u8, DecodeError> {
116        if self.offset >= self.input.len() {
117            return Err(DecodeError::InsufficientData);
118        }
119        let value = self.input[self.offset];
120        self.offset += 1;
121        Ok(value)
122    }
123
124    /// Decodes a 16-bit unsigned integer in big-endian byte order.
125    /// This is a low-level method used by other decoders.
126    fn decode_u16(&mut self) -> Result<u16, DecodeError> {
127        if self.offset + 2 > self.input.len() {
128            return Err(DecodeError::InsufficientData);
129        }
130        let value =
131            u16::from_be_bytes(self.input[self.offset..self.offset + 2].try_into().unwrap());
132        self.offset += 2;
133        Ok(value)
134    }
135
136    /// Decodes a 32-bit unsigned integer in big-endian byte order.
137    /// This is a low-level method used by other decoders.
138    fn decode_u32(&mut self) -> Result<u32, DecodeError> {
139        if self.offset + 4 > self.input.len() {
140            return Err(DecodeError::InsufficientData);
141        }
142        let value =
143            u32::from_be_bytes(self.input[self.offset..self.offset + 4].try_into().unwrap());
144        self.offset += 4;
145        Ok(value)
146    }
147
148    /// Decodes a MessagePack-encoded unsigned 64-bit integer.
149    /// Handles the following MessagePack types:
150    /// - positive fixint (0x00 - 0x7f): single-byte positive integer
151    /// - uint8 (0xcc): 8-bit unsigned integer
152    /// - uint16 (0xcd): 16-bit unsigned integer (big-endian)
153    /// - uint32 (0xce): 32-bit unsigned integer (big-endian)
154    /// - uint64 (0xcf): 64-bit unsigned integer (big-endian)
155    ///
156    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family>
157    fn decode_u64(&mut self) -> Result<u64, DecodeError> {
158        match self.decode_u8()? {
159            MSGPACK_UINT8 => Ok(self.decode_u8()? as u64),
160            MSGPACK_UINT16 => Ok(self.decode_u16()? as u64),
161            MSGPACK_UINT32 => Ok(self.decode_u32()? as u64),
162            MSGPACK_UINT64 => {
163                if self.offset + 8 > self.input.len() {
164                    return Err(DecodeError::InsufficientData);
165                }
166                let value = u64::from_be_bytes(
167                    self.input[self.offset..self.offset + 8].try_into().unwrap(),
168                );
169                self.offset += 8;
170                Ok(value)
171            }
172            prefix @ MSGPACK_POSFIXINT_MIN..=MSGPACK_POSFIXINT_MAX => Ok(prefix as u64),
173            _ => Err(DecodeError::UnexpectedType),
174        }
175    }
176
177    /// Decodes a MessagePack-encoded string.
178    /// Handles the following MessagePack types:
179    /// - fixstr (0xa0 - 0xbf): string up to 31 bytes
180    /// - str8 (0xd9): string up to 255 bytes
181    /// - str16 (0xda): string up to 65535 bytes
182    /// - str32 (0xdb): string up to 4294967295 bytes
183    ///
184    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str>
185    fn decode_string(&mut self) -> Result<String, DecodeError> {
186        let prefix = self.decode_u8()?;
187
188        let len = match prefix {
189            prefix @ MSGPACK_FIXSTR_MIN..=MSGPACK_FIXSTR_MAX => (prefix & 0x1f) as usize,
190            MSGPACK_STR8 => self.decode_u8()? as usize,
191            MSGPACK_STR16 => self.decode_u16()? as usize,
192            MSGPACK_STR32 => self.decode_u32()? as usize,
193            _ => return Err(DecodeError::UnexpectedType),
194        };
195
196        if self.offset + len > self.input.len() {
197            return Err(DecodeError::InsufficientData);
198        }
199
200        let value = String::from_utf8(self.input[self.offset..self.offset + len].to_vec())
201            .map_err(|_| DecodeError::InvalidData)?;
202        self.offset += len;
203        Ok(value)
204    }
205
206    /// Decodes a MessagePack-encoded map length.
207    /// Handles the following MessagePack types:
208    /// - fixmap (0x80 - 0x8f): map with up to 15 elements
209    /// - map16 (0xde): map with up to 65535 elements
210    /// - map32 (0xdf): map with up to 4294967295 elements
211    ///
212    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-map>
213    fn decode_map_len(&mut self) -> Result<usize, DecodeError> {
214        let prefix = self.decode_u8()?;
215
216        match prefix {
217            prefix @ MSGPACK_FIXMAP_MIN..=MSGPACK_FIXMAP_MAX => Ok((prefix & 0x0f) as usize),
218            MSGPACK_MAP16 => Ok(self.decode_u16()? as usize),
219            MSGPACK_MAP32 => Ok(self.decode_u32()? as usize),
220            _ => Err(DecodeError::UnexpectedType),
221        }
222    }
223
224    /// Decodes a MessagePack-encoded array length.
225    /// Handles the following MessagePack types:
226    /// - fixarray (0x90 - 0x9f): array with up to 15 elements
227    /// - array16 (0xdc): array with up to 65535 elements
228    /// - array32 (0xdd): array with up to 4294967295 elements
229    ///
230    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-array>
231    #[allow(dead_code)]
232    fn decode_array_len(&mut self) -> Result<usize, DecodeError> {
233        let prefix = self.decode_u8()?;
234
235        match prefix {
236            prefix @ MSGPACK_FIXARRAY_MIN..=MSGPACK_FIXARRAY_MAX => Ok((prefix & 0x0f) as usize),
237            MSGPACK_ARRAY16 => Ok(self.decode_u16()? as usize),
238            MSGPACK_ARRAY32 => Ok(self.decode_u32()? as usize),
239            _ => Err(DecodeError::UnexpectedType),
240        }
241    }
242
243    /// Decodes a MessagePack-encoded boolean value.
244    /// Handles the following MessagePack types:
245    /// - true (0xc3): boolean true
246    /// - false (0xc2): boolean false
247    ///
248    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-bool>
249    fn decode_bool(&mut self) -> Result<bool, DecodeError> {
250        match self.decode_u8()? {
251            MSGPACK_TRUE => Ok(true),
252            MSGPACK_FALSE => Ok(false),
253            _ => Err(DecodeError::UnexpectedType),
254        }
255    }
256
257    /// Decodes a MessagePack-encoded nil value.
258    /// Handles the following MessagePack types:
259    /// - nil (0xc0): nil/null value
260    ///
261    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-nil>
262    #[allow(dead_code)]
263    fn decode_nil(&mut self) -> Result<(), DecodeError> {
264        match self.decode_u8()? {
265            MSGPACK_NIL => Ok(()),
266            _ => Err(DecodeError::UnexpectedType),
267        }
268    }
269
270    /// Peeks at the next byte to check if it's a nil value without advancing the offset.
271    /// Returns true if the next value is nil, false otherwise.
272    #[allow(dead_code)]
273    fn peek_nil(&mut self) -> Result<bool, DecodeError> {
274        if self.offset >= self.input.len() {
275            return Err(DecodeError::InsufficientData);
276        }
277        Ok(self.input[self.offset] == MSGPACK_NIL)
278    }
279
280    /// Peeks at the next byte to check if it's a string value without advancing the offset.
281    /// Returns true if the next value is a string, false otherwise.
282    fn peek_string(&mut self) -> Result<bool, DecodeError> {
283        if self.offset >= self.input.len() {
284            return Err(DecodeError::InsufficientData);
285        }
286        let prefix = self.input[self.offset];
287        Ok((MSGPACK_FIXSTR_MIN..=MSGPACK_FIXSTR_MAX).contains(&prefix)
288            || prefix == MSGPACK_STR8
289            || prefix == MSGPACK_STR16
290            || prefix == MSGPACK_STR32)
291    }
292
293    /// Skips a MessagePack value of any type.
294    /// This is used when encountering unknown field names in a struct.
295    fn skip_value(&mut self) -> Result<(), DecodeError> {
296        let prefix = self.decode_u8()?;
297
298        match prefix {
299            // String formats
300            prefix @ MSGPACK_FIXSTR_MIN..=MSGPACK_FIXSTR_MAX => {
301                let len = (prefix & 0x1f) as usize;
302                if self.offset + len > self.input.len() {
303                    return Err(DecodeError::InsufficientData);
304                }
305                self.offset += len;
306                Ok(())
307            }
308            MSGPACK_STR8 => {
309                let len = self.decode_u8()? as usize;
310                if self.offset + len > self.input.len() {
311                    return Err(DecodeError::InsufficientData);
312                }
313                self.offset += len;
314                Ok(())
315            }
316            MSGPACK_STR16 => {
317                let len = self.decode_u16()? as usize;
318                if self.offset + len > self.input.len() {
319                    return Err(DecodeError::InsufficientData);
320                }
321                self.offset += len;
322                Ok(())
323            }
324            MSGPACK_STR32 => {
325                let len = self.decode_u32()? as usize;
326                if self.offset + len > self.input.len() {
327                    return Err(DecodeError::InsufficientData);
328                }
329                self.offset += len;
330                Ok(())
331            }
332
333            // Integer formats
334            MSGPACK_UINT8 => {
335                self.offset += 1;
336                Ok(())
337            }
338            MSGPACK_UINT16 => {
339                self.offset += 2;
340                Ok(())
341            }
342            MSGPACK_UINT32 => {
343                self.offset += 4;
344                Ok(())
345            }
346            MSGPACK_UINT64 => {
347                self.offset += 8;
348                Ok(())
349            }
350            MSGPACK_INT8 => {
351                self.offset += 1;
352                Ok(())
353            }
354            MSGPACK_INT16 => {
355                self.offset += 2;
356                Ok(())
357            }
358            MSGPACK_INT32 => {
359                self.offset += 4;
360                Ok(())
361            }
362            MSGPACK_INT64 => {
363                self.offset += 8;
364                Ok(())
365            }
366            // Fixed integers are already handled by decode_u8
367
368            // Boolean and nil
369            MSGPACK_NIL | MSGPACK_TRUE | MSGPACK_FALSE => Ok(()),
370
371            // Map format
372            prefix @ MSGPACK_FIXMAP_MIN..=MSGPACK_FIXMAP_MAX => {
373                let len = (prefix & 0x0f) as usize;
374                for _ in 0..len {
375                    self.skip_value()?; // Skip key
376                    self.skip_value()?; // Skip value
377                }
378                Ok(())
379            }
380            MSGPACK_MAP16 => {
381                let len = self.decode_u16()? as usize;
382                for _ in 0..len {
383                    self.skip_value()?; // Skip key
384                    self.skip_value()?; // Skip value
385                }
386                Ok(())
387            }
388            MSGPACK_MAP32 => {
389                let len = self.decode_u32()? as usize;
390                for _ in 0..len {
391                    self.skip_value()?; // Skip key
392                    self.skip_value()?; // Skip value
393                }
394                Ok(())
395            }
396
397            // Array format
398            prefix @ MSGPACK_FIXARRAY_MIN..=MSGPACK_FIXARRAY_MAX => {
399                let len = (prefix & 0x0f) as usize;
400                for _ in 0..len {
401                    self.skip_value()?;
402                }
403                Ok(())
404            }
405            MSGPACK_ARRAY16 => {
406                let len = self.decode_u16()? as usize;
407                for _ in 0..len {
408                    self.skip_value()?;
409                }
410                Ok(())
411            }
412            MSGPACK_ARRAY32 => {
413                let len = self.decode_u32()? as usize;
414                for _ in 0..len {
415                    self.skip_value()?;
416                }
417                Ok(())
418            }
419
420            _ => Err(DecodeError::UnexpectedType),
421        }
422    }
423
424    fn deserialize_value<'facet>(
425        &mut self,
426        partial: Partial<'facet>,
427    ) -> Result<Partial<'facet>, DecodeError> {
428        let mut partial = partial;
429        let shape = partial.shape();
430        trace!("Deserializing {shape:?}");
431
432        // First check the type system (Type)
433        match &shape.ty {
434            Type::User(UserType::Struct(struct_type))
435                if struct_type.kind != facet_core::StructKind::Tuple =>
436            {
437                trace!("Deserializing struct");
438                let map_len = self.decode_map_len()?;
439
440                // Track which fields we've seen so we can handle defaults for the rest
441                let mut seen_fields = vec![false; struct_type.fields.len()];
442
443                for _ in 0..map_len {
444                    let key = self.decode_string()?;
445                    match partial.field_index(&key) {
446                        Some(index) => {
447                            seen_fields[index] = true;
448                            let field_partial = partial.begin_nth_field(index).unwrap();
449                            let field_partial = self.deserialize_value(field_partial)?;
450                            partial = field_partial.end().unwrap();
451                        }
452                        None => {
453                            // Skip unknown field value
454                            self.skip_value()?;
455                            trace!("Skipping unknown field: {key}");
456                        }
457                    }
458                }
459
460                // Handle defaults for fields that weren't seen in the input
461                for (i, &seen) in seen_fields.iter().enumerate() {
462                    if !seen {
463                        let field = &struct_type.fields[i];
464                        if field.has_default() {
465                            partial = partial.set_nth_field_to_default(i)?;
466                        } else {
467                            // Non-default field was missing
468                            return Err(DecodeError::MissingField(field.name.to_string()));
469                        }
470                    }
471                }
472
473                return Ok(partial);
474            }
475            Type::User(facet_core::UserType::Struct(struct_type))
476                if struct_type.kind == facet_core::StructKind::Tuple =>
477            {
478                trace!("Deserializing tuple");
479                let array_len = self.decode_array_len()?;
480                let field_count = struct_type.fields.len();
481
482                if array_len != field_count {
483                    return Err(DecodeError::InvalidData);
484                }
485
486                // For tuples, deserialize fields in order
487                for idx in 0..field_count {
488                    trace!("Deserializing tuple field {idx}");
489                    let field_partial = partial.begin_nth_field(idx)?;
490                    let field_partial = self.deserialize_value(field_partial)?;
491                    partial = field_partial.end().map_err(DecodeError::ReflectError)?;
492                }
493
494                return Ok(partial);
495            }
496            Type::User(UserType::Enum(enum_type)) => {
497                trace!("Deserializing enum");
498
499                // Check if it's a unit variant which is represented as a string
500                if self.peek_string()? {
501                    let variant_name = self.decode_string()?;
502                    for (idx, variant) in enum_type.variants.iter().enumerate() {
503                        if variant.name == variant_name {
504                            partial = partial.select_nth_variant(idx)?;
505                            return Ok(partial);
506                        }
507                    }
508                    return Err(DecodeError::InvalidEnum(format!(
509                        "Unknown variant: {variant_name}"
510                    )));
511                }
512
513                // Otherwise it's represented as a map with single entry where key is the variant name
514                let map_len = self.decode_map_len()?;
515                if map_len != 1 {
516                    return Err(DecodeError::InvalidData);
517                }
518
519                let variant_name = self.decode_string()?;
520
521                for (idx, variant) in enum_type.variants.iter().enumerate() {
522                    if variant.name == variant_name {
523                        match &variant.data.kind {
524                            // Handle unit variant
525                            facet_core::StructKind::Unit => {
526                                // Need to skip any value that might be present
527                                self.skip_value()?;
528                                partial = partial.select_nth_variant(idx)?;
529                                return Ok(partial);
530                            }
531
532                            // Handle tuple variant
533                            facet_core::StructKind::Tuple => {
534                                let array_len = self.decode_array_len()?;
535                                let field_count = variant.data.fields.len();
536
537                                if array_len != field_count {
538                                    return Err(DecodeError::InvalidData);
539                                }
540
541                                partial = partial.select_nth_variant(idx)?;
542                                for field_idx in 0..field_count {
543                                    let field_partial = partial.begin_nth_field(field_idx)?;
544                                    let field_partial = self.deserialize_value(field_partial)?;
545                                    partial = field_partial.end()?;
546                                }
547                                return Ok(partial);
548                            }
549
550                            // Handle struct variant
551                            facet_core::StructKind::Struct => {
552                                let map_len = self.decode_map_len()?;
553                                partial = partial.select_nth_variant(idx)?;
554
555                                // Handle fields as a normal struct
556                                for _ in 0..map_len {
557                                    let field_name = self.decode_string()?;
558                                    match partial.field_index(&field_name) {
559                                        Some(field_idx) => {
560                                            let field_partial =
561                                                partial.begin_nth_field(field_idx)?;
562                                            let field_partial =
563                                                self.deserialize_value(field_partial)?;
564                                            partial = field_partial.end()?;
565                                        }
566                                        None => {
567                                            // Skip unknown field
568                                            self.skip_value()?;
569                                            trace!("Skipping unknown field in enum: {field_name}");
570                                        }
571                                    }
572                                }
573
574                                return Ok(partial);
575                            }
576
577                            // Handle other kinds that might be added in the future
578                            _ => {
579                                return Err(DecodeError::UnsupportedType(format!(
580                                    "Unsupported enum variant kind: {:?}",
581                                    variant.data.kind
582                                )));
583                            }
584                        }
585                    }
586                }
587
588                return Err(DecodeError::InvalidEnum(format!(
589                    "Unknown variant: {variant_name}"
590                )));
591            }
592            _ => {}
593        }
594
595        // Then check the def system (Def)
596        if let Def::Scalar = shape.def {
597            trace!("Deserializing scalar");
598            if shape.is_type::<String>() {
599                let s = self.decode_string()?;
600                partial = partial.set(s)?;
601            } else if shape.is_type::<u64>() {
602                let n = self.decode_u64()?;
603                partial = partial.set(n)?;
604            } else if shape.is_type::<u32>() {
605                let n = self.decode_u64()?;
606                if n > u32::MAX as u64 {
607                    return Err(DecodeError::IntegerOverflow);
608                }
609                partial = partial.set(n as u32)?;
610            } else if shape.is_type::<u16>() {
611                let n = self.decode_u64()?;
612                if n > u16::MAX as u64 {
613                    return Err(DecodeError::IntegerOverflow);
614                }
615                partial = partial.set(n as u16)?;
616            } else if shape.is_type::<u8>() {
617                let n = self.decode_u64()?;
618                if n > u8::MAX as u64 {
619                    return Err(DecodeError::IntegerOverflow);
620                }
621                partial = partial.set(n as u8)?;
622            } else if shape.is_type::<i64>() {
623                // TODO: implement proper signed int decoding including negative values
624                let n = self.decode_u64()?;
625                if n > i64::MAX as u64 {
626                    return Err(DecodeError::IntegerOverflow);
627                }
628                partial = partial.set(n as i64)?;
629            } else if shape.is_type::<i32>() {
630                let n = self.decode_u64()?;
631                if n > i32::MAX as u64 {
632                    return Err(DecodeError::IntegerOverflow);
633                }
634                partial = partial.set(n as i32)?;
635            } else if shape.is_type::<i16>() {
636                let n = self.decode_u64()?;
637                if n > i16::MAX as u64 {
638                    return Err(DecodeError::IntegerOverflow);
639                }
640                partial = partial.set(n as i16)?;
641            } else if shape.is_type::<i8>() {
642                let n = self.decode_u64()?;
643                if n > i8::MAX as u64 {
644                    return Err(DecodeError::IntegerOverflow);
645                }
646                partial = partial.set(n as i8)?;
647            } else if shape.is_type::<f64>() {
648                // TODO: Implement proper f64 decoding from MessagePack format
649                return Err(DecodeError::NotImplemented(
650                    "f64 deserialization not yet implemented".to_string(),
651                ));
652            } else if shape.is_type::<f32>() {
653                // TODO: Implement proper f32 decoding from MessagePack format
654                return Err(DecodeError::NotImplemented(
655                    "f32 deserialization not yet implemented".to_string(),
656                ));
657            } else if shape.is_type::<bool>() {
658                let b = self.decode_bool()?;
659                partial = partial.set(b)?;
660            } else {
661                return Err(DecodeError::UnsupportedType(format!("{shape}")));
662            }
663        } else if let Def::Map(_map_def) = shape.def {
664            trace!("Deserializing map");
665            let map_len = self.decode_map_len()?;
666            partial = partial.begin_map()?;
667
668            for _ in 0..map_len {
669                // Each map entry has a key and value
670                let key_partial = partial.begin_key()?;
671                let key_partial = self.deserialize_value(key_partial)?;
672                partial = key_partial.end()?;
673
674                let value_partial = partial.begin_value()?;
675                let value_partial = self.deserialize_value(value_partial)?;
676                partial = value_partial.end()?;
677            }
678        } else if let Def::List(_list_def) = shape.def {
679            trace!("Deserializing list");
680            let array_len = self.decode_array_len()?;
681            partial = partial.begin_list()?;
682
683            for _ in 0..array_len {
684                let item_partial = partial.begin_list_item()?;
685                let item_partial = self.deserialize_value(item_partial)?;
686                partial = item_partial.end()?;
687            }
688        } else if let Def::Option(_option_def) = shape.def {
689            trace!("Deserializing option with shape: {shape}");
690            if self.peek_nil()? {
691                trace!("Option value is nil, setting to None");
692                // Consume the nil value
693                self.decode_nil()?;
694                // Initialize None option
695                partial = partial.set_default()?;
696            } else {
697                trace!("Option value is present, setting to Some");
698                // Value is present - initialize a Some option
699                let some_partial = partial.begin_some()?;
700                trace!("After begin_some, partial shape: {}", some_partial.shape());
701                let some_partial = self.deserialize_value(some_partial)?;
702                trace!("After deserialize_value, calling end");
703                partial = some_partial.end()?;
704                trace!("After end, partial shape: {}", partial.shape());
705            }
706        } else {
707            return Err(DecodeError::UnsupportedShape(format!("{shape:?}")));
708        }
709
710        Ok(partial)
711    }
712}