stripe_shared/
customer_tax.rs

1#[derive(Clone, Debug)]
2#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
3#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4pub struct CustomerTax {
5    /// Surfaces if automatic tax computation is possible given the current customer location information.
6    pub automatic_tax: CustomerTaxAutomaticTax,
7    /// A recent IP address of the customer used for tax reporting and tax location inference.
8    pub ip_address: Option<String>,
9    /// The identified tax location of the customer.
10    pub location: Option<stripe_shared::CustomerTaxLocation>,
11    /// The tax calculation provider used for location resolution.
12    /// Defaults to `stripe` when not using a [third-party provider](/tax/third-party-apps).
13    pub provider: CustomerTaxProvider,
14}
15#[doc(hidden)]
16pub struct CustomerTaxBuilder {
17    automatic_tax: Option<CustomerTaxAutomaticTax>,
18    ip_address: Option<Option<String>>,
19    location: Option<Option<stripe_shared::CustomerTaxLocation>>,
20    provider: Option<CustomerTaxProvider>,
21}
22
23#[allow(
24    unused_variables,
25    irrefutable_let_patterns,
26    clippy::let_unit_value,
27    clippy::match_single_binding,
28    clippy::single_match
29)]
30const _: () = {
31    use miniserde::de::{Map, Visitor};
32    use miniserde::json::Value;
33    use miniserde::{Deserialize, Result, make_place};
34    use stripe_types::miniserde_helpers::FromValueOpt;
35    use stripe_types::{MapBuilder, ObjectDeser};
36
37    make_place!(Place);
38
39    impl Deserialize for CustomerTax {
40        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
41            Place::new(out)
42        }
43    }
44
45    struct Builder<'a> {
46        out: &'a mut Option<CustomerTax>,
47        builder: CustomerTaxBuilder,
48    }
49
50    impl Visitor for Place<CustomerTax> {
51        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
52            Ok(Box::new(Builder {
53                out: &mut self.out,
54                builder: CustomerTaxBuilder::deser_default(),
55            }))
56        }
57    }
58
59    impl MapBuilder for CustomerTaxBuilder {
60        type Out = CustomerTax;
61        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
62            Ok(match k {
63                "automatic_tax" => Deserialize::begin(&mut self.automatic_tax),
64                "ip_address" => Deserialize::begin(&mut self.ip_address),
65                "location" => Deserialize::begin(&mut self.location),
66                "provider" => Deserialize::begin(&mut self.provider),
67                _ => <dyn Visitor>::ignore(),
68            })
69        }
70
71        fn deser_default() -> Self {
72            Self {
73                automatic_tax: Deserialize::default(),
74                ip_address: Deserialize::default(),
75                location: Deserialize::default(),
76                provider: Deserialize::default(),
77            }
78        }
79
80        fn take_out(&mut self) -> Option<Self::Out> {
81            let (Some(automatic_tax), Some(ip_address), Some(location), Some(provider)) = (
82                self.automatic_tax.take(),
83                self.ip_address.take(),
84                self.location.take(),
85                self.provider.take(),
86            ) else {
87                return None;
88            };
89            Some(Self::Out { automatic_tax, ip_address, location, provider })
90        }
91    }
92
93    impl Map for Builder<'_> {
94        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
95            self.builder.key(k)
96        }
97
98        fn finish(&mut self) -> Result<()> {
99            *self.out = self.builder.take_out();
100            Ok(())
101        }
102    }
103
104    impl ObjectDeser for CustomerTax {
105        type Builder = CustomerTaxBuilder;
106    }
107
108    impl FromValueOpt for CustomerTax {
109        fn from_value(v: Value) -> Option<Self> {
110            let Value::Object(obj) = v else {
111                return None;
112            };
113            let mut b = CustomerTaxBuilder::deser_default();
114            for (k, v) in obj {
115                match k.as_str() {
116                    "automatic_tax" => b.automatic_tax = FromValueOpt::from_value(v),
117                    "ip_address" => b.ip_address = FromValueOpt::from_value(v),
118                    "location" => b.location = FromValueOpt::from_value(v),
119                    "provider" => b.provider = FromValueOpt::from_value(v),
120                    _ => {}
121                }
122            }
123            b.take_out()
124        }
125    }
126};
127/// Surfaces if automatic tax computation is possible given the current customer location information.
128#[derive(Clone, Eq, PartialEq)]
129#[non_exhaustive]
130pub enum CustomerTaxAutomaticTax {
131    Failed,
132    NotCollecting,
133    Supported,
134    UnrecognizedLocation,
135    /// An unrecognized value from Stripe. Should not be used as a request parameter.
136    Unknown(String),
137}
138impl CustomerTaxAutomaticTax {
139    pub fn as_str(&self) -> &str {
140        use CustomerTaxAutomaticTax::*;
141        match self {
142            Failed => "failed",
143            NotCollecting => "not_collecting",
144            Supported => "supported",
145            UnrecognizedLocation => "unrecognized_location",
146            Unknown(v) => v,
147        }
148    }
149}
150
151impl std::str::FromStr for CustomerTaxAutomaticTax {
152    type Err = std::convert::Infallible;
153    fn from_str(s: &str) -> Result<Self, Self::Err> {
154        use CustomerTaxAutomaticTax::*;
155        match s {
156            "failed" => Ok(Failed),
157            "not_collecting" => Ok(NotCollecting),
158            "supported" => Ok(Supported),
159            "unrecognized_location" => Ok(UnrecognizedLocation),
160            v => {
161                tracing::warn!("Unknown value '{}' for enum '{}'", v, "CustomerTaxAutomaticTax");
162                Ok(Unknown(v.to_owned()))
163            }
164        }
165    }
166}
167impl std::fmt::Display for CustomerTaxAutomaticTax {
168    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
169        f.write_str(self.as_str())
170    }
171}
172
173impl std::fmt::Debug for CustomerTaxAutomaticTax {
174    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
175        f.write_str(self.as_str())
176    }
177}
178#[cfg(feature = "serialize")]
179impl serde::Serialize for CustomerTaxAutomaticTax {
180    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
181    where
182        S: serde::Serializer,
183    {
184        serializer.serialize_str(self.as_str())
185    }
186}
187impl miniserde::Deserialize for CustomerTaxAutomaticTax {
188    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
189        crate::Place::new(out)
190    }
191}
192
193impl miniserde::de::Visitor for crate::Place<CustomerTaxAutomaticTax> {
194    fn string(&mut self, s: &str) -> miniserde::Result<()> {
195        use std::str::FromStr;
196        self.out = Some(CustomerTaxAutomaticTax::from_str(s).expect("infallible"));
197        Ok(())
198    }
199}
200
201stripe_types::impl_from_val_with_from_str!(CustomerTaxAutomaticTax);
202#[cfg(feature = "deserialize")]
203impl<'de> serde::Deserialize<'de> for CustomerTaxAutomaticTax {
204    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
205        use std::str::FromStr;
206        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
207        Ok(Self::from_str(&s).expect("infallible"))
208    }
209}
210/// The tax calculation provider used for location resolution.
211/// Defaults to `stripe` when not using a [third-party provider](/tax/third-party-apps).
212#[derive(Clone, Eq, PartialEq)]
213#[non_exhaustive]
214pub enum CustomerTaxProvider {
215    Anrok,
216    Avalara,
217    Sphere,
218    Stripe,
219    /// An unrecognized value from Stripe. Should not be used as a request parameter.
220    Unknown(String),
221}
222impl CustomerTaxProvider {
223    pub fn as_str(&self) -> &str {
224        use CustomerTaxProvider::*;
225        match self {
226            Anrok => "anrok",
227            Avalara => "avalara",
228            Sphere => "sphere",
229            Stripe => "stripe",
230            Unknown(v) => v,
231        }
232    }
233}
234
235impl std::str::FromStr for CustomerTaxProvider {
236    type Err = std::convert::Infallible;
237    fn from_str(s: &str) -> Result<Self, Self::Err> {
238        use CustomerTaxProvider::*;
239        match s {
240            "anrok" => Ok(Anrok),
241            "avalara" => Ok(Avalara),
242            "sphere" => Ok(Sphere),
243            "stripe" => Ok(Stripe),
244            v => {
245                tracing::warn!("Unknown value '{}' for enum '{}'", v, "CustomerTaxProvider");
246                Ok(Unknown(v.to_owned()))
247            }
248        }
249    }
250}
251impl std::fmt::Display for CustomerTaxProvider {
252    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
253        f.write_str(self.as_str())
254    }
255}
256
257impl std::fmt::Debug for CustomerTaxProvider {
258    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
259        f.write_str(self.as_str())
260    }
261}
262#[cfg(feature = "serialize")]
263impl serde::Serialize for CustomerTaxProvider {
264    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
265    where
266        S: serde::Serializer,
267    {
268        serializer.serialize_str(self.as_str())
269    }
270}
271impl miniserde::Deserialize for CustomerTaxProvider {
272    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
273        crate::Place::new(out)
274    }
275}
276
277impl miniserde::de::Visitor for crate::Place<CustomerTaxProvider> {
278    fn string(&mut self, s: &str) -> miniserde::Result<()> {
279        use std::str::FromStr;
280        self.out = Some(CustomerTaxProvider::from_str(s).expect("infallible"));
281        Ok(())
282    }
283}
284
285stripe_types::impl_from_val_with_from_str!(CustomerTaxProvider);
286#[cfg(feature = "deserialize")]
287impl<'de> serde::Deserialize<'de> for CustomerTaxProvider {
288    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
289        use std::str::FromStr;
290        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
291        Ok(Self::from_str(&s).expect("infallible"))
292    }
293}