stripe_shared/
address.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct Address {
5    /// City, district, suburb, town, or village.
6    pub city: Option<String>,
7    /// Two-letter country code ([ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
8    pub country: Option<String>,
9    /// Address line 1, such as the street, PO Box, or company name.
10    pub line1: Option<String>,
11    /// Address line 2, such as the apartment, suite, unit, or building.
12    pub line2: Option<String>,
13    /// ZIP or postal code.
14    pub postal_code: Option<String>,
15    /// State, county, province, or region.
16    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::{Deserialize, Result, make_place};
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                _ => <dyn Visitor>::ignore(),
72            })
73        }
74
75        fn deser_default() -> Self {
76            Self {
77                city: Deserialize::default(),
78                country: Deserialize::default(),
79                line1: Deserialize::default(),
80                line2: Deserialize::default(),
81                postal_code: Deserialize::default(),
82                state: Deserialize::default(),
83            }
84        }
85
86        fn take_out(&mut self) -> Option<Self::Out> {
87            let (
88                Some(city),
89                Some(country),
90                Some(line1),
91                Some(line2),
92                Some(postal_code),
93                Some(state),
94            ) = (
95                self.city.take(),
96                self.country.take(),
97                self.line1.take(),
98                self.line2.take(),
99                self.postal_code.take(),
100                self.state.take(),
101            )
102            else {
103                return None;
104            };
105            Some(Self::Out { city, country, line1, line2, postal_code, state })
106        }
107    }
108
109    impl Map for Builder<'_> {
110        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
111            self.builder.key(k)
112        }
113
114        fn finish(&mut self) -> Result<()> {
115            *self.out = self.builder.take_out();
116            Ok(())
117        }
118    }
119
120    impl ObjectDeser for Address {
121        type Builder = AddressBuilder;
122    }
123
124    impl FromValueOpt for Address {
125        fn from_value(v: Value) -> Option<Self> {
126            let Value::Object(obj) = v else {
127                return None;
128            };
129            let mut b = AddressBuilder::deser_default();
130            for (k, v) in obj {
131                match k.as_str() {
132                    "city" => b.city = FromValueOpt::from_value(v),
133                    "country" => b.country = FromValueOpt::from_value(v),
134                    "line1" => b.line1 = FromValueOpt::from_value(v),
135                    "line2" => b.line2 = FromValueOpt::from_value(v),
136                    "postal_code" => b.postal_code = FromValueOpt::from_value(v),
137                    "state" => b.state = FromValueOpt::from_value(v),
138                    _ => {}
139                }
140            }
141            b.take_out()
142        }
143    }
144};