serde_hkx/bytes/de/
class_index_map.rs

1//! Deserializing each field element in an `Struct`
2use super::BytesDeserializer;
3use crate::errors::de::Error;
4use havok_serde::de::{ClassIndexAccess, DeserializeSeed};
5use havok_types::Pointer;
6
7/// Order in which they are called.: `SeqAccess::next_class_element` -> `next_key` -> `next_value`
8#[derive(Debug)]
9pub struct BytesClassIndexMapDeserializer<'a, 'de: 'a> {
10    /// Root Deserializer
11    de: &'a mut BytesDeserializer<'de>,
12}
13
14impl<'a, 'de> BytesClassIndexMapDeserializer<'a, 'de> {
15    /// Create a new map deserializer
16    #[inline]
17    pub const fn new(de: &'a mut BytesDeserializer<'de>) -> Self {
18        Self { de }
19    }
20}
21
22impl<'de> ClassIndexAccess<'de> for BytesClassIndexMapDeserializer<'_, 'de> {
23    type Error = Error;
24
25    // Call constructor by class name
26    fn next_key(&mut self) -> Result<&'de str, Self::Error> {
27        let mut start_offset = 0;
28
29        if let Some((virtual_src, (_section_index, name_start_offset))) = &self
30            .de
31            .data_fixups
32            .virtual_fixups
33            .get_index(self.de.class_index)
34        {
35            // NOTE: First increment class_index(XML: `#0000`) to 1 based index notation.
36            self.de.class_index += 1;
37            self.de.takable_class_index = Some(Pointer::new(self.de.class_index));
38            start_offset = *name_start_offset;
39
40            if let Some(name) = self.de.classnames.get(name_start_offset) {
41                let virtual_src_abs =
42                    (*virtual_src + self.de.data_header.absolute_data_start) as usize;
43                self.de.current_position = virtual_src_abs;
44
45                #[cfg(feature = "tracing")]
46                tracing::debug!(
47                    "name: {name}, class_index: {}, virtual_src: {virtual_src_abs:#x}",
48                    self.de.class_index
49                );
50                return Ok(*name);
51            };
52        };
53
54        Err(Error::NotFoundClass {
55            index: self.de.class_index,
56            start_offset,
57        })
58    }
59
60    #[inline]
61    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
62    where
63        V: DeserializeSeed<'de>,
64    {
65        seed.deserialize(&mut *self.de)
66    }
67}