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 IEEE 754 single precision floating point number
178    /// This is a low-level method used by other decoders.
179    fn decode_f32(&mut self) -> Result<f32, DecodeError> {
180        if self.offset + 4 > self.input.len() {
181            return Err(DecodeError::InsufficientData);
182        }
183        let value =
184            f32::from_be_bytes(self.input[self.offset..self.offset + 4].try_into().unwrap());
185        self.offset += 4;
186        Ok(value)
187    }
188
189    /// Decodes a MessagePack-encoded IEEE 754 double precision floating point number
190    /// This is a low-level method used by other decoders.
191    fn decode_f64(&mut self) -> Result<f64, DecodeError> {
192        if self.offset + 8 > self.input.len() {
193            return Err(DecodeError::InsufficientData);
194        }
195        let value =
196            f64::from_be_bytes(self.input[self.offset..self.offset + 8].try_into().unwrap());
197        self.offset += 8;
198        Ok(value)
199    }
200
201    /// Decodes a MessagePack-encoded float.
202    /// Handles the following MessagePack types:
203    /// - f32 (0xca): 32-bit IEEE 754 single precision floating point number
204    /// - f64 (0xcb): 64-bit IEEE 754 double precision floating point number
205    ///
206    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#float-format-family>
207    fn decode_float(&mut self) -> Result<f64, DecodeError> {
208        match self.decode_u8()? {
209            MSGPACK_FLOAT32 => self.decode_f32().map(f64::from),
210            MSGPACK_FLOAT64 => self.decode_f64(),
211            _ => Err(DecodeError::UnexpectedType),
212        }
213    }
214
215    /// Decodes a MessagePack-encoded string.
216    /// Handles the following MessagePack types:
217    /// - fixstr (0xa0 - 0xbf): string up to 31 bytes
218    /// - str8 (0xd9): string up to 255 bytes
219    /// - str16 (0xda): string up to 65535 bytes
220    /// - str32 (0xdb): string up to 4294967295 bytes
221    ///
222    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str>
223    fn decode_string(&mut self) -> Result<String, DecodeError> {
224        let prefix = self.decode_u8()?;
225
226        let len = match prefix {
227            prefix @ MSGPACK_FIXSTR_MIN..=MSGPACK_FIXSTR_MAX => (prefix & 0x1f) as usize,
228            MSGPACK_STR8 => self.decode_u8()? as usize,
229            MSGPACK_STR16 => self.decode_u16()? as usize,
230            MSGPACK_STR32 => self.decode_u32()? as usize,
231            _ => return Err(DecodeError::UnexpectedType),
232        };
233
234        if self.offset + len > self.input.len() {
235            return Err(DecodeError::InsufficientData);
236        }
237
238        let value = String::from_utf8(self.input[self.offset..self.offset + len].to_vec())
239            .map_err(|_| DecodeError::InvalidData)?;
240        self.offset += len;
241        Ok(value)
242    }
243
244    /// Decodes a MessagePack-encoded map length.
245    /// Handles the following MessagePack types:
246    /// - fixmap (0x80 - 0x8f): map with up to 15 elements
247    /// - map16 (0xde): map with up to 65535 elements
248    /// - map32 (0xdf): map with up to 4294967295 elements
249    ///
250    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-map>
251    fn decode_map_len(&mut self) -> Result<usize, DecodeError> {
252        let prefix = self.decode_u8()?;
253
254        match prefix {
255            prefix @ MSGPACK_FIXMAP_MIN..=MSGPACK_FIXMAP_MAX => Ok((prefix & 0x0f) as usize),
256            MSGPACK_MAP16 => Ok(self.decode_u16()? as usize),
257            MSGPACK_MAP32 => Ok(self.decode_u32()? as usize),
258            _ => Err(DecodeError::UnexpectedType),
259        }
260    }
261
262    /// Decodes a MessagePack-encoded array length.
263    /// Handles the following MessagePack types:
264    /// - fixarray (0x90 - 0x9f): array with up to 15 elements
265    /// - array16 (0xdc): array with up to 65535 elements
266    /// - array32 (0xdd): array with up to 4294967295 elements
267    ///
268    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-array>
269    #[allow(dead_code)]
270    fn decode_array_len(&mut self) -> Result<usize, DecodeError> {
271        let prefix = self.decode_u8()?;
272
273        match prefix {
274            prefix @ MSGPACK_FIXARRAY_MIN..=MSGPACK_FIXARRAY_MAX => Ok((prefix & 0x0f) as usize),
275            MSGPACK_ARRAY16 => Ok(self.decode_u16()? as usize),
276            MSGPACK_ARRAY32 => Ok(self.decode_u32()? as usize),
277            _ => Err(DecodeError::UnexpectedType),
278        }
279    }
280
281    /// Decodes a MessagePack-encoded boolean value.
282    /// Handles the following MessagePack types:
283    /// - true (0xc3): boolean true
284    /// - false (0xc2): boolean false
285    ///
286    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-bool>
287    fn decode_bool(&mut self) -> Result<bool, DecodeError> {
288        match self.decode_u8()? {
289            MSGPACK_TRUE => Ok(true),
290            MSGPACK_FALSE => Ok(false),
291            _ => Err(DecodeError::UnexpectedType),
292        }
293    }
294
295    /// Decodes a MessagePack-encoded nil value.
296    /// Handles the following MessagePack types:
297    /// - nil (0xc0): nil/null value
298    ///
299    /// Ref: <https://github.com/msgpack/msgpack/blob/master/spec.md#formats-nil>
300    #[allow(dead_code)]
301    fn decode_nil(&mut self) -> Result<(), DecodeError> {
302        match self.decode_u8()? {
303            MSGPACK_NIL => Ok(()),
304            _ => Err(DecodeError::UnexpectedType),
305        }
306    }
307
308    /// Peeks at the next byte to check if it's a nil value without advancing the offset.
309    /// Returns true if the next value is nil, false otherwise.
310    #[allow(dead_code)]
311    fn peek_nil(&mut self) -> Result<bool, DecodeError> {
312        if self.offset >= self.input.len() {
313            return Err(DecodeError::InsufficientData);
314        }
315        Ok(self.input[self.offset] == MSGPACK_NIL)
316    }
317
318    /// Peeks at the next byte to check if it's a string value without advancing the offset.
319    /// Returns true if the next value is a string, false otherwise.
320    fn peek_string(&mut self) -> Result<bool, DecodeError> {
321        if self.offset >= self.input.len() {
322            return Err(DecodeError::InsufficientData);
323        }
324        let prefix = self.input[self.offset];
325        Ok((MSGPACK_FIXSTR_MIN..=MSGPACK_FIXSTR_MAX).contains(&prefix)
326            || prefix == MSGPACK_STR8
327            || prefix == MSGPACK_STR16
328            || prefix == MSGPACK_STR32)
329    }
330
331    /// Skips a MessagePack value of any type.
332    /// This is used when encountering unknown field names in a struct.
333    fn skip_value(&mut self) -> Result<(), DecodeError> {
334        let prefix = self.decode_u8()?;
335
336        match prefix {
337            // String formats
338            prefix @ MSGPACK_FIXSTR_MIN..=MSGPACK_FIXSTR_MAX => {
339                let len = (prefix & 0x1f) as usize;
340                if self.offset + len > self.input.len() {
341                    return Err(DecodeError::InsufficientData);
342                }
343                self.offset += len;
344                Ok(())
345            }
346            MSGPACK_STR8 => {
347                let len = self.decode_u8()? as usize;
348                if self.offset + len > self.input.len() {
349                    return Err(DecodeError::InsufficientData);
350                }
351                self.offset += len;
352                Ok(())
353            }
354            MSGPACK_STR16 => {
355                let len = self.decode_u16()? as usize;
356                if self.offset + len > self.input.len() {
357                    return Err(DecodeError::InsufficientData);
358                }
359                self.offset += len;
360                Ok(())
361            }
362            MSGPACK_STR32 => {
363                let len = self.decode_u32()? as usize;
364                if self.offset + len > self.input.len() {
365                    return Err(DecodeError::InsufficientData);
366                }
367                self.offset += len;
368                Ok(())
369            }
370
371            // Integer formats
372            MSGPACK_UINT8 => {
373                self.offset += 1;
374                Ok(())
375            }
376            MSGPACK_UINT16 => {
377                self.offset += 2;
378                Ok(())
379            }
380            MSGPACK_UINT32 => {
381                self.offset += 4;
382                Ok(())
383            }
384            MSGPACK_UINT64 => {
385                self.offset += 8;
386                Ok(())
387            }
388            MSGPACK_INT8 => {
389                self.offset += 1;
390                Ok(())
391            }
392            MSGPACK_INT16 => {
393                self.offset += 2;
394                Ok(())
395            }
396            MSGPACK_INT32 => {
397                self.offset += 4;
398                Ok(())
399            }
400            MSGPACK_INT64 => {
401                self.offset += 8;
402                Ok(())
403            }
404            // Fixed integers are already handled by decode_u8
405
406            // Float formats
407            MSGPACK_FLOAT32 => {
408                self.offset += 4;
409                Ok(())
410            }
411            MSGPACK_FLOAT64 => {
412                self.offset += 8;
413                Ok(())
414            }
415
416            // Boolean and nil
417            MSGPACK_NIL | MSGPACK_TRUE | MSGPACK_FALSE => Ok(()),
418
419            // Map format
420            prefix @ MSGPACK_FIXMAP_MIN..=MSGPACK_FIXMAP_MAX => {
421                let len = (prefix & 0x0f) as usize;
422                for _ in 0..len {
423                    self.skip_value()?; // Skip key
424                    self.skip_value()?; // Skip value
425                }
426                Ok(())
427            }
428            MSGPACK_MAP16 => {
429                let len = self.decode_u16()? as usize;
430                for _ in 0..len {
431                    self.skip_value()?; // Skip key
432                    self.skip_value()?; // Skip value
433                }
434                Ok(())
435            }
436            MSGPACK_MAP32 => {
437                let len = self.decode_u32()? as usize;
438                for _ in 0..len {
439                    self.skip_value()?; // Skip key
440                    self.skip_value()?; // Skip value
441                }
442                Ok(())
443            }
444
445            // Array format
446            prefix @ MSGPACK_FIXARRAY_MIN..=MSGPACK_FIXARRAY_MAX => {
447                let len = (prefix & 0x0f) as usize;
448                for _ in 0..len {
449                    self.skip_value()?;
450                }
451                Ok(())
452            }
453            MSGPACK_ARRAY16 => {
454                let len = self.decode_u16()? as usize;
455                for _ in 0..len {
456                    self.skip_value()?;
457                }
458                Ok(())
459            }
460            MSGPACK_ARRAY32 => {
461                let len = self.decode_u32()? as usize;
462                for _ in 0..len {
463                    self.skip_value()?;
464                }
465                Ok(())
466            }
467
468            _ => Err(DecodeError::UnexpectedType),
469        }
470    }
471
472    fn deserialize_value<'facet>(
473        &mut self,
474        partial: Partial<'facet>,
475    ) -> Result<Partial<'facet>, DecodeError> {
476        let mut partial = partial;
477        let shape = partial.shape();
478        trace!("Deserializing {shape:?}");
479
480        // First check the type system (Type)
481        match &shape.ty {
482            Type::User(UserType::Struct(struct_type))
483                if struct_type.kind != facet_core::StructKind::Tuple =>
484            {
485                trace!("Deserializing struct");
486                let map_len = self.decode_map_len()?;
487
488                // Track which fields we've seen so we can handle defaults for the rest
489                let mut seen_fields = vec![false; struct_type.fields.len()];
490
491                for _ in 0..map_len {
492                    let key = self.decode_string()?;
493                    match partial.field_index(&key) {
494                        Some(index) => {
495                            seen_fields[index] = true;
496                            let field_partial = partial.begin_nth_field(index).unwrap();
497                            let field_partial = self.deserialize_value(field_partial)?;
498                            partial = field_partial.end().unwrap();
499                        }
500                        None => {
501                            // Skip unknown field value
502                            self.skip_value()?;
503                            trace!("Skipping unknown field: {key}");
504                        }
505                    }
506                }
507
508                // Handle defaults for fields that weren't seen in the input
509                for (i, &seen) in seen_fields.iter().enumerate() {
510                    if !seen {
511                        let field = &struct_type.fields[i];
512                        if field.has_default() {
513                            partial = partial.set_nth_field_to_default(i)?;
514                        } else {
515                            // Non-default field was missing
516                            return Err(DecodeError::MissingField(field.name.to_string()));
517                        }
518                    }
519                }
520
521                return Ok(partial);
522            }
523            Type::User(facet_core::UserType::Struct(struct_type))
524                if struct_type.kind == facet_core::StructKind::Tuple =>
525            {
526                trace!("Deserializing tuple");
527                let array_len = self.decode_array_len()?;
528                let field_count = struct_type.fields.len();
529
530                if array_len != field_count {
531                    return Err(DecodeError::InvalidData);
532                }
533
534                // For tuples, deserialize fields in order
535                for idx in 0..field_count {
536                    trace!("Deserializing tuple field {idx}");
537                    let field_partial = partial.begin_nth_field(idx)?;
538                    let field_partial = self.deserialize_value(field_partial)?;
539                    partial = field_partial.end().map_err(DecodeError::ReflectError)?;
540                }
541
542                return Ok(partial);
543            }
544            Type::User(UserType::Enum(enum_type)) => {
545                trace!("Deserializing enum");
546
547                // Check if it's a unit variant which is represented as a string
548                if self.peek_string()? {
549                    let variant_name = self.decode_string()?;
550                    for (idx, variant) in enum_type.variants.iter().enumerate() {
551                        if variant.name == variant_name {
552                            partial = partial.select_nth_variant(idx)?;
553                            return Ok(partial);
554                        }
555                    }
556                    return Err(DecodeError::InvalidEnum(format!(
557                        "Unknown variant: {variant_name}"
558                    )));
559                }
560
561                // Otherwise it's represented as a map with single entry where key is the variant name
562                let map_len = self.decode_map_len()?;
563                if map_len != 1 {
564                    return Err(DecodeError::InvalidData);
565                }
566
567                let variant_name = self.decode_string()?;
568
569                for (idx, variant) in enum_type.variants.iter().enumerate() {
570                    if variant.name == variant_name {
571                        match &variant.data.kind {
572                            // Handle unit variant
573                            facet_core::StructKind::Unit => {
574                                // Need to skip any value that might be present
575                                self.skip_value()?;
576                                partial = partial.select_nth_variant(idx)?;
577                                return Ok(partial);
578                            }
579
580                            // Handle tuple variant
581                            facet_core::StructKind::Tuple => {
582                                let array_len = self.decode_array_len()?;
583                                let field_count = variant.data.fields.len();
584
585                                if array_len != field_count {
586                                    return Err(DecodeError::InvalidData);
587                                }
588
589                                partial = partial.select_nth_variant(idx)?;
590                                for field_idx in 0..field_count {
591                                    let field_partial = partial.begin_nth_field(field_idx)?;
592                                    let field_partial = self.deserialize_value(field_partial)?;
593                                    partial = field_partial.end()?;
594                                }
595                                return Ok(partial);
596                            }
597
598                            // Handle struct variant
599                            facet_core::StructKind::Struct => {
600                                let map_len = self.decode_map_len()?;
601                                partial = partial.select_nth_variant(idx)?;
602
603                                // Handle fields as a normal struct
604                                for _ in 0..map_len {
605                                    let field_name = self.decode_string()?;
606                                    match partial.field_index(&field_name) {
607                                        Some(field_idx) => {
608                                            let field_partial =
609                                                partial.begin_nth_field(field_idx)?;
610                                            let field_partial =
611                                                self.deserialize_value(field_partial)?;
612                                            partial = field_partial.end()?;
613                                        }
614                                        None => {
615                                            // Skip unknown field
616                                            self.skip_value()?;
617                                            trace!("Skipping unknown field in enum: {field_name}");
618                                        }
619                                    }
620                                }
621
622                                return Ok(partial);
623                            }
624
625                            // Handle other kinds that might be added in the future
626                            _ => {
627                                return Err(DecodeError::UnsupportedType(format!(
628                                    "Unsupported enum variant kind: {:?}",
629                                    variant.data.kind
630                                )));
631                            }
632                        }
633                    }
634                }
635
636                return Err(DecodeError::InvalidEnum(format!(
637                    "Unknown variant: {variant_name}"
638                )));
639            }
640            _ => {}
641        }
642
643        // Then check the def system (Def)
644        if let Def::Scalar = shape.def {
645            trace!("Deserializing scalar");
646            if shape.is_type::<String>() {
647                let s = self.decode_string()?;
648                partial = partial.set(s)?;
649            } else if shape.is_type::<u64>() {
650                let n = self.decode_u64()?;
651                partial = partial.set(n)?;
652            } else if shape.is_type::<u32>() {
653                let n = self.decode_u64()?;
654                if n > u32::MAX as u64 {
655                    return Err(DecodeError::IntegerOverflow);
656                }
657                partial = partial.set(n as u32)?;
658            } else if shape.is_type::<u16>() {
659                let n = self.decode_u64()?;
660                if n > u16::MAX as u64 {
661                    return Err(DecodeError::IntegerOverflow);
662                }
663                partial = partial.set(n as u16)?;
664            } else if shape.is_type::<u8>() {
665                let n = self.decode_u64()?;
666                if n > u8::MAX as u64 {
667                    return Err(DecodeError::IntegerOverflow);
668                }
669                partial = partial.set(n as u8)?;
670            } else if shape.is_type::<i64>() {
671                // TODO: implement proper signed int decoding including negative values
672                let n = self.decode_u64()?;
673                if n > i64::MAX as u64 {
674                    return Err(DecodeError::IntegerOverflow);
675                }
676                partial = partial.set(n as i64)?;
677            } else if shape.is_type::<i32>() {
678                let n = self.decode_u64()?;
679                if n > i32::MAX as u64 {
680                    return Err(DecodeError::IntegerOverflow);
681                }
682                partial = partial.set(n as i32)?;
683            } else if shape.is_type::<i16>() {
684                let n = self.decode_u64()?;
685                if n > i16::MAX as u64 {
686                    return Err(DecodeError::IntegerOverflow);
687                }
688                partial = partial.set(n as i16)?;
689            } else if shape.is_type::<i8>() {
690                let n = self.decode_u64()?;
691                if n > i8::MAX as u64 {
692                    return Err(DecodeError::IntegerOverflow);
693                }
694                partial = partial.set(n as i8)?;
695            } else if shape.is_type::<f64>() {
696                let n = self.decode_float()?;
697                partial = partial.set(n)?;
698            } else if shape.is_type::<f32>() {
699                let n = self.decode_float()?;
700                let n_as_f32 = std::hint::black_box(n as f32);
701                if n_as_f32 as f64 != n {
702                    return Err(DecodeError::FloatOverflow);
703                }
704                partial = partial.set(n_as_f32)?;
705            } else if shape.is_type::<bool>() {
706                let b = self.decode_bool()?;
707                partial = partial.set(b)?;
708            } else {
709                return Err(DecodeError::UnsupportedType(format!("{shape}")));
710            }
711        } else if let Def::Map(_map_def) = shape.def {
712            trace!("Deserializing map");
713            let map_len = self.decode_map_len()?;
714            partial = partial.begin_map()?;
715
716            for _ in 0..map_len {
717                // Each map entry has a key and value
718                let key_partial = partial.begin_key()?;
719                let key_partial = self.deserialize_value(key_partial)?;
720                partial = key_partial.end()?;
721
722                let value_partial = partial.begin_value()?;
723                let value_partial = self.deserialize_value(value_partial)?;
724                partial = value_partial.end()?;
725            }
726        } else if let Def::List(_list_def) = shape.def {
727            trace!("Deserializing list");
728            let array_len = self.decode_array_len()?;
729            partial = partial.begin_list()?;
730
731            for _ in 0..array_len {
732                let item_partial = partial.begin_list_item()?;
733                let item_partial = self.deserialize_value(item_partial)?;
734                partial = item_partial.end()?;
735            }
736        } else if let Def::Option(_option_def) = shape.def {
737            trace!("Deserializing option with shape: {shape}");
738            if self.peek_nil()? {
739                trace!("Option value is nil, setting to None");
740                // Consume the nil value
741                self.decode_nil()?;
742                // Initialize None option
743                partial = partial.set_default()?;
744            } else {
745                trace!("Option value is present, setting to Some");
746                // Value is present - initialize a Some option
747                let some_partial = partial.begin_some()?;
748                trace!("After begin_some, partial shape: {}", some_partial.shape());
749                let some_partial = self.deserialize_value(some_partial)?;
750                trace!("After deserialize_value, calling end");
751                partial = some_partial.end()?;
752                trace!("After end, partial shape: {}", partial.shape());
753            }
754        } else {
755            return Err(DecodeError::UnsupportedShape(format!("{shape:?}")));
756        }
757
758        Ok(partial)
759    }
760}