Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use serde::Deserialize;
- #[derive(Deserialize, Debug, PartialEq)]
- #[serde(rename_all = "snake_case")]
- enum Foo {
- BarA(BarA),
- BarB(BarB),
- }
- use std::ops::Deref;
- impl Deref for Foo {
- type Target = Common;
- fn deref(&self) -> &Common {
- match self {
- Foo::BarA(a) => &a.common,
- Foo::BarB(b) => &b.common,
- }
- }
- }
- #[derive(Deserialize, Debug, PartialEq)]
- struct BarA {
- a: String,
- #[serde(flatten)]
- common: Common,
- }
- #[derive(Deserialize, Debug, PartialEq)]
- struct BarB {
- b: String,
- #[serde(flatten)]
- common: Common,
- }
- #[derive(Deserialize, Debug, PartialEq)]
- struct Common {
- common: String,
- }
- impl Common {
- fn complex_logic(&self) -> bool {
- true
- }
- }
- fn main() {
- let input = r#"
- bar_a:
- common: Hello
- a: World
- "#;
- let foo: Foo = serde_yaml::from_str(&input).unwrap();
- println!("{:?}", foo);
- println!("{}", foo.complex_logic());
- // assert_eq!(
- // foo,
- // Foo {
- // common: "Hello".to_string(),
- // bar: Bar::BarA(BarA {
- // a: "World".to_string()
- // })
- // }
- // );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement