~tdeo/serde_bare

cde8ea7f52874780d8a410f499bcc7debc1da532 — Richard Bradfield 11 months ago 6695663
Deserialize Enum discriminant as Uint

The serializer encodes the variant of an enum as a BARE Uint, so the
deserializer should do the inverse when trying to deserialize a message.

Serde's serialize interface only receives variant indexes as u32,
despite the compiler allowing a cast to virtually any primitive integer
type.
1 files changed, 7 insertions(+), 3 deletions(-)

M src/de.rs
M src/de.rs => src/de.rs +7 -3
@@ 516,12 516,16 @@ where
        visitor.visit_enum(Enum::<'a, R>(self))
    }

    /// Returns Error::IdentifierUnsupported.
    fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
    /// Deserialize the enum discriminant as a BARE Uint
    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: de::Visitor<'de>,
    {
        Err(Error::IdentifierUnsupported)
        let Uint(id) = <Uint as de::Deserialize>::deserialize(&mut *self)?;
        let variant: u32 = id.try_into().map_err(|_| {
            Error::Message("Enum identifiers larger than u32 are not supported".to_string())
        })?;
        visitor.visit_u32(variant)
    }

    /// Returns Error::AnyUnsupported.