serde_hkx/bytes/de/
seq.rs

1//! Deserializing each element in an `Array`
2use super::BytesDeserializer;
3use crate::errors::de::{Error, Result};
4use havok_serde::de::{DeserializeSeed, SeqAccess};
5
6/// A structure for deserializing each element in an `Array`.
7///
8/// Since XML Array has strings to be parsed before and after parsing the value, the methods of this structure process
9/// them and call `deserialize` to parse and return the internal value.
10pub struct SeqDeserializer<'a, 'de: 'a> {
11    /// Deserializer
12    de: &'a mut BytesDeserializer<'de>,
13
14    /// Array length
15    size: usize,
16
17    /// Array length
18    index: usize,
19}
20
21impl<'a, 'de> SeqDeserializer<'a, 'de> {
22    /// Create a new seq deserializer
23    pub const fn new(de: &'a mut BytesDeserializer<'de>, size: usize) -> Self {
24        Self { de, size, index: 0 }
25    }
26}
27
28// `SeqAccess` is provided to the `Visitor` to give it the ability to iterate
29// through elements of the sequence.
30impl<'de> SeqAccess<'de> for SeqDeserializer<'_, 'de> {
31    type Error = Error;
32
33    // If we don't call `next_class_element` afterwards, we won't get the index of the correct class.
34    #[inline]
35    fn class_ptr(&self) -> Result<usize, Self::Error> {
36        if self.de.class_index == 0 {
37            Err(Error::NotFoundClassPtr)
38        } else {
39            Ok(self.de.class_index)
40        }
41    }
42
43    fn next_primitive_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
44    where
45        T: DeserializeSeed<'de>,
46    {
47        // Check if there are no more elements.
48        if self.size == 0 || self.index == self.size {
49            #[cfg(feature = "tracing")]
50            tracing::debug!("seq end. index: {}, size: {}", self.index, self.size);
51            return Ok(None);
52        }
53
54        self.index += 1;
55        #[cfg(feature = "tracing")]
56        tracing::debug!(self.index);
57
58        seed.deserialize(&mut *self.de).map(Some)
59    }
60
61    #[inline]
62    fn next_class_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
63    where
64        T: DeserializeSeed<'de>,
65    {
66        self.next_primitive_element_seed(seed)
67    }
68
69    #[inline]
70    fn next_math_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
71    where
72        T: DeserializeSeed<'de>,
73    {
74        self.next_primitive_element_seed(seed)
75    }
76
77    #[inline]
78    fn next_cstring_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
79    where
80        T: DeserializeSeed<'de>,
81    {
82        self.next_primitive_element_seed(seed)
83    }
84
85    #[inline]
86    fn next_stringptr_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
87    where
88        T: DeserializeSeed<'de>,
89    {
90        self.next_primitive_element_seed(seed)
91    }
92}