customer/
customer.rs

1use serde::{Deserialize, Serialize};
2use superstruct::superstruct;
3
4#[superstruct(
5    variants(V1, V2, V3),
6    variant_attributes(derive(Deserialize, Serialize))
7)]
8#[derive(Deserialize, Serialize)]
9#[serde(untagged)]
10pub struct Customer {
11    pub name: String,
12    #[superstruct(only(V1), partial_getter(rename = "age_v1"))]
13    pub age: String,
14    #[superstruct(only(V2), partial_getter(rename = "age_v2"))]
15    pub age: u64,
16    #[superstruct(only(V3))]
17    pub dob: u64,
18    #[superstruct(only(V2, V3))]
19    pub favourite_colour: String,
20}
21
22fn main() {
23    let customer = Customer::V3(CustomerV3 {
24        name: "Michael".into(),
25        dob: 0,
26        favourite_colour: "purple".into(),
27    });
28    assert_eq!(customer.name(), "Michael");
29    assert_eq!(customer.dob(), Ok(&0));
30    assert_eq!(customer.favourite_colour().unwrap(), "purple");
31}