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                _ => <dyn Visitor>::ignore(),
63            })
64        }
65
66        fn deser_default() -> Self {
67            Self {
68                country: Deserialize::default(),
69                source: Deserialize::default(),
70                state: Deserialize::default(),
71            }
72        }
73
74        fn take_out(&mut self) -> Option<Self::Out> {
75            let (Some(country), Some(source), Some(state)) =
76                (self.country.take(), self.source, self.state.take())
77            else {
78                return None;
79            };
80            Some(Self::Out { country, source, state })
81        }
82    }
83
84    impl Map for Builder<'_> {
85        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
86            self.builder.key(k)
87        }
88
89        fn finish(&mut self) -> Result<()> {
90            *self.out = self.builder.take_out();
91            Ok(())
92        }
93    }
94
95    impl ObjectDeser for CustomerTaxLocation {
96        type Builder = CustomerTaxLocationBuilder;
97    }
98
99    impl FromValueOpt for CustomerTaxLocation {
100        fn from_value(v: Value) -> Option<Self> {
101            let Value::Object(obj) = v else {
102                return None;
103            };
104            let mut b = CustomerTaxLocationBuilder::deser_default();
105            for (k, v) in obj {
106                match k.as_str() {
107                    "country" => b.country = FromValueOpt::from_value(v),
108                    "source" => b.source = FromValueOpt::from_value(v),
109                    "state" => b.state = FromValueOpt::from_value(v),
110                    _ => {}
111                }
112            }
113            b.take_out()
114        }
115    }
116};
117/// The data source used to infer the customer's location.
118#[derive(Copy, Clone, Eq, PartialEq)]
119pub enum CustomerTaxLocationSource {
120    BillingAddress,
121    IpAddress,
122    PaymentMethod,
123    ShippingDestination,
124}
125impl CustomerTaxLocationSource {
126    pub fn as_str(self) -> &'static str {
127        use CustomerTaxLocationSource::*;
128        match self {
129            BillingAddress => "billing_address",
130            IpAddress => "ip_address",
131            PaymentMethod => "payment_method",
132            ShippingDestination => "shipping_destination",
133        }
134    }
135}
136
137impl std::str::FromStr for CustomerTaxLocationSource {
138    type Err = stripe_types::StripeParseError;
139    fn from_str(s: &str) -> Result<Self, Self::Err> {
140        use CustomerTaxLocationSource::*;
141        match s {
142            "billing_address" => Ok(BillingAddress),
143            "ip_address" => Ok(IpAddress),
144            "payment_method" => Ok(PaymentMethod),
145            "shipping_destination" => Ok(ShippingDestination),
146            _ => Err(stripe_types::StripeParseError),
147        }
148    }
149}
150impl std::fmt::Display for CustomerTaxLocationSource {
151    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
152        f.write_str(self.as_str())
153    }
154}
155
156impl std::fmt::Debug for CustomerTaxLocationSource {
157    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
158        f.write_str(self.as_str())
159    }
160}
161#[cfg(feature = "serialize")]
162impl serde::Serialize for CustomerTaxLocationSource {
163    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
164    where
165        S: serde::Serializer,
166    {
167        serializer.serialize_str(self.as_str())
168    }
169}
170impl miniserde::Deserialize for CustomerTaxLocationSource {
171    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
172        crate::Place::new(out)
173    }
174}
175
176impl miniserde::de::Visitor for crate::Place<CustomerTaxLocationSource> {
177    fn string(&mut self, s: &str) -> miniserde::Result<()> {
178        use std::str::FromStr;
179        self.out = Some(CustomerTaxLocationSource::from_str(s).map_err(|_| miniserde::Error)?);
180        Ok(())
181    }
182}
183
184stripe_types::impl_from_val_with_from_str!(CustomerTaxLocationSource);
185#[cfg(feature = "deserialize")]
186impl<'de> serde::Deserialize<'de> for CustomerTaxLocationSource {
187    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
188        use std::str::FromStr;
189        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
190        Self::from_str(&s)
191            .map_err(|_| serde::de::Error::custom("Unknown value for CustomerTaxLocationSource"))
192    }
193}