From e88a8fc5441410aa76af97ab3ab4d7bdae0bb490 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Thu, 19 Oct 2023 14:57:47 +1100 Subject: [PATCH] Add another example (#32) --- examples/customer.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/customer.rs diff --git a/examples/customer.rs b/examples/customer.rs new file mode 100644 index 00000000..2b42dcb8 --- /dev/null +++ b/examples/customer.rs @@ -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"); +}