Why do I get an UnsupportedType error when serializing to TOML with a manually implemented Serialize for an enum with struct variants?

纵饮孤独 提交于 2021-01-27 07:00:11

问题


I'm trying to implement Serialize for an enum that includes struct variants. The serde.rs documentation indicates the following:

enum E {
    // Use three-step process:
    //   1. serialize_struct_variant
    //   2. serialize_field
    //   3. end
    Color { r: u8, g: u8, b: u8 },

    // Use three-step process:
    //   1. serialize_tuple_variant
    //   2. serialize_field
    //   3. end
    Point2D(f64, f64),

    // Use serialize_newtype_variant.
    Inches(u64),

    // Use serialize_unit_variant.
    Instance,
}

With that in mind, I proceeded to implemention:

use serde::ser::{Serialize, SerializeStructVariant, Serializer};
use serde_derive::Deserialize;

#[derive(Deserialize)]
enum Variants {
    VariantA,
    VariantB { k: u32, p: f64 },
}

impl Serialize for Variants {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            Variants::VariantA => serializer.serialize_unit_variant("Variants", 0, "VariantA"),
            Variants::VariantB { ref k, ref p } => {
                let mut state =
                    serializer.serialize_struct_variant("Variants", 1, "VariantB", 2)?;
                state.serialize_field("k", k)?;
                state.serialize_field("p", p)?;
                state.end()
            }
        }
    }
}

fn main() {
    let x = Variants::VariantB { k: 5, p: 5.0 };
    let toml_str = toml::to_string(&x).unwrap();
    println!("{}", toml_str);
}

The code compiles, but when I run it it fails:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: UnsupportedType', src/libcore/result.rs:999:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

I figured the issue must be in my use of the API, so I consulted the API documentation for StructVariant and it looks practically the same as my code. I'm sure I'm missing something, but I don't see it based on the docs and output.


回答1:


Enabling external tagging for the enum enables Serde to serialize/deserialize it to TOML:

#[derive(Deserialize)]
#[serde(tag = "type")]
enum Variants {
    VariantA,
    VariantB { k: u32, p: f64 },
}

toml::to_string(&Variants::VariantB { k: 42, p: 13.37 })

serializes to

type = VariantB
k = 42
p = 13.37

This works well in Vecs and HashMaps, too.




回答2:


The TOML format does not support enums with values:

use serde::Serialize; // 1.0.99
use toml; // 0.5.3

#[derive(Serialize)]
enum A {
    B(i32),
}

fn main() {
    match toml::to_string(&A::B(42)) {
        Ok(s) => println!("{}", s),
        Err(e) => eprintln!("Error: {}", e),
    }
}
Error: unsupported Rust type

It's unclear what you'd like your data structure to map to as TOML. Using JSON works just fine:

use serde::Serialize; // 1.0.99
use serde_json; // 1.0.40

#[derive(Serialize)]
enum Variants {
    VariantA,
    VariantB { k: u32, p: f64 },
}

fn main() {
    match serde_json::to_string(&Variants::VariantB { k: 42, p: 42.42 }) {
        Ok(s) => println!("{}", s),
        Err(e) => eprintln!("Error: {}", e),
    }
}
{"VariantB":{"k":42,"p":42.42}}


来源:https://stackoverflow.com/questions/57560593/why-do-i-get-an-unsupportedtype-error-when-serializing-to-toml-with-a-manually-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!