Lines
100 %
Functions
Branches
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
enum InnerEnum {
Unit,
Newtype(bool),
Tuple(bool, i32),
Struct { field: char },
}
#[serde(deny_unknown_fields)]
struct Container {
field: InnerEnum,
enum OuterEnum {
Variant(Container),
Sum { field: InnerEnum, value: i32 },
#[serde(tag = "tag")]
enum OuterEnumInternal {
#[serde(tag = "tag", content = "c")]
enum OuterEnumAdjacent {
#[serde(untagged)]
enum OuterEnumUntagged {
#[test]
fn test_enum_in_enum_roundtrip() {
let outer = OuterEnum::Variant(Container {
field: InnerEnum::Unit,
});
let ron = ron::to_string(&outer).unwrap();
assert_eq!(ron, "Variant((field:Unit))");
let de = ron::from_str::<OuterEnum>(&ron);
assert_eq!(de, Ok(outer));
let outer = OuterEnum::Sum {
field: InnerEnum::Newtype(true),
value: 42,
};
assert_eq!(ron, "Sum(field:Newtype(true),value:42)");
field: InnerEnum::Tuple(true, 24),
assert_eq!(ron, "Sum(field:Tuple(true,24),value:42)");
field: InnerEnum::Struct { field: '🦀' },
assert_eq!(ron, "Sum(field:Struct(field:'🦀'),value:42)");
fn test_enum_in_internally_tagged_roundtrip() {
let outer = OuterEnumInternal::Variant(Container {
assert_eq!(ron, "(tag:\"Variant\",field:Unit)");
let de = ron::from_str::<OuterEnumInternal>(&ron);
let outer = OuterEnumInternal::Sum {
assert_eq!(ron, "(tag:\"Sum\",field:Newtype(true),value:42)");
assert_eq!(ron, "(tag:\"Sum\",field:Tuple(true,24),value:42)");
assert_eq!(ron, "(tag:\"Sum\",field:Struct(field:'🦀'),value:42)");
fn test_enum_in_adjacently_tagged_roundtrip() {
let outer = OuterEnumAdjacent::Variant(Container {
assert_eq!(ron, "(tag:Variant,c:(field:Unit))");
let de = ron::from_str::<OuterEnumAdjacent>(&ron);
let outer = OuterEnumAdjacent::Sum {
assert_eq!(ron, "(tag:Sum,c:(field:Newtype(true),value:42))");
assert_eq!(ron, "(tag:Sum,c:(field:Tuple(true,24),value:42))");
assert_eq!(ron, "(tag:Sum,c:(field:Struct(field:'🦀'),value:42))");
fn test_enum_in_untagged_roundtrip() {
let outer = OuterEnumUntagged::Variant(Container {
assert_eq!(ron, "(field:Unit)");
let de = ron::from_str::<OuterEnumUntagged>(&ron);
let outer = OuterEnumUntagged::Sum {
assert_eq!(ron, "(field:Newtype(true),value:42)");
assert_eq!(ron, "(field:Tuple(true,24),value:42)");
assert_eq!(ron, "(field:Struct(field:'🦀'),value:42)");