0

我正在反序列化 Avro 的外部编码枚举。我创建了一个与其架构匹配的枚举,但我不知道如何获取变体标识符(一个数字)并选择正确的枚举变体。

目标结构:

#[derive(Deserialize, Debug)]
struct UT {
    timestamp: Timestamp,
    metric: String,
}

#[derive(Deserialize, Debug)]
enum Timestamp {
    Long(u64),
    Int(i64),
    Float(f32),
    Double(f64)
}

当我到达时deserialize_enum,我可以从流中读取变体 id,但我不知道如何使用它来选择变体。

调度到枚举变体是通过AvroEnumVisitor实现EnumAccess和来完成的VariantAccess。我无法让我的代码VariantAccess被调用。EnumAccesssvariant_seed被我不知道如何引导它调用,这里是细节:

// `EnumAccess` is provided to the `Visitor` to give it the ability to determine
// which variant of the enum is supposed to be deserialized.
//
// Note that all enum deserialization methods in Serde refer exclusively to the
// "externally tagged" enum representation.
impl<'de, 'a> EnumAccess<'de> for AvroEnumVisitor<'a, 'de> {
    type Error = AvroError;
    type Variant = Self;

    fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
        where
            V: serde::de::DeserializeSeed<'de>,
    {
        println!("EnumAccess::variant_seed");

        // !!! This is the index in to the timestamp enum
        let variant = self.de.visit_varint();
        // !!! it's 1, corresponding to Int(i64)

        // This is code I just copy/pasted
        let val = match seed.deserialize(&mut *self.de) {
            Ok(t) => t,
            Err(e) => {
                println!("error! {:#?}", e);
                panic!("not sure how to direct deserialize");
            }
        };

        Ok((val,self))
    }
}

操场

4

0 回答 0