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.take(), 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(Clone, Eq, PartialEq)]
119#[non_exhaustive]
120pub enum CustomerTaxLocationSource {
121    BillingAddress,
122    IpAddress,
123    PaymentMethod,
124    ShippingDestination,
125    /// An unrecognized value from Stripe. Should not be used as a request parameter.
126    Unknown(String),
127}
128impl CustomerTaxLocationSource {
129    pub fn as_str(&self) -> &str {
130        use CustomerTaxLocationSource::*;
131        match self {
132            BillingAddress => "billing_address",
133            IpAddress => "ip_address",
134            PaymentMethod => "payment_method",
135            ShippingDestination => "shipping_destination",
136            Unknown(v) => v,
137        }
138    }
139}
140
141impl std::str::FromStr for CustomerTaxLocationSource {
142    type Err = std::convert::Infallible;
143    fn from_str(s: &str) -> Result<Self, Self::Err> {
144        use CustomerTaxLocationSource::*;
145        match s {
146            "billing_address" => Ok(BillingAddress),
147            "ip_address" => Ok(IpAddress),
148            "payment_method" => Ok(PaymentMethod),
149            "shipping_destination" => Ok(ShippingDestination),
150            v => {
151                tracing::warn!("Unknown value '{}' for enum '{}'", v, "CustomerTaxLocationSource");
152                Ok(Unknown(v.to_owned()))
153            }
154        }
155    }
156}
157impl std::fmt::Display for CustomerTaxLocationSource {
158    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
159        f.write_str(self.as_str())
160    }
161}
162
163impl std::fmt::Debug for CustomerTaxLocationSource {
164    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
165        f.write_str(self.as_str())
166    }
167}
168#[cfg(feature = "serialize")]
169impl serde::Serialize for CustomerTaxLocationSource {
170    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
171    where
172        S: serde::Serializer,
173    {
174        serializer.serialize_str(self.as_str())
175    }
176}
177impl miniserde::Deserialize for CustomerTaxLocationSource {
178    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
179        crate::Place::new(out)
180    }
181}
182
183impl miniserde::de::Visitor for crate::Place<CustomerTaxLocationSource> {
184    fn string(&mut self, s: &str) -> miniserde::Result<()> {
185        use std::str::FromStr;
186        self.out = Some(CustomerTaxLocationSource::from_str(s).expect("infallible"));
187        Ok(())
188    }
189}
190
191stripe_types::impl_from_val_with_from_str!(CustomerTaxLocationSource);
192#[cfg(feature = "deserialize")]
193impl<'de> serde::Deserialize<'de> for CustomerTaxLocationSource {
194    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
195        use std::str::FromStr;
196        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
197        Ok(Self::from_str(&s).expect("infallible"))
198    }
199}