stripe_shared/
customer_tax_location.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct CustomerTaxLocation {
5    /// The identified tax country of the customer.
6    pub country: String,
7    /// The data source used to infer the customer's location.
8    pub source: CustomerTaxLocationSource,
9    /// The identified tax state, county, province, or region of the customer.
10    pub state: Option<String>,
11}
12#[doc(hidden)]
13pub struct CustomerTaxLocationBuilder {
14    country: Option<String>,
15    source: Option<CustomerTaxLocationSource>,
16    state: Option<Option<String>>,
17}
18
19#[allow(
20    unused_variables,
21    irrefutable_let_patterns,
22    clippy::let_unit_value,
23    clippy::match_single_binding,
24    clippy::single_match
25)]
26const _: () = {
27    use miniserde::de::{Map, Visitor};
28    use miniserde::json::Value;
29    use miniserde::{Deserialize, Result, make_place};
30    use stripe_types::miniserde_helpers::FromValueOpt;
31    use stripe_types::{MapBuilder, ObjectDeser};
32
33    make_place!(Place);
34
35    impl Deserialize for CustomerTaxLocation {
36        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
37            Place::new(out)
38        }
39    }
40
41    struct Builder<'a> {
42        out: &'a mut Option<CustomerTaxLocation>,
43        builder: CustomerTaxLocationBuilder,
44    }
45
46    impl Visitor for Place<CustomerTaxLocation> {
47        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
48            Ok(Box::new(Builder {
49                out: &mut self.out,
50                builder: CustomerTaxLocationBuilder::deser_default(),
51            }))
52        }
53    }
54
55    impl MapBuilder for CustomerTaxLocationBuilder {
56        type Out = CustomerTaxLocation;
57        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
58            Ok(match k {
59                "country" => Deserialize::begin(&mut self.country),
60                "source" => Deserialize::begin(&mut self.source),
61                "state" => Deserialize::begin(&mut self.state),
62
63                _ => <dyn Visitor>::ignore(),
64            })
65        }
66
67        fn deser_default() -> Self {
68            Self {
69                country: Deserialize::default(),
70                source: Deserialize::default(),
71                state: Deserialize::default(),
72            }
73        }
74
75        fn take_out(&mut self) -> Option<Self::Out> {
76            let (Some(country), Some(source), Some(state)) =
77                (self.country.take(), self.source, self.state.take())
78            else {
79                return None;
80            };
81            Some(Self::Out { country, source, state })
82        }
83    }
84
85    impl Map for Builder<'_> {
86        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
87            self.builder.key(k)
88        }
89
90        fn finish(&mut self) -> Result<()> {
91            *self.out = self.builder.take_out();
92            Ok(())
93        }
94    }
95
96    impl ObjectDeser for CustomerTaxLocation {
97        type Builder = CustomerTaxLocationBuilder;
98    }
99
100    impl FromValueOpt for CustomerTaxLocation {
101        fn from_value(v: Value) -> Option<Self> {
102            let Value::Object(obj) = v else {
103                return None;
104            };
105            let mut b = CustomerTaxLocationBuilder::deser_default();
106            for (k, v) in obj {
107                match k.as_str() {
108                    "country" => b.country = FromValueOpt::from_value(v),
109                    "source" => b.source = FromValueOpt::from_value(v),
110                    "state" => b.state = FromValueOpt::from_value(v),
111
112                    _ => {}
113                }
114            }
115            b.take_out()
116        }
117    }
118};
119/// The data source used to infer the customer's location.
120#[derive(Copy, Clone, Eq, PartialEq)]
121pub enum CustomerTaxLocationSource {
122    BillingAddress,
123    IpAddress,
124    PaymentMethod,
125    ShippingDestination,
126}
127impl CustomerTaxLocationSource {
128    pub fn as_str(self) -> &'static str {
129        use CustomerTaxLocationSource::*;
130        match self {
131            BillingAddress => "billing_address",
132            IpAddress => "ip_address",
133            PaymentMethod => "payment_method",
134            ShippingDestination => "shipping_destination",
135        }
136    }
137}
138
139impl std::str::FromStr for CustomerTaxLocationSource {
140    type Err = stripe_types::StripeParseError;
141    fn from_str(s: &str) -> Result<Self, Self::Err> {
142        use CustomerTaxLocationSource::*;
143        match s {
144            "billing_address" => Ok(BillingAddress),
145            "ip_address" => Ok(IpAddress),
146            "payment_method" => Ok(PaymentMethod),
147            "shipping_destination" => Ok(ShippingDestination),
148            _ => Err(stripe_types::StripeParseError),
149        }
150    }
151}
152impl std::fmt::Display for CustomerTaxLocationSource {
153    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
154        f.write_str(self.as_str())
155    }
156}
157
158impl std::fmt::Debug for CustomerTaxLocationSource {
159    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
160        f.write_str(self.as_str())
161    }
162}
163#[cfg(feature = "serialize")]
164impl serde::Serialize for CustomerTaxLocationSource {
165    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
166    where
167        S: serde::Serializer,
168    {
169        serializer.serialize_str(self.as_str())
170    }
171}
172impl miniserde::Deserialize for CustomerTaxLocationSource {
173    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
174        crate::Place::new(out)
175    }
176}
177
178impl miniserde::de::Visitor for crate::Place<CustomerTaxLocationSource> {
179    fn string(&mut self, s: &str) -> miniserde::Result<()> {
180        use std::str::FromStr;
181        self.out = Some(CustomerTaxLocationSource::from_str(s).map_err(|_| miniserde::Error)?);
182        Ok(())
183    }
184}
185
186stripe_types::impl_from_val_with_from_str!(CustomerTaxLocationSource);
187#[cfg(feature = "deserialize")]
188impl<'de> serde::Deserialize<'de> for CustomerTaxLocationSource {
189    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
190        use std::str::FromStr;
191        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
192        Self::from_str(&s)
193            .map_err(|_| serde::de::Error::custom("Unknown value for CustomerTaxLocationSource"))
194    }
195}