serde_hkx/bytes/de/
enum_access.rs

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