macro_rules! nested_singleton_map_serialize { ($value:expr, $writer:expr) => { ... }; }
Expand description
A macro that serializes a nested singleton map to a YAML format.
This macro uses the serde_yml
crate to serialize the input value to the provided writer.
§Examples
use serde::{Deserialize, Serialize};
use serde_yml::with::nested_singleton_map;
use serde_yml::nested_singleton_map_serialize;
use serde_yml::nested_singleton_map_deserialize;
// Define the inner enum with different variants
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum InnerEnum {
Variant1,
Variant2(String),
Variant3 {
field1: i32,
field2: bool,
},
}
// Define the outer enum that contains the inner enum as a field
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum OuterEnum {
Variant1(InnerEnum),
Variant2 {
inner: InnerEnum,
},
}
// Define a struct that contains the outer enum as a field
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct NestedEnumStruct {
#[serde(with = "nested_singleton_map")]
field: OuterEnum,
}
// Example 1: OuterEnum::Variant1(InnerEnum::Variant1)
let input1 = NestedEnumStruct {
field: OuterEnum::Variant1(InnerEnum::Variant1),
};
let mut writer = Vec::new();
nested_singleton_map_serialize!(&input1, &mut writer).unwrap();
println!("\n✅ Serialized YAML for Example 1:\n{}", String::from_utf8(writer).unwrap());