serde_hkx/xml/de/
enum_access.rs

1//! Deserializing `enum` or `bitflags`
2use super::XmlDeserializer;
3use crate::errors::de::{Error, Result};
4use havok_serde::de::{DeserializeSeed, EnumAccess, VariantAccess};
5
6/// A enum for deserializing each variant in an `enum`.
7///
8/// # Expected XML
9/// - Note that `</hkparam>` confirms the presence but does not consume.
10/// ```xml
11/// EVENT_MODE_IGNORE_FROM_GENERATOR</hkparam>
12/// ```
13#[derive(Debug)]
14pub struct EnumDeserializer<'a, 'de: 'a> {
15    de: &'a mut XmlDeserializer<'de>,
16}
17
18impl<'a, 'de> EnumDeserializer<'a, 'de> {
19    #[inline]
20    pub const fn new(de: &'a mut XmlDeserializer<'de>) -> Self {
21        EnumDeserializer { de }
22    }
23}
24
25// `EnumAccess` is provided to the `Visitor` to give it the ability to determine
26// which variant of the enum is supposed to be deserialized.
27//
28// Note that all enum deserialization methods in Serde refer exclusively to the
29// "externally tagged" enum representation.
30impl<'de> EnumAccess<'de> for EnumDeserializer<'_, 'de> {
31    type Error = Error;
32    type Variant = Self;
33
34    #[inline]
35    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
36    where
37        V: DeserializeSeed<'de>,
38    {
39        let val = seed.deserialize(&mut *self.de)?;
40        Ok((val, self))
41    }
42}
43
44/// `VariantAccess` is provided to the `Visitor` to give it the ability to see
45/// the content of the single variant that it decided to deserialize.
46impl<'de> VariantAccess<'de> for EnumDeserializer<'_, 'de> {
47    type Error = Error;
48
49    #[inline]
50    fn unit_variant(self) -> Result<(), Self::Error> {
51        Ok(())
52    }
53}