Files
sousa-gecko/third_party/rust/toml/examples/enum_external.rs
T
Ben Dean-Kawamura a09712605d Bug 1998236 - Prepare for toml 0.9, r=supply-chain-reviewers
Added a build hack to upgrade all 0.5 consumers to 0.9.  This is in
preperation of upgrading the `ohttp` crate, which is on toml 0.9.

Added audits for the new versions.  I added trusted audits for all the
toml crates.  cargo depends on toml, if they trust that seems like good
grounds for us to trust it as well.

Differential Revision: https://phabricator.services.mozilla.com/D271295
2025-11-12 22:27:41 +00:00

46 lines
1.0 KiB
Rust

//! 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::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:#?}");
}