1//! Deserializing `enum` or `bitflags`
2use super::BytesDeserializer;
3use crate::errors::de::{Error, Result};
4use havok_serde::de::{DeserializeSeed, EnumAccess, VariantAccess};
56/// 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}
1617impl<'a, 'de> EnumDeserializer<'a, 'de> {
18#[inline]
19pub const fn new(de: &'a mut BytesDeserializer<'de>) -> Self {
20 EnumDeserializer { de }
21 }
22}
2324// `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> {
30type Error = Error;
31type Variant = Self;
3233#[inline]
34fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
35where
36V: DeserializeSeed<'de>,
37 {
38let val = seed.deserialize(&mut *self.de)?;
39Ok((val, self))
40 }
41}
4243/// `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> {
46type Error = Error;
4748#[inline]
49fn unit_variant(self) -> Result<(), Self::Error> {
50Ok(())
51 }
52}