Sha256: 360618cced5c6339afbbc8799df2c047aaeeb1fa7d840dc777c2ffd83871883e

Contents?: true

Size: 1.02 KB

Versions: 25

Compression:

Stored size: 1.02 KB

Contents

//! An example showing off the usage of `Deserialize` to automatically decode
//! TOML into a Rust `struct`, with enums.

#![deny(warnings)]
#![allow(dead_code)]

use serde_derive::Deserialize;

/// This is what we're going to decode into.
#[derive(Debug, Deserialize)]
struct Config {
    plain: MyEnum,
    plain_table: MyEnum,
    tuple: MyEnum,
    #[serde(rename = "struct")]
    structv: MyEnum,
    newtype: MyEnum,
    my_enum: Vec<MyEnum>,
}

#[derive(Debug, Deserialize)]
enum MyEnum {
    Plain,
    Tuple(i64, bool),
    NewType(String),
    Struct { value: i64 },
}

fn main() {
    let toml_str = r#"
    plain = "Plain"
    plain_table = { Plain = {} }
    tuple = { Tuple = { 0 = 123, 1 = true } }
    struct = { Struct = { value = 123 } }
    newtype = { NewType = "value" }
    my_enum = [
        { Plain = {} },
        { Tuple = { 0 = 123, 1 = true } },
        { NewType = "value" },
        { Struct = { value = 123 } }
    ]"#;

    let decoded: Config = toml::from_str(toml_str).unwrap();
    println!("{:#?}", decoded);
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
wasmtime-18.0.3 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-17.0.1 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-17.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-16.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-15.0.1 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-15.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-14.0.4 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-14.0.3 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-14.0.1 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-14.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-13.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-12.0.1 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-12.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-11.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-10.0.1 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-10.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-9.0.4 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-9.0.1 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-8.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs
wasmtime-7.0.0 ./ext/cargo-vendor/toml-0.5.11/examples/enum_external.rs