Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. use serde::Deserialize;
  2.  
  3. #[derive(Deserialize, Debug, PartialEq)]
  4. #[serde(rename_all = "snake_case")]
  5. enum Foo {
  6. BarA(BarA),
  7. BarB(BarB),
  8. }
  9.  
  10. use std::ops::Deref;
  11. impl Deref for Foo {
  12. type Target = Common;
  13. fn deref(&self) -> &Common {
  14. match self {
  15. Foo::BarA(a) => &a.common,
  16. Foo::BarB(b) => &b.common,
  17. }
  18. }
  19. }
  20.  
  21. #[derive(Deserialize, Debug, PartialEq)]
  22. struct BarA {
  23. a: String,
  24. #[serde(flatten)]
  25. common: Common,
  26. }
  27.  
  28. #[derive(Deserialize, Debug, PartialEq)]
  29. struct BarB {
  30. b: String,
  31. #[serde(flatten)]
  32. common: Common,
  33. }
  34.  
  35. #[derive(Deserialize, Debug, PartialEq)]
  36. struct Common {
  37. common: String,
  38. }
  39.  
  40. impl Common {
  41. fn complex_logic(&self) -> bool {
  42. true
  43. }
  44. }
  45.  
  46. fn main() {
  47. let input = r#"
  48. bar_a:
  49. common: Hello
  50. a: World
  51. "#;
  52.  
  53. let foo: Foo = serde_yaml::from_str(&input).unwrap();
  54. println!("{:?}", foo);
  55. println!("{}", foo.complex_logic());
  56.  
  57. // assert_eq!(
  58. // foo,
  59. // Foo {
  60. // common: "Hello".to_string(),
  61. // bar: Bar::BarA(BarA {
  62. // a: "World".to_string()
  63. // })
  64. // }
  65. // );
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement