Skip to main content

stripe_shared/
address.rs

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