-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
780b67a
commit e88a8fc
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |