1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct Address {
5 pub city: Option<String>,
7 pub country: Option<String>,
9 pub line1: Option<String>,
11 pub line2: Option<String>,
13 pub postal_code: Option<String>,
15 pub state: Option<String>,
17}
18#[doc(hidden)]
19pub struct AddressBuilder {
20 city: Option<Option<String>>,
21 country: Option<Option<String>>,
22 line1: Option<Option<String>>,
23 line2: Option<Option<String>>,
24 postal_code: Option<Option<String>>,
25 state: Option<Option<String>>,
26}
27
28#[allow(
29 unused_variables,
30 irrefutable_let_patterns,
31 clippy::let_unit_value,
32 clippy::match_single_binding,
33 clippy::single_match
34)]
35const _: () = {
36 use miniserde::de::{Map, Visitor};
37 use miniserde::json::Value;
38 use miniserde::{make_place, Deserialize, Result};
39 use stripe_types::miniserde_helpers::FromValueOpt;
40 use stripe_types::{MapBuilder, ObjectDeser};
41
42 make_place!(Place);
43
44 impl Deserialize for Address {
45 fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
46 Place::new(out)
47 }
48 }
49
50 struct Builder<'a> {
51 out: &'a mut Option<Address>,
52 builder: AddressBuilder,
53 }
54
55 impl Visitor for Place<Address> {
56 fn map(&mut self) -> Result<Box<dyn Map + '_>> {
57 Ok(Box::new(Builder { out: &mut self.out, builder: AddressBuilder::deser_default() }))
58 }
59 }
60
61 impl MapBuilder for AddressBuilder {
62 type Out = Address;
63 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
64 Ok(match k {
65 "city" => Deserialize::begin(&mut self.city),
66 "country" => Deserialize::begin(&mut self.country),
67 "line1" => Deserialize::begin(&mut self.line1),
68 "line2" => Deserialize::begin(&mut self.line2),
69 "postal_code" => Deserialize::begin(&mut self.postal_code),
70 "state" => Deserialize::begin(&mut self.state),
71
72 _ => <dyn Visitor>::ignore(),
73 })
74 }
75
76 fn deser_default() -> Self {
77 Self {
78 city: Deserialize::default(),
79 country: Deserialize::default(),
80 line1: Deserialize::default(),
81 line2: Deserialize::default(),
82 postal_code: Deserialize::default(),
83 state: Deserialize::default(),
84 }
85 }
86
87 fn take_out(&mut self) -> Option<Self::Out> {
88 let (
89 Some(city),
90 Some(country),
91 Some(line1),
92 Some(line2),
93 Some(postal_code),
94 Some(state),
95 ) = (
96 self.city.take(),
97 self.country.take(),
98 self.line1.take(),
99 self.line2.take(),
100 self.postal_code.take(),
101 self.state.take(),
102 )
103 else {
104 return None;
105 };
106 Some(Self::Out { city, country, line1, line2, postal_code, state })
107 }
108 }
109
110 impl Map for Builder<'_> {
111 fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
112 self.builder.key(k)
113 }
114
115 fn finish(&mut self) -> Result<()> {
116 *self.out = self.builder.take_out();
117 Ok(())
118 }
119 }
120
121 impl ObjectDeser for Address {
122 type Builder = AddressBuilder;
123 }
124
125 impl FromValueOpt for Address {
126 fn from_value(v: Value) -> Option<Self> {
127 let Value::Object(obj) = v else {
128 return None;
129 };
130 let mut b = AddressBuilder::deser_default();
131 for (k, v) in obj {
132 match k.as_str() {
133 "city" => b.city = FromValueOpt::from_value(v),
134 "country" => b.country = FromValueOpt::from_value(v),
135 "line1" => b.line1 = FromValueOpt::from_value(v),
136 "line2" => b.line2 = FromValueOpt::from_value(v),
137 "postal_code" => b.postal_code = FromValueOpt::from_value(v),
138 "state" => b.state = FromValueOpt::from_value(v),
139
140 _ => {}
141 }
142 }
143 b.take_out()
144 }
145 }
146};