Skip to content

Commit

Permalink
Add another example (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul authored Oct 19, 2023
1 parent 780b67a commit e88a8fc
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/customer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use serde::{Deserialize, Serialize};
use superstruct::superstruct;

#[superstruct(
variants(V1, V2, V3),
variant_attributes(derive(Deserialize, Serialize))
)]
#[derive(Deserialize, Serialize)]
#[serde(untagged)]
pub struct Customer {
pub name: String,
#[superstruct(only(V1), partial_getter(rename = "age_v1"))]
pub age: String,
#[superstruct(only(V2), partial_getter(rename = "age_v2"))]
pub age: u64,
#[superstruct(only(V3))]
pub dob: u64,
#[superstruct(only(V2, V3))]
pub favourite_colour: String,
}

fn main() {
let customer = Customer::V3(CustomerV3 {
name: "Michael".into(),
dob: 0,
favourite_colour: "purple".into(),
});
assert_eq!(customer.name(), "Michael");
assert_eq!(customer.dob(), Ok(&0));
assert_eq!(customer.favourite_colour().unwrap(), "purple");
}

0 comments on commit e88a8fc

Please sign in to comment.